Skip to content

Commit

Permalink
fix(i18n): allow multi-context (#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcabrera authored Jun 7, 2021
1 parent 8bae02a commit afcf623
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exports[`I18n Component should attempt to perform a component translate: transla
exports[`I18n Component should attempt to perform a string replace: translate 1`] = `
Object {
"localeKey": "t(lorem.ipsum)",
"multiContext": "t(lorem.ipsum, {\\"context\\":\\"hello_world\\"})",
"placeholder": "t(lorem.ipsum, hello world)",
}
`;
Expand Down
4 changes: 3 additions & 1 deletion src/components/i18n/__tests__/i18n.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ describe('I18n Component', () => {
it('should attempt to perform a string replace', () => {
const localeKey = translate('lorem.ipsum');
const placeholder = translate('lorem.ipsum', 'hello world');
const multiContext = translate('lorem.ipsum', { context: ['hello', 'world'] });

expect({
localeKey,
placeholder
placeholder,
multiContext
}).toMatchSnapshot('translate');
});

Expand Down
12 changes: 9 additions & 3 deletions src/components/i18n/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@ import { helpers } from '../../common/helpers';
* @returns {string|Node}
*/
const translate = (translateKey, values = null, components) => {
const updatedValues = values;

if (Array.isArray(updatedValues?.context)) {
updatedValues.context = updatedValues.context.join('_');
}

if (helpers.TEST_MODE) {
return helpers.noopTranslate(translateKey, values, components);
return helpers.noopTranslate(translateKey, updatedValues, components);
}

if (components) {
return (
(i18next.store && <Trans i18nKey={translateKey} values={values} components={components} />) || (
(i18next.store && <Trans i18nKey={translateKey} values={updatedValues} components={components} />) || (
<React.Fragment>t({translateKey})</React.Fragment>
)
);
}

return (i18next.store && i18next.t(translateKey, values)) || `t(${translateKey})`;
return (i18next.store && i18next.t(translateKey, updatedValues)) || `t(${translateKey})`;
};

/**
Expand Down

0 comments on commit afcf623

Please sign in to comment.