Simple tools for check `react-i18next` keys in app
npm install react-i18next-lint> Simple tools for check react-i18next keys in whole app which use regexp.



> for react-intl use react-intl-lint
>
> for ngx-translate use ngx-translate-lint
- Background
- Installation
- Usage
- CLI
- TypeScript
- Contribute
- Used By
- License
There are a lot of translation [react-i18next][react-i18next] keys in the whole app.
This repository contains a proposal to check all translation keys in the whole app
which should exist in all languages files.
``bash`
npm install react-i18next-lint -g
The source code are available for download
at [GitHub Releases][github-release-url] and
[GitHub pages][github-pages-url] as well.
`text
Usage: react-i18next-lint [options]
Simple CLI tools for check react-i18next keys in app
Options:
-p, --project [glob]
The path to project folder
Possible Values:
-l, --languages [glob]
The path to languages folder
Possible Values:
--kv, --keysOnViews [enum]
Described how to handle the error of missing keys on view
Possible Values:
--zk, --zombieKeys [enum]
Described how to handle the error of zombies keys. Zombie keys are keys that doesn't exist on any languages file but exist on project, or exist languages but doesn't exist on project
Possible Values:
--ek, --emptyKeys [enum]
Described how to handle empty value on translate keys. Empty keys are keys that doesn't have any value on languages files
Possible Values:
-i, --ignore [glob]
Ignore projects and languages files
Possible Values:
--maxWarning [glob]
Max count of warnings in all files. If this value more that count of warnings, then an error is return
Possible Values:
--mk, --misprintKeys [enum]
Try to find matches with misprint keys on views and languages keys. Coefficient: 0.9. Can be longer process!!
Possible Values:
--ds, --deepSearch [enum]
Add each translate key to global regexp end try to find them on project. Can be longer process!!
Possible Values:
--mc, --misprintCoefficient [number]
Coefficient for misprint option can be from 0 to 1.0.
-c, --config [path]
Path to config via JSON or JS file
Possible Values:
--fz, --fixZombiesKeys [boolean]
Auto fix zombies keys on languages files
-v, --version Print current version of ngx-translate-lint
-h, --help display help for command
Examples:
$ npx react-i18next-lint -p ./src/app/*/.{html,ts,js} -l ./src/assets/i18n/*.json
$ react-i18next-lint -p ./src/app/*/.{html,ts,js} -l ./src/assets/i18n/*.json
$ react-i18next-lint -p ./src/app/*/.{html,ts,js} -z disable -v error
$ react-i18next-lint -p ./src/app/*/.{html,ts,js} -l https://8.8.8.8/locales/EN-eu.json
`
> NOTE: For project and languages options need to include file types like on the example.
Default JSON Config is:
`json`
{
"rules": {
"keysOnViews": "error",
"zombieKeys": "warning",
"misprintKeys": "disable",
"deepSearch": "disable",
"emptyKeys": "warning",
"maxWarning": "0",
"misprintCoefficient": "0.9",
"ignoredKeys": [ "IGNORED.KEY.(.*)" ], // can be string or RegExp
"ignoredMisprintKeys": [],
"customRegExpToFindKeys": [ "(?<=marker\\(['\"])([A-Za-z0-9_\\-.]+)(?=['\"]\\))"], // to find: marker('TRSNLATE.KEY');
},
"fetch": {
"requestQuery": "",
"requestHeaders": {},
"responseQuery": ""
},
"fixZombiesKeys": false,
"project": "./src/app/*/.{html,ts}",
"languages": "./src/assets/i18n/*.json"
}
JS Config should have default export via object like config. See example:
Example JS config is:
`javascript
const config = {
rules: {
keysOnViews: "error",
zombieKeys: "warning",
emptyKeys: "warning",
misprint: {
type: "warning",
coefficient: 0.9
},
ignoredKeys: [],
ignoredMisprintKeys: []
},
fetch: {
requestQuery: "",
requestHeaders: {},
responseQuery: "",
get: async () => {
const requestOne = fetch('https://8.8.8.8/locales/EN-eu.json');
const requestTwo = fetch('https://8.8.8.8/locales/EN-us.json');
const result = await Promise.all([requestOne, requestTwo]).then(async ([responseOne, responseTwo]) => {
return {
...(await responseOne.json()),
...(await responseTwo.json())
}
});
// NOTE: result should contains only translation keys. Example
// {
// "translation.key": "value"
// }
return result;
}
},
fixZombiesKeys: false,
project: "./src/app/*/.{html,ts}",
languages: "./src/assets/i18n/*.json"
}
export default config;
`
#### How to write Custom RegExp
We have (?<=marker\\(['\"])([A-Za-z0-9_\\-.]+)(?=['\"]\\)) RegExp witch contains of 3 parts:
- Prefix - (?<=marker\\(['\"])(?<=
- This construction tells that what we need matching before translate key
- start with and end ).marker\\(['\"]
- - tells that we try to find word market witch have on the second character 'or "market
- To summarize, we are trying to find keys before each word to be and commas ' or "
- Matching for key: ([A-Za-z0-9_\\-.]+)_
- This construction tells that we find and save all words which contain alphabet, numbers, and or -.(.*)
- We recommend using this part of RegExp to find and save translated keys
- But you can also use If it's enough for your project(?=['\"]\\))
- Postfix - (the same as prefix, but need to be ended)(?=
- This construction tells that what we need matching after translate key
- start with and end )['\"]\\)
- - tells that we try to find word comas ' or " and ended with )'
- To summarize, we are trying to find keys ended each word to be commas or " and )
Example RegExp will find following keys
- marker('TRSNLATE.KEY')marker("TRSNLATE.KEY-2")
-
#### Exit Codes
The CLI process may exit with the following codes:
- 0: Linting succeeded without errors (warnings may have occurred)1
- : Linting failed with one or more rule violations with severity error2
- : An invalid command line argument or combination thereof was used
`typescript
import {
ToggleRule,
NgxTranslateLint,
IRulesConfig,
ResultCliModel,
ErrorTypes,
LanguagesModel,
IFetch,
ngxTranslateRegEx,
} from 'ngx-translate-lint';
const viewsPath: string = './src/app/*/.{html,ts}';
const languagesPath: string = './src/assets/i18n/*.json';
const ignoredLanguagesPath: string = "./src/assets/i18n/ru.json, ./src/assets/i18n/ru-RU.json";
const ruleConfig: IRulesConfig = {
keysOnViews: ErrorTypes.error,
zombieKeys: ErrorTypes.warning,
misprintKeys: ErrorTypes.disable,
deepSearch: ToggleRule.disable,
emptyKeys: ErrorTypes.warning,
maxWarning: 0,
misprintCoefficient: 0.9,
fixZombiesKeys: false,
ignoredKeys: ['EXAMPLE.KEY', 'IGNORED.KEY.(.*)'], // can be string or RegExp
ignoredMisprintKeys: [],
customRegExpToFindKeys: ["(?<=marker\\(['\"])([A-Za-z0-9_\\-.]+)(?=['\"]\\))"] // to find: marker('TRSNLATE.KEY');
};
const fixZombiesKeys: boolean = false;
const fetchSettings: IFetch = {
requestQuery: "",
requestHeaders: {},
responseQuery: "",
get: () => {
// You fetch to get locales
}
};
const ngxTranslateRegEx: ngxTranslateRegEx = ngxTranslateRegEx; // Here can be your array of regexp to find keys
const ngxTranslateLint = new NgxTranslateLint(viewsPath, languagesPath, ignoredLanguagesPath, ruleConfig, fixZombiesKeys, fetchSettings, ngxTranslateRegEx)
const resultLint: ResultCliModel = ngxTranslateLint.lint(); // Run Lint
const languages: LanguagesModel[] = ngxTranslateLint.getLanguages() // Get Languages with all keys and views
``
You may contribute in several ways like requesting new features,
adding tests, fixing bugs, improving documentation or examples.
Please check our [contributing guidelines][contributing].
Here can be your extensions:
- ngx-translate-editor - Simple GUI for CRUD translate keys of ngx-translate, which included ngx-translate-lint
- 121 Platform - 121 is an open source platform for Cash based Aid built with Digital Identity & Local/Global Financial service partners.
[MIT][license-url]
[react-i18next]: https://react.i18next.com/
[semantic-shield]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
[semantic-url]: https://github.com/semantic-release/semantic-release
[npm-shield]: https://img.shields.io/npm/v/svoboda-rabstvo/react-i18next-lint.svg
[npm-url]: https://www.npmjs.com/package/react-i18next-lint
[npm]: https://www.npmjs.com
[node-js]: https://nodejs.org
[github-shield]: https://img.shields.io/github/release/svoboda-rabstvo/react-i18next-lint.svg?label=github
[github-url]: https://github.com/svoboda-rabstvo/react-i18next-lint
[github-release-url]: https://github.com/svoboda-rabstvo/react-i18next-lint/releases
[github-pages-url]: https://svoboda-rabstvo.github.io/react-i18next-lint/
[schema-url]: http://json-schema.org/
[doc-url]: https://github.com/svoboda-rabstvo/react-i18next-lint/blob/develop/doc
[license-url]: https://github.com/svoboda-rabstvo/react-i18next-lint/blob/develop/LICENSE.md
[meta-url]: https://en.wikipedia.org/wiki/List_of_software_package_management_systems#Meta_package_managers
[contributing]: https://github.com/svoboda-rabstvo/react-i18next-lint/blob/develop/.github/CONTRIBUTING.md