-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transform nested translations into flat translations during assignment
- Loading branch information
1 parent
fad0768
commit 76a4d1f
Showing
9 changed files
with
111 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/plugins/piral-translate/src/flatten-translations.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* @vitest-environment jsdom | ||
*/ | ||
import { describe, it, expect } from 'vitest'; | ||
import { flattenTranslations } from './flatten-translations'; | ||
|
||
describe('Flatten translations', () => { | ||
it('flattenTranslations can handle flat keys', () => { | ||
const messages = { | ||
en: { | ||
key: 'value' | ||
}, | ||
fr: { | ||
key: 'value (fr)' | ||
} | ||
}; | ||
const flatMessages = flattenTranslations(messages); | ||
expect(flatMessages.fr.key).toEqual('value (fr)'); | ||
}); | ||
|
||
it('flattenTranslations can handle flat dot keys', () => { | ||
const messages = { | ||
en: { | ||
'header.title': 'Hello world' | ||
} | ||
}; | ||
const flatMessages = flattenTranslations(messages); | ||
expect(flatMessages.en['header.title']).toBe('Hello world'); | ||
}); | ||
|
||
it('flattenTranslations can handle nested keys', () => { | ||
const messages = { | ||
en: { | ||
header: { | ||
title: 'Hello world' | ||
} | ||
} | ||
}; | ||
const flatMessages = flattenTranslations(messages); | ||
expect(flatMessages.en['header.title']).toBe('Hello world'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { flatten } from 'flat'; | ||
import { LocalizationMessages, NestedLocalizationMessages } from './types'; | ||
|
||
export function flattenTranslations(messages: LocalizationMessages | NestedLocalizationMessages): LocalizationMessages { | ||
return Object.fromEntries( | ||
Object | ||
.entries(messages) | ||
.map(([ language, translations ]) => { | ||
return [ | ||
language, | ||
flatten(translations) | ||
] | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2443,11 +2443,6 @@ | |
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" | ||
|
@@ -6081,6 +6076,11 @@ flat@^5.0.2: | |
resolved "https://registry.yarnpkg.com/flat/-/flat-5.0.2.tgz#8ca6fe332069ffa9d324c327198c598259ceb241" | ||
integrity sha512-b6suED+5/3rTpUBdG1gupIl8MPFCAMA0QXwmljLhvCUKcUvdE4gWky9zpuGCcXHOsz4J9wPGNWq6OKpmIzz3hQ== | ||
|
||
flat@^6.0.1: | ||
version "6.0.1" | ||
resolved "https://registry.yarnpkg.com/flat/-/flat-6.0.1.tgz#09070cf918293b401577f20843edeadf4d3e8755" | ||
integrity sha512-/3FfIa8mbrg3xE7+wAhWeV+bd7L2Mof+xtZb5dRDKZ+wDvYJK4WDYeIOuOhre5Yv5aQObZrlbRmk3RTSiuQBtw== | ||
|
||
flexsearch@^0.6.32: | ||
version "0.6.32" | ||
resolved "https://registry.yarnpkg.com/flexsearch/-/flexsearch-0.6.32.tgz#1e20684d317af65baa445cdd9864a5f5b320f510" | ||
|
@@ -6355,13 +6355,6 @@ [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" | ||
|