Skip to content

Commit

Permalink
Enable nested translations
Browse files Browse the repository at this point in the history
  • Loading branch information
kkasperconsolinno committed Nov 27, 2023
1 parent 07d38da commit 14d41b4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/plugins/piral-translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/piral-translate/src/localize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const messages = {
en: {
hi: 'hello',
greeting: 'Hi {{name}}, welcome back',
header: {
title: 'Hello world'
}
},
de: {
hi: 'hallo',
Expand Down Expand Up @@ -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');
});
});
3 changes: 2 additions & 1 deletion src/plugins/piral-translate/src/localize.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import getValue from 'get-value';
import { LocalizationMessages, Localizable } from './types';

function defaultFallback(key: string, language: string): string {
Expand Down Expand Up @@ -69,7 +70,7 @@ export class Localizer implements Localizable {
private translateMessage<T extends object>(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);
}
}
1 change: 1 addition & 0 deletions src/plugins/piral-translate/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"esModuleInterop": true,
"skipLibCheck": true,
"lib": ["dom", "es2015", "es2018"],
"jsx": "react",
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -6350,6 +6355,13 @@ [email protected]:
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"
Expand Down

0 comments on commit 14d41b4

Please sign in to comment.