Package Exports
- i18next-json-sync
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (i18next-json-sync) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
i18next-json-sync
Keeps i18next JSON resource files in sync against a primary language, including plural forms. When hooked up to a build process/CI server, ensures keys added/removed from one language are correctly propagated to the other languages, reducing the chance for missing or obselete keys, merge conflicts, and typos.
Example
Given these files:
locales
├── en.json
├── fr.json
└── ru.jsonen.json
{
"key_one": "value",
"book": "book",
"book_plural": "books"
}
fr.json
{
"key_one": "french value"
}
ru.json
{
"extra_key": "extra value"
}fr.json and ru.json can be synced against en.json:
import sync from 'i18next-json-sync'
sync({
files: 'locales/*.json',
primary: 'en'
});(or via CLI: sync-i18n --files locales/*.json --primary en)
resulting in:
en.json
{
"key_one": "value",
"book": "book",
"book_plural": "books"
}
fr.json
{
"key_one": "french value",
"book": "book",
"book_plural": "books"
}
ru.json
{
"key_one": "value",
"book_0": "books",
"book_1": "books",
"book_2": "books"
}key_one was left alone in fr.json since it's already localized, but book and book_plural were copied over.
An extraneous key in ru.json was deleted and keys from en.json copied over. Plurals are mapped between
languages according to the i18next suffix rules.
This works on one folder at a time, but can deal with whatever the files glob returns. Files are grouped into directories before processing starts. Folders without a 'primary' found are ignored.
Node.js Usage
$ npm install i18next-json-sync --save-dev
import sync from 'i18next-json-sync';
//or in ES5 world:
//const sync = require('i18next-json-sync').default;
//defaults are inline:
sync({
/** Audit files in memory instead of changing them on the filesystem and
* throw an error if any changes would be made */
check: false,
/** Glob pattern for the resource JSON files */
files: '**/locales/*.json',
/** Primary localization language. Other language files will be changed to match */
primary: 'en',
/** Language files to create if they don't exist, e.g. ['es, 'pt-BR', 'fr'] */
createResources: [],
/** Space value used for JSON.stringify */
space: 4
})CLI Usage
It can be installed globally, but npm's package.json scripts are a better fit.
{
"name": "foo",
"scripts": {
"i18n": "sync-i18n --files **/locales/*.json --primary en --languages es fr ja zh ko --space 2",
"check-i18n": "npm run i18n -- --check"
},
"devDependencies": {
"i18next-json-sync" : "^1.0.0"
}
}Then use npm run i18n to sync on the filesystem and npm run check-i18n to validate.
Use sync-i18n -h to get help output:
Options:
-c, --check Audit files in memory instead of changing them on the
filesystem and throw an error if any changes would be made
[boolean]
-f, --files Glob pattern for the resource JSON files
-p, --primary Primary localization language. Other language files will be
changed to match
-l, --languages Language files to create if they don't exist [array]
-s, --space Space value used for JSON.stringify
-h, --help Show help [boolean]
Examples:
--files **/locales/*.json
--languages es fr pt-BR ja