- Notifications
You must be signed in to change notification settings - Fork7
Validate i18n translation files
License
lingualdev/i18n-check
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
i18n-check validates yourICU andi18next translation files and checks for missing and broken translations.It compares the defined source language with all target translation files and finds inconsistencies between source and target files.You can run these checks as a pre-commit hook or on the CI depending on your use-case and setup.
Usingyarn:
yarn add --dev @lingual/i18n-check
Usingnpm:
npm install --save-dev @lingual/i18n-check
Usingpnpm:
pnpm add --save-dev @lingual/i18n-check
After the installation, i18n-check can either be accessed via defining a command in thepackage.json file or directly in the CLI.
Update yourpackage.json and add a new command:
"scripts": { // ...other commands,"i18n:check":"i18n-check"}
Now you can run thei18n:check command directly from the command-line, i.e.yarn i18n:check.
Alternatively you can also access the library directly:
node_modules/.bin/i18n-check
For i18n-check to work you need to provide it at a minimum the source locale (--source, -s) for the primary language and the path to the locale translation files (--locales, -l). Currently supported file formats are JSON and YAML.
Example:
yarn i18n:check -s en-US --locales translations/
Instead of a single source file you can also pass a directory:
yarn i18n:check -s en-US --locales translations/
See theexamples for more details.
With the-l or--locales option you define which folder or multiple folders you want to run the i18n checks against. It is arequired option. i18n-check will try to find all target locale files and compare these files against the defined source file(s).Check theexample to see how different locale translation files are organised and how they can be addressed.
yarn i18n:check --locales translations/messageExamples -s en-US
With the-s or--source option you define the source locale to compare all target files against. It is arequired option. i18n-check will try to find all target locale files and compare these files against the applicable source file(s).Check theexamples to see how different locale translation files are organised and how they can be addressed.
yarn i18n:check --locales translations/messageExamples -s en-US
By default i18n-check will validate against anyICU compliant translations.Additionally thei18next format is supported and can be set via the-f or--format option.
There are i18n libraries that have their own specific format, which might not be based on ICU and therefore can not be validated against currently. On a side-note: there might be future support for more specific formats.
Hint: If you want to use the--unused flag, you should providereact-intl ori18-next as the format. Also see theunused section for more details.
yarn i18n:check --locales translations/i18NextMessageExamples -s en-US -f i18next
By default i18n-check will perform a validation against anymissing and/orinvalid keys, additionallyunused andundefined checks if the--unused option is set. There are situations where only a specific check should run. By using the-o or--only option you can specify a specific check to run.
The available options are:
missingKeys: will check against any missing keys in the target files.invalidKeys: will check for invalid keys, where the target translations has a different type then the one defined in the source file.unused: will check for any locale keys that do not exist in the codebase.undefined: will check for any keys that exist in the codebase but not in the source locale files.
Check for missing keys only:
yarn i18n:check --locales translations/messageExamples -s en-US -o missingKeys
Check for invalid keys only:
yarn i18n:check --locales translations/messageExamples -s en-US -o invalidKeys
Check for unused key only:
yarn i18n:check --locales translations/messageExamples -s en-US -o unused
Check for undefined keys only:
yarn i18n:check --locales translations/messageExamples -s en-US -o undefined
Check for missing and invalid keys (which is the default):
yarn i18n:check --locales translations/messageExamples -s en-US -o missingKeys invalidKeys
Check for unused and undefined keys only:
yarn i18n:check --locales translations/messageExamples -s en-US -o unused undefined
This feature is currently only supported forreact-intl andi18next as well asnext-intl (experimental at the moment) based React applications and is useful when you need to know which keys exist in your translation files but not in your codebase. Additionally an inverse check is run to find any keys that exist in the codebase but not in the translation files.
Via the-u or--unused option you provide a source path to the code, which will be parsed to find all unused as well as undefined keys in the primary target language.
It is important to note that you must also provide the-f or--format option withreact-intl,i18next ornext-intl as value. See theformat section for more information.
yarn i18n:check --locales translations/messageExamples -s en-US -u client/ -f react-intl
or
yarn i18n:check --locales translations/messageExamples -s en-US -u client/ -f i18next
or
yarn i18n:check --locales translations/messageExamples -s en-US -u client/ -f next-intl
The standard reporting prints out all the missing or invalid keys.Using the-r or--reporter option enables to override the standard error reporting. Passing thesummary option will print a summary of the missing or invalid keys.
yarn i18n:check --locales translations/messageExamples -s en-US -r summary
There are situations where we want to exclude a single or multiple files or a single folder or a group of folders. A typical scenario would be that some keys are missing in a specific folder, as they are being work in progress for example. To exclude this or these files/folders you can use the-e or--exclude option. It expects one or more files and/or folders.
To exclude a single file:
yarn i18n:check --locales translations/messageExamples -s en-US -e translations/messageExamples/fr-fr.json
To exclude multiple files provide all files:
yarn i18n:check --locales translations/messageExamples -s en-US -e translations/messageExamples/fr-fr.json translations/messageExamples/de-at.json
To exclude a single folder:
yarn i18n:check --locales translations/folderExamples -s en-US -e translations/folderExamples/fr/*Alternatively you can exclude multiple folders by providing the folders to be excluded:
yarn i18n:check --locales translations/folderExamples -s en-US -e translations/folderExamples/fr/* translations/folderExample/it/*
The--exclude option also accepts a mix of files and folders, which follows the same pattern as above, i.e.-e translations/folderExamples/fr/* translations/messageExamples/it.json
There can be situations where we only want to translate a feature for a specific region and therefore need to ignore any missing key checks against non supported locales. Another scenario is that we know of the missing keys and want to be able to skip these missing keys when running checks. For these aforementioned scenarios, by using the--ignore or-i option you can specify which keys to ignore, additionally also being able to define ignoring all keys inside a defined path, i.e.some.keys.path.*.
To ignore regular keys:
yarn i18n:check --locales translations/folderExamples -s en-US -i some.key.to.ignore other.key.to.ignore
To ignore all keys within a provided path:
yarn i18n:check --locales translations/folderExamples -s en-US -i"some.path.to.keys.*"A mix of regular keys and paths:
yarn i18n:check --locales translations/folderExamples -s en-US -i"some.path.to.keys.*" some.key.to.ignore other.key.to.ignoreWhen using the--unused option, there will be situations where the i18next-parser will not be able to find components that wrap aTrans component.The component names for i18next-parser to match should be provided via the--parser-component-functions option. This option should onlybe used to define additional names for matching, a by defaultTrans will always be matched.
yarn i18n:check --locales translations/i18NextMessageExamples -s en-US -f i18next-u src --parser-component-functions WrappedTransComponent
yarn i18n:check --locales translations/i18NextMessageExamples -s en-US -f i18next-u src --parser-component-functions WrappedTransComponent AnotherWrappedTransComponent
i18n-check is able to load and validate against different locale folder structures. Depending on how the locale files are organized, there are different configuration options.
If all the locales are organized in asingle folder:
locales/ en-en.json de-de.jsonUse the-l or--locales option to define the directory that should be checked for target files. With thes orsource option you can specify the base/reference file to compare the target files against.
yarn i18n:check --locales locales -s locales/en-us.json
If the locales areorganised as folders containing a single JSON/YAML file:
locales/ en-US/ index.json de-DE/ index.jsonDefine thelocales folder as the directory to look for target files.
yarn i18n:check --locales locales -s en-US
If the locales areorganised as folders containing multiple JSON/YAML files:
locales/ en-US/ one.json two.json three.json de-DE/ one.json two.json three.jsonDefine thelocales folder as the directory to look for target files and passlocales/en-US/ as thesource option. i18n-check will try to collect all the files in the provided source directory and compare each one against the corresponding files in the target locales.
yarn i18n:check --locales locales -s en-US
If the locales areorganised as folders containing multiple JSON/YAML files:
- spaceOne - locales/ - en-US/ - one.json - two.json - three.json - de-DE/ - one.json - two.json - three.json- spaceTwo - locales/ - en-US/ - one.json - two.json - three.json - de-DE/ - one.json - two.json - three.jsonDefine thelocales folder as the directory to look for target files and passen-US as thesource option. i18n-check will try to collect all the files in the provided source directory and compare each one against the corresponding files in the target locales.
yarn i18n:check -l spaceOne spaceTwo -s en-US
We currently do not offer an explicitGithub Action you can use out of the box, but if you have i18n-check already installed, you can define your ownYAML file. The following example can be seen as a starting point that you can adapt to your current setup:
name:i18n Checkon:pull_request:branches: -mainpush:branches: -mainjobs:i18n-check:runs-on:ubuntu-lateststeps: -uses:actions/checkout@master -name:yarn install & buildrun:| yarn install yarn build -name:yarn i18n-checkrun:| yarn i18n-check --locales translations/messageExamples --source en-US
The above workflow will return any missing or invalid keys and the action would fail if missing/invalid keys are found:
Aside from using the CLI, i18n-check also exposes a set of check functions that can be accessed programmatically.Start by importing i18n-check:
import*asi18nCheckfrom'@lingual/i18n-check';
checkTranslations expects the base and comparison or target files and returns an object containing the missing and invalid keys. The optionaloptions objects can be provided as a third argument to define the format style via theformat property, this is useful if you want to validatei18next specific translations.
import{checkTranslations}from'@lingual/i18n-check';constoptions={format:'i18next',};const{ invalidKeys, missingKeys}=checkTranslations(source,targets,options);
Additionally theoptions object enables to also define which checks should run via thechecks property, f.e. if you only want to check for missing or invalid keys only.
import{checkTranslations}from'@lingual/i18n-check';constoptions={format:'icu',checks:['invalidKeys'],};const{ invalidKeys}=checkTranslations(source,targets,options);
CallingcheckTranslation will return the following shape:
exporttypeCheckResult=Record<string,string[]>;typeResult={missingKeys:CheckResult|undefined;invalidKeys:CheckResult|undefined;};
The result formissingKeys as well asinvalidKeys is an object containing the provided locales and their corresponding affected keys as an array
{missingKeys:{"de-de":["missing_example_key","some_other_key"],"fr-fr":[],}};
checkMissingTranslations checks for any missing keys in the target files. All files are compared against the source file.
import{checkMissingTranslations}from'@lingual/i18n-check';constresult=checkMissingTranslations(source,targets);// {// "de-de": ["missing_translation_key", "some_other_missing_translation_key"],// "fr-fr": [],// };
The result is an object containing the provided locales and their corresponding missing keys as an array.
checkInvalidTranslations checks if there are any invalid keys in the target files. All files are compared against the source file.
import{checkInvalidTranslations}from'@lingual/i18n-check';constoptions={format:'i18next',};constresult=checkInvalidTranslations(source,targets,options);// {// "de-de": ["invalid_translation_key", "some_other_invalid_translation_key"],// "fr-fr": [],// };
The result is an object containing the provided locales and their corresponding invalid keys as an array.
If you want to checkout and run the code, you need to run thebuild command first.
Runpnpm run build and then depending on the scenario one of the following commands.
Basic icu translation example:
node dist/bin/index.js --locales translations/messageExamples -s en-US
Flatted translation keys example:
node dist/bin/index.js --locales translations/flattenExamples -s en-US
i18next translation example:
node dist/bin/index.js --locales translations/i18NextMessageExamples -s en-US -f i18next
Single file translation example:
node dist/bin/index.js --locales translations/folderExample -s en-US
Multiple files per folder translation example:
node dist/bin/index.js --locales translations/multipleFilesFolderExample/ -s en-US
Multiple folders containing locales translation example:
node dist/bin/index.js --locales translations/folderExample,translations/messageExamples -s en-US
To run the tests use one of the following commands:
pnpmtestAbout
Validate i18n translation files
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors7
Uh oh!
There was an error while loading.Please reload this page.


