diff --git a/packages/react/src/components/adapters/Consent.tsx b/packages/react/src/components/adapters/Consent.tsx index fbe14bae..11a1f6eb 100644 --- a/packages/react/src/components/adapters/Consent.tsx +++ b/packages/react/src/components/adapters/Consent.tsx @@ -94,9 +94,12 @@ export interface ConsentProps { t?: UseTranslation['t']; } -const defaultConfig: Required> = { - essential: 'Essential Attributtes', - optional: 'Optional Attributes', +// default config for consent related translation keys +const defaultConfig: ConsentConfig = { + essential: 'essential', + optional: 'optional', + essentialInfo: 'essential_info', + optionalInfo: 'optional_info', }; /** @@ -140,14 +143,32 @@ const Consent: FC = ({ if (!text || (!t && !meta)) { return text || ''; } - return resolveFlowTemplateLiterals(text, {meta, t: t || ((k: string): string => k)}); + // first check if the key is present in the translation file, + // if not then resolve the template literals + const consentKey = `consent.${text}`; + const translated: string = t ? t(consentKey) : consentKey; + + // if the translated value is same as the consent key, + // then resolve the template literals + const resolvedValue = + translated === consentKey + ? resolveFlowTemplateLiterals(text, {meta, t: t || ((k: string): string => k)}) + : translated; + + // if the resolved value is same as the original text, + // then return empty string + return resolvedValue === text ? '' : resolvedValue; }; const config: ConsentConfig = {...defaultConfig, ...suppliedConfig}; - const essentialInfo = typeof config.essentialInfo === 'string' ? resolve(config.essentialInfo.trim()) : ''; - const optionalInfo = typeof config.optionalInfo === 'string' ? resolve(config.optionalInfo.trim()) : ''; - const essentialLabel = resolve(config['essential']); - const optionalLabel = resolve(config['optional']); + const essentialInfo = resolve(config['essentialInfo']); + const optionalInfo = resolve(config['optionalInfo']); + /** + * Falls back to default config values if essential/optional keys + * cannot be resolved via translation files or meta template literals. + */ + const essentialLabel = resolve(config['essential']) || 'Essential Attributes'; + const optionalLabel = resolve(config['optional']) || 'Optional Attributes'; /** * Method to check whether master toggle button is checked or not @@ -223,6 +244,7 @@ const Consent: FC = ({ purpose={purpose} formValues={formValues} onInputChange={onInputChange} + t={t} /> )} @@ -252,6 +274,7 @@ const Consent: FC = ({ purpose={purpose} formValues={formValues} onInputChange={onInputChange} + t={t} /> )} diff --git a/packages/react/src/components/adapters/ConsentCheckboxList.tsx b/packages/react/src/components/adapters/ConsentCheckboxList.tsx index 00d20040..922c1ae7 100644 --- a/packages/react/src/components/adapters/ConsentCheckboxList.tsx +++ b/packages/react/src/components/adapters/ConsentCheckboxList.tsx @@ -8,6 +8,7 @@ import useTheme from '../../contexts/Theme/useTheme'; import {cx} from '../../styles/emotion'; import Toggle from '../primitives/Toggle/Toggle'; import Typography from '../primitives/Typography/Typography'; +import {UseTranslation} from '../../hooks/useTranslation'; /** * Computes the form value key for tracking an optional attribute's consent state. @@ -78,6 +79,10 @@ export interface ConsentCheckboxListProps { purpose: ConsentPurposeData; /** Whether to render essential (disabled) or optional (toggleable) attributes */ variant: ConsentInputVariant; + /** + * translation data + */ + t?: UseTranslation['t']; } /** @@ -93,10 +98,21 @@ const ConsentCheckboxList: FC = ({ formValues, onInputChange, children, + t, }: ConsentCheckboxListProps) => { const {theme, colorScheme}: ReturnType = useTheme(); const styles: Record = useStyles(theme, colorScheme); + /** Resolve any remaining {{t()}} or {{meta()}} template expressions in a string at render time. */ + const resolve = (text: string | undefined): string => { + if (!text || !t) { + return text || ''; + } + + const translated: string = t(`consent.${text}`); + return translated === `consent.${text}` ? text : translated; + }; + const attributes: string[] = (variant === 'ESSENTIAL' ? purpose.essential : purpose.optional).map( (e): string => e.name, ); @@ -153,11 +169,11 @@ const ConsentCheckboxList: FC = ({ styles['typography'], )} > - {attr} + {resolve(attr)} {isEssential ? ( - Required + {resolve('required')} ) : (