jwbay / i18next-json-sync Goto Github PK
View Code? Open in Web Editor NEWKeep i18next JSON resource files in sync
License: MIT License
Keep i18next JSON resource files in sync
License: MIT License
I have two scripts in my package.json
for this package.
"i18n": "sync-i18n --files src/assets/i18n/*.json --primary de --languages en --space 2 --newkeysempty",
"check-i18n": "npm run i18n -- --check"
i18n
adds new keys to all other languages.
check-i18n
checks for missing keys.
check-i18n
should check if all keys are translated. But it doesn't fail for empty keys inserted by i18n
.
Only for missing ones.
I try run next command
node src/scripts/syncTranslateResources.js
where
scrypt is:
`const sync = require('i18next-json-sync').default;
try{
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: 'src/assets/lang/.json',
/** Primary localization language. Other language files will be changed to match /
primary: 'uk',
/* Language files to create if they don't exist, e.g. ['es, 'pt-BR', 'fr'] /
createResources: [],
/* Space value used for JSON.stringify when writing JSON files to disk /
space: 4,
/* Line endings used when writing JSON files to disk. Either LF or CRLF /
lineEndings: 'LF',
/* Insert a final newline when writing JSON files to disk /
finalNewline: false,
/* Use empty string for new keys instead of the primary language value */
newKeysEmpty: false
})
}catch(e){
console.log(e);
}`
and every time get error:
SyntaxError: Unexpected token in JSON at position 0 at JSON.parse (<anonymous>) at C:\Proj\AdSaver\LmsAdSaver\node_modules\i18next-json-sync\dist\LocalizationFolder.js:19:38 at Array.map (<anonymous>) at LocalizationFolder.populateFromDisk (C:\Proj\AdSaver\LmsAdSaver\node_modules\i18next-json-sync\dist\LocalizationFolder.js:17:57) at sync (C:\Proj\AdSaver\LmsAdSaver\node_modules\i18next-json-sync\dist\index.js:20:16) at Object.<anonymous> (C:\Proj\AdSaver\LmsAdSaver\src\scripts\syncTranslateResources.js:4:3) at Module._compile (internal/modules/cjs/loader.js:689:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) at Module.load (internal/modules/cjs/loader.js:599:32) at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
"i18n": "sync-i18n --files 'src/app/i18n/*.json' --primary pt_br --languages es --space 4 --newKeysEmpty 'true'"
this is my configuration, but the new keys added to es.json still using primary language text, i want it to be empty;
can you add an option to have an end of line at the end of the localization files? I edit the keys manually and my editor automatically remove whitespaces and add an empty line at the end of the file, and the --check
option fails because of that.
Our translation team has been known to accidentally translate replacement tokens. It would be awesome if this lib could parse the format and consistency of interpolation tokens. Bonus points if we could support ICU plurals in the replacements; but, I realize this is probably an entirely different ticket and feature as it looks like i18n prefers the plural suffixes by default.
// en.json
{
"hello": "hello {{name}}"
}
// es.json
{
"hello": "hola {{nombre}}
}
It seems this pattern doesn't work:
$ ls static/locales/**/*.json
static/locales/cs/common.json static/locales/de/common.json static/locales/en/common.json
The parsing of the json file seems to fail if there's a trailing newline and doesn't seem to consult the finalNewline
option. I recognize the finalNewline is likely an output option, but would be great if we could have an option to control if it's allowed in the source translation files.
it's pretty common on *nix systems, editor configs, and IDE formatters to insert a trailing newline.
I actually had to remove --check
from my command to format so I could find the issue.
npx sync-i18n --files '**/src/i18n/*.json' --primary "en-us" --space 4 --finalNewline true --check
For example,
// en.json
{
"one": "singular message",
"one_plural": "plural message"
}
Should be for he (Hebrew) synced as
// he.json
{
"one": "singular message",
"one_0": "singular message",
"one_1": "singular message",
"one_2": "plural message",
"one_3": "plural message",
}
Currently the sync will produce
// he.json
{
"one": "singular message",
"one_plural": "plural message"
}
For more details there is a playground that demonstrates it
It probably related to https://github.com/i18next/i18next/blob/master/src/PluralResolver.js
Hi,
I created the node.js file with the default parameters. I'm using typescript so I have this script in package.json:
"i18n": "ts-node ./scripts/node.js"
This runs fine and generates the language files, but the language files are still in english. How can I get it to automatically translate the new keys to the proper language?
Hi, this package looks very useful, but unfortunately it is reordering all my JSON keys into alphabetical order. So my en.json got changed to, for example,
"april": "April",
"august": "August",
"december": "December",
"february": "February",
"january": "January",
"july": "July",
"june": "June",
"march": "March",
"may": "May",
"november": "November",
"october": "October",
"september": "September"
I would much prefer to preserve the ordering in en.json which is logical and therefore helps the translator to translate. Can we have an option for this? Based on the example in your documentation, I did not expect the keys to reorder.
In some particular scenario, ordered keys are required. I found i18next-json-sync
will change the keys' ordering; I digged the code but don't know where it sort key nor know why it can.
A suggestion about the option:
sortKeys: 'followPrimaryLang' | 'always'
cli.ts
and dist/cli.js
have CRLF (Windows) line ending instead LF (Unix)
Line 1 in e7f06e7
So it impossible to run script from package.json
{
"name": "my-app",
"scripts": {
"i18n": "sync-i18n --files '**/locales/*.json' --primary en --languages es fr ja zh ko --space 2",
"check-i18n": "npm run i18n -- --check"
}
}
$ yarn i18n
/usr/bin/env: ‘node\r’: No such file or directory
Temporary solution - execute sync-i18n directly:
{
"name": "my-app",
"scripts": {
"i18n": "node node_modules/.bin/sync-i18n --files '**/locales/*.json' --primary en --languages es fr ja zh ko --space 2",
"check-i18n": "npm run i18n -- --check"
}
}
I suggest this TODO list to improve repo:
OS: Arch Linux
Node.js: 10.7.0
Yarn: 1.9.2
i18next-json-sync: 2.2.0 (latest)
Hello,
Thanks for this tool, it helps us a lot, but I found the next issue with plurals recognition:
When we have filenames with country codes (en-US.json
and ru-RU.json
), i18next-json-sync doesn't correctly recognize language plurals for ru-RU and removes key_0
and other plural keys from it. It pushes key_plural
instead, which is incorrect for ru-RU language. Looks like, it doesn't work, when there is -
character in file name.
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: './js/i18n/languages/.json',
/** Primary localization language. Other language files will be changed to match /
primary: 'zh',
/* Language files to create if they don't exist, e.g. ['es, 'pt-BR', 'fr'] /
createResources: ['fr'],
/* Space value used for JSON.stringify when writing JSON files to disk /
space: 4,
/* Line endings used when writing JSON files to disk. Either LF or CRLF */
lineEndings: 'LF'
})
sync en.json will be {} , is empty;
primary: 'zh' is fine
When using wildcards in package.json
as shown in the CLI example, globbing behaves unpredictably on OSX. In my case, I used --files wwwroot/locales/*.json
. When run from the CLI, this caused globbing to be handled by the shell instead of Node, strangely enough resulting in the script only finding the first file in the folder.
Escaping the wildcards ensures that globbing is handled by Node, avoiding the unpredictability. Thus, the example should look like this:
"i18n": "sync-i18n --files \\*\\*/locales/\\*.json --primary en --languages es fr ja zh ko --space 2"
The readme says we can use -h
to show help .
But it do nothing
To repair this, you need to add here
.alias('h', 'help')
or update the readm
Instead of having to run
sync-i18n --files '**/locales/*.json' --primary en --languages es fr ja zh ko --space 2
It would be nice to be able to run
sync-i18n i18next-json-sync.config.js
Where i18next-json-sync.config.js
would be:
module.exports = {
files: '**/locales/*.json',
primary: 'en',
createResources: ["en", "fr", "ja", "zh", "ko"],
space: 2
}
Hi
My files are organized in namespaces like this
How can i use this setup with your tool? I can't get it to work
If I run npx sync-i18n --files ./locale/*.json --primary en --space 4
, my JSON ends up looking like this:
{
4"_name": "English"
}
rather than
{
"_name": "English"
}
This does not happen on 2.3.1
, but happens on any newer version.
We're going to be working with YAML files instead of JSON in our project that is using i18next. I like this project but it doesn't support YAML.
If there is some interest in adding that support I'd be more than happy to put together a PR to add it.
Thoughts?
I am new to this package and am trying to use it to sync different language files for my angular2/nodejs project. But the given usage is not working for me.
My lang file structure is (multiple levels deep):
And my package.json has:
{
"scripts": {
...
"i18n": "sync-i18n --files './public/app/assets/language/*.json' --primary en --languages es fr ja",
"check-i18n": "npm run i18n -- --check",
...
}
"devDependencies": {
...
"i18next-json-sync": "2.2.0",
...
}
}
But when I run npm run check-i18n
or npm run i18n
I do not see any key/values being copied from en file (primary) to other language files. What am I missing?
@jwbay Does this package support folder namespace support? For example:
examples
├── en
│ └── namespace.json
├── de
│ └── namespace.json
└── es
└── namespace.json
Thanks in advance
en.json
{
"one": "one",
"two": "two",
"two_plural": "two_plural",
"three": "three",
"three_plural": "three_plural"
}
de.json
{
"two": "two",
"two_plural": "two_plural",
"three": "three",
"three_plural": "three_plural"
}
ja.json
{
"two": "two",
"three": "three",
}
After syncing, one
is only added to ja, when it should be added to de as well. Only happens with two plurals, so a comparison with plural form count is likely going wrong somewhere in 24abe89
function targetPluralsPopulated(target, key) {
//given 'x' for key, do we have 'x' and 'x_plural' for en?
return (
Object.keys(target).filter(function(targetKey) {
return keyMatchesPluralForLanguage(targetKey, targetLanguage);
}).length === getPluralsForLanguage(targetLanguage).length
);
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.