diff --git a/src/plugins/piral-translate/package.json b/src/plugins/piral-translate/package.json index 95b9d15cf..c2b7b7238 100644 --- a/src/plugins/piral-translate/package.json +++ b/src/plugins/piral-translate/package.json @@ -63,10 +63,12 @@ "test": "echo \"Error: run tests from root\" && exit 1" }, "dependencies": { - "deepmerge": "^4.2.2" + "deepmerge": "^4.2.2", + "get-value": "^3.0.1" }, "devDependencies": { "@types/deepmerge": "^2.2.0", + "@types/get-value": "^3.0.5", "@types/react": "^18.0.0", "piral-core": "^1.3.3", "react": "^18.0.0" diff --git a/src/plugins/piral-translate/src/localize.test.ts b/src/plugins/piral-translate/src/localize.test.ts index 56255e433..91ea96a1c 100644 --- a/src/plugins/piral-translate/src/localize.test.ts +++ b/src/plugins/piral-translate/src/localize.test.ts @@ -8,6 +8,9 @@ const messages = { en: { hi: 'hello', greeting: 'Hi {{name}}, welcome back', + header: { + title: 'Hello world' + } }, de: { hi: 'hallo', @@ -76,4 +79,11 @@ describe('Localize Module', () => { const result = localizer.localizeGlobal('greeting', { name: undefined }); expect(result).toBe('Hi , welcome back'); }); + + it('localizeGlobal uses nested translations if the key contains dots', () => { + const localizer = new Localizer(messages, 'en'); + const result = localizer.localizeGlobal('header.title'); + + expect(result).toBe('Hello world'); + }); }); diff --git a/src/plugins/piral-translate/src/localize.ts b/src/plugins/piral-translate/src/localize.ts index c2e3567ff..c8bbdc169 100644 --- a/src/plugins/piral-translate/src/localize.ts +++ b/src/plugins/piral-translate/src/localize.ts @@ -1,3 +1,4 @@ +import getValue from 'get-value'; import { LocalizationMessages, Localizable } from './types'; function defaultFallback(key: string, language: string): string { @@ -69,7 +70,7 @@ export class Localizer implements Localizable { private translateMessage(messages: LocalizationMessages, key: string, variables?: T) { const language = this.language; const translations = language && messages[language]; - const translation = translations && translations[key]; + const translation = translations && getValue(translations, key); return translation && (variables ? formatMessage(translation, variables) : translation); } } diff --git a/src/plugins/piral-translate/tsconfig.json b/src/plugins/piral-translate/tsconfig.json index 3c8e07c9b..9b9a41644 100644 --- a/src/plugins/piral-translate/tsconfig.json +++ b/src/plugins/piral-translate/tsconfig.json @@ -1,6 +1,7 @@ { "extends": "../../../tsconfig.json", "compilerOptions": { + "esModuleInterop": true, "skipLibCheck": true, "lib": ["dom", "es2015", "es2018"], "jsx": "react", diff --git a/yarn.lock b/yarn.lock index 5523ec5c3..95f785688 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2443,6 +2443,11 @@ dependencies: "@types/node" "*" +"@types/get-value@^3.0.5": + version "3.0.5" + resolved "https://registry.yarnpkg.com/@types/get-value/-/get-value-3.0.5.tgz#4ea0e0b0a31c256636b3e7e0026c2ad38baea6f6" + integrity sha512-+o8nw0TId5cDwtdVrhlc8rvzaxbCU+JksFeu8ZunY9vUaODxngXiNceTFj2gkSwGWNRpe3PtaSWt1y0VB71PvA== + "@types/glob@*": version "8.1.0" resolved "https://registry.yarnpkg.com/@types/glob/-/glob-8.1.0.tgz#b63e70155391b0584dce44e7ea25190bbc38f2fc" @@ -6350,6 +6355,13 @@ get-them-args@1.3.2: resolved "https://registry.yarnpkg.com/get-them-args/-/get-them-args-1.3.2.tgz#74a20ba8a4abece5ae199ad03f2bcc68fdfc9ba5" integrity sha512-LRn8Jlk+DwZE4GTlDbT3Hikd1wSHgLMme/+7ddlqKd7ldwR6LjJgTVWzBnR01wnYGe4KgrXjg287RaI22UHmAw== +get-value@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/get-value/-/get-value-3.0.1.tgz#5efd2a157f1d6a516d7524e124ac52d0a39ef5a8" + integrity sha512-mKZj9JLQrwMBtj5wxi6MH8Z5eSKaERpAwjg43dPtlGI1ZVEgH/qC7T8/6R2OBSUA+zzHBZgICsVJaEIV2tKTDA== + dependencies: + isobject "^3.0.1" + git-raw-commits@^2.0.8: version "2.0.11" resolved "https://registry.yarnpkg.com/git-raw-commits/-/git-raw-commits-2.0.11.tgz#bc3576638071d18655e1cc60d7f524920008d723"