node-i18n-checker
v0.0.88322785
Published
A JS library to validate YAML and JSON translation files
Downloads
14
Maintainers
Readme
node-i18n-checker
Validate YAML and JSON translation files
Fork of https://github.com/linagora/i18n-checker
Usage
checker(options, (err, report) => {
if (err) {
console.log(err);
} else {
console.log('Report:', report);
}
});
Where options
is an object with:
baseDir
: the absolute path to the base directory that contains directories to scandirs
: list of directories, in which:localeDir
: relative path to the directory containing locale files (.json
files)core
:true
if current directory contains core locale files. One of the directories in the list must be marked as core.
verifyOptions
: options to verify locale filesfileType
: the fileType of translation files. Possible values:'yaml'
,'yml'
,'json'
- default:'json'
defaultLocale
: the default locale (default:'en'
)locales
: list of locales (e.g.['en', 'fr', 'vi']
)rules
: list of rules to check (omit to check all rules)
Example
const checker = require('node-i18n-checker');
const reporter = checker.reporters.standard;
const options = {
baseDir: __dirname,
dirs: [{
localeDir: 'backend/i18n/locales',
core: true
}, {
localeDir: 'modules/contact/i18n/locales'
}, {
localeDir: 'modules/calendar/i18n/locales'
}],
verifyOptions: {
fileType: 'json',
defaultLocale: 'en',
locales: ['en', 'fr', 'vi'],
rules: [
'all-locales-present',
'valid-json-file',
'default-locale-translate',
'no-duplicate-with-core',
'all-keys-translated'
]
}
};
checker(options, (err, report) {
reporter(report);
});
Binary
- Install the binary
npm install -g node-i18n-checker
- Put the options in file
.i18n-lintrc.js
module.exports = {
baseDir: __dirname,
...
};
- Run
node-i18n-checker [path/to/.i18n-lintrc.js]
- By default,
node-i18n-checker
will find the file in working directory
- By default,
Rules
all-keys-sorted
All keys must be sorted in translation files.
all-keys-translated
All keys translated in default locale must be translated in other locales.
all-locales-present
All directories must contain translation files for all locales defined in
options.verifyOptions.locales
list.
default-locale-translate
In translation file of the default locale, value must have the same value as key.
key-trimmed
No white space character at the beginning or the end of the key.
no-duplicate-among-modules
Keys are duplicated between modules should be in core.
no-duplicate-with-core
Keys are translated in core module must not be translated again in modules.
no-untranslated-key
All keys used in template files must be translated.
no-unused-key
All locale keys defined in core module must be used in core's template files or modules's template files.
All locale keys defined in a module must be used in that module's template files.
valid-json-file
Every translation files must be valid JSON file and has no duplicate keys.
valid-yaml-file
Every translation files must be valid YAML file and has no duplicate keys.
Development
Run tests
grunt lint # lint JS files
grunt test # run test cases
grunt # grunt lint test
Write a new rule
1/ Create a new file in src/rules
directory named rule-id.js
.
2/ Each rule is a function, it receives the options
and returns a promise
which resolves the report:
module.exports = function(options) {
const report = [{
filePath: 'module/contact/en.json',
messages: [{
severity: 2,
message: 'something wrong'
}, {
//....
}]
}, {
filePath: 'module/calendar/vi.json',
messages: [{
severity: 1,
message: 'something not OK'
}]
}];
return q(report);
}
3/ Update documentation and write test for the new rule.