Simple tools for check `react-i18next` keys in app
npm install react-i18next-translation-checkerreact-i18next keys in whole app which use regexp.
react-intl use react-intl-lint
ngx-translate use ngx-translate-lint
react-i18next][react-i18next] keys in the whole app.
bash
npm install react-i18next-translation-checker -g
`
$3
The source code are available for download
at [GitHub Releases][github-release-url] and
[GitHub pages][github-pages-url] as well.
Usage
$3
`text
Usage: react-i18next-translation-checker [options]
Simple CLI tools for check react-i18next keys in app
Options:
-p, --project [glob] (required)
The path to project folder
Possible Values:
(default: "./src/*/.{html,ts,js}")
-l, --languages [glob] (required)
The path to languages folder
Possible Values:
(default: "./src/assets/i18n/*.json")
-kv, --keysOnViews [enum]
Described how to handle the error of missing keys on view
Possible Values:
(default: "error")
-zk, --zombieKeys [enum]
Described how to handle the error of zombies keys
Possible Values:
(default: "warning")
-ek, --emptyKeys [enum]
Described how to handle empty value on translate keys
Possible Values:
(default: "warning")
-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:
(default: "0")
-ds, --deepSearch [enum]
Add each translate key to global regexp end try to find them on project. Can be longer process!!
Possible Values:
(default: "disable")
-c, --config [path]
Path to the config file.
-V, --version output the version number
-h, --help output usage information
Examples:
$ npx react-i18next-translation-checker -p ./src/app/*/.{html,ts,js} -l ./src/assets/i18n/*.json
$ react-i18next-translation-checker -p ./src/app/*/.{html,ts,js} -l ./src/assets/i18n/*.json
$ react-i18next-translation-checker -p ./src/app/*/.{html,ts,js} -z disable -v error
`
> NOTE: For project and languages options need to include file types like on the example.
Default Config is:
`json
{
"rules": {
"keysOnViews": "error",
"zombieKeys": "warning",
"deepSearch": "disable",
"emptyKeys": "warning",
"maxWarning": "0",
"ignoredKeys": [ "IGNORED.KEY.(.*)" ], // can be string or RegExp
"customRegExpToFindKeys": [ "(?<=marker\\(['\"])([A-Za-z0-9_\\-.]+)(?=['\"]\\))"], // to find: marker('TRSNLATE.KEY');
},
"project": "./src/app/*/.{html,ts,js}",
"languages": "./src/assets/i18n/*.json"
}
`
#### 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 "
- To summarize, we are trying to find keys before each word to be market 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 error
- 2: An invalid command line argument or combination thereof was used
$3
`typescript
import { ToggleRule, ReactI18nextLint, IRulesConfig, ResultCliModel, ErrorTypes, LanguagesModel } from 'react-i18next-translation-checker';
const viewsPath: string = './src/app/*/.{html,ts,js}';
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,
deepSearch: ToggleRule.disable,
emptyKeys: ErrorTypes.warning,
maxWarning: 0,
ignoredKeys: [ 'EXAMPLE.KEY', 'IGNORED.KEY.(.*)' ], // can be string or RegExp
customRegExpToFindKeys: [ "(?<=marker\\(['\"])([A-Za-z0-9_\\-.]+)(?=['\"]\\))" ] // to find: marker('TRSNLATE.KEY');
};
const reactI18nLint = new ReactI18nextLint(viewsPath, languagesPath, ignoredLanguagesPath, ruleConfig)
const resultLint: ResultCliModel = reactI18nLint.lint(); // Run Lint
const languages: LanguagesModel[] = reactI18nLint.getLanguages() // Get Languages with all keys and views
`
#### NOTE!
If you have error Can't resolve 'fs' in .... Please add next setting to you project:
- tsconfig.json
`json
{
"skipLibCheck": true
}
``