What is i18next-parser?
i18next-parser is a tool that extracts translation keys from your source code to JSON, allowing you to manage and maintain your internationalization (i18n) files more efficiently. It supports various file formats and can be integrated into your build process.
What are i18next-parser's main functionalities?
Extract Translation Keys
This feature allows you to extract translation keys from your source code files and generate JSON files for each locale. The code sample demonstrates how to use i18next-parser to parse JavaScript files in the 'src' directory and output the translations to 'locales/en/translation.json' and 'locales/fr/translation.json'.
const parser = require('i18next-parser');
const fs = require('fs');
const options = {
locales: ['en', 'fr'],
output: 'locales/$LOCALE/$NAMESPACE.json'
};
parser.parseFiles('src/**/*.js', options, (err, translations) => {
if (err) throw err;
fs.writeFileSync('locales/en/translation.json', JSON.stringify(translations.en, null, 2));
fs.writeFileSync('locales/fr/translation.json', JSON.stringify(translations.fr, null, 2));
});
Support for Multiple File Formats
i18next-parser supports multiple file formats including JavaScript, JSX, TypeScript, and TSX. The code sample shows how to configure the parser to handle these different file types and extract translations from them.
const parser = require('i18next-parser');
const options = {
input: ['src/**/*.js', 'src/**/*.jsx', 'src/**/*.ts', 'src/**/*.tsx'],
output: 'locales/$LOCALE/$NAMESPACE.json'
};
parser.parseFiles(options.input, options, (err, translations) => {
if (err) throw err;
console.log('Translations extracted successfully');
});
Customizable Output
You can customize the output path and separators for namespaces and keys. The code sample demonstrates how to set a custom output path and define custom separators for namespaces and keys.
const parser = require('i18next-parser');
const options = {
locales: ['en', 'de'],
output: 'custom_locales/$LOCALE/$NAMESPACE.json',
namespaceSeparator: ':',
keySeparator: '.'
};
parser.parseFiles('src/**/*.js', options, (err, translations) => {
if (err) throw err;
console.log('Custom output path and separators applied');
});
Other packages similar to i18next-parser
babel-plugin-i18next-extract
babel-plugin-i18next-extract is a Babel plugin that extracts translation keys from your code and generates JSON files for i18next. It integrates directly with Babel, making it a good choice if you are already using Babel in your project. Compared to i18next-parser, it offers a more seamless integration with the Babel build process but may require more setup if you are not using Babel.
react-intl-translations-manager
react-intl-translations-manager is a tool for managing translations in projects using react-intl. It extracts messages from your React components and generates translation files. While it is specifically designed for use with react-intl, it offers similar functionality to i18next-parser in terms of extracting and managing translation keys. However, it is less flexible in terms of file format support and integration with non-React projects.
i18next Parser
A simple command line and gulp plugin that lets you parse your code and extract the translations keys in it.
Features
- Is a stream transform (so it works with gulp)
- Parses a single file or a directory (recursively or not)
- Creates one json file per locale and per namespace.
- Remove old keys your code doesn't use anymore and place them in a
namespace_old.json
file. It is usefull to avoid losing translations you may want to reuse.
Installation
npm install i18next-parser -g
Tests
mocha --reporter nyan test.js
Gulp Usage
var i18next = require('i18next-parser');
gulp.task('i18next', function() {
gulp.src('app/**')
.pipe(i18next({locales: ['en', 'de'], functions: ['__', '_e']}))
.pipe(gulp.dest('locales'));
});
- functions: An array of functions names to parse. Defaults to
['t']
- namespace: Default namespace in i18next. Defaults to
translation
- locales: An array of the locales in your applications. Defaults to
['en','fr']
- regex: A custom regex for the parser to use.
CLI Usage
i18next /path/to/file/or/dir [-orpfnl]
- -o, --output : Where to write the locale files.
- -r, --recursive: Is --output is a directory, parses files in sub directories.
- -f, --function : Function names to parse. Defaults to
t
- -p, --parser : A custom regex for the parser to use.
- -n, --namespace : Default namespace in i18next. Defaults to
translation
- -l, --locales : The locales in your applications. Defaults to
en,fr
Exemples
Parse single file or directory
i18next /path/to/file/or/dir
It will create the following files in the directory from which you run the command:
locales/en/translation.json
locales/en/namespace1.json
locales/en/translation_old.json
locales/en/namespace1_old.json
locales/fr/translation.json
locales/fr/namespace1.json
locales/fr/translation_old.json
locales/fr/namespace1_old.json
...
Change the output directory
i18next /path/to/file/or/dir -o /output/directory
It will create the file in the specified folder:
/output/directory/en/translation.json
...
Change the locales
i18next /path/to/file/or/dir -l en,de,sp
This will create a directory per locale in the output folder:
locales/en/...
locales/de/...
locales/sp/...
Change the default namespace
i18next /path/to/file/or/dir -n my_default_namespace
This will add all the translation from the default namespace in the following file:
locales/en/my_default_namespace.json
...
Filter files and folders
i18next /path/to/file/or/dir -filterFolder *.hbs,*.js -filterFolder !.git
In recursive mode, it will parse *.hbs
and *.js
files and skip .git
folder. This options is passed to readdirp. To learn more, read their documentation.
Change the translation functions
i18next /path/to/file/or/dir -f __,_e
This will parse any of the following function calls in your code and extract the key:
__('key'
__ 'key'
__("key"
__ "key"
_e('key'
_e 'key'
_e("key"
_e "key"
Note1: we don't match the closing parenthesis as you may want to pass arguments to your translation function.
Note2: the parser is smart about escaped single or double quotes you may have in your key.
Change the regex
i18next /path/to/file/or/dir -r "(.*)"
You must pass the regex as a string. That means that you will have to properly escape it.
The regex used by default is:
/[^a-zA-Z0-9](?:(?:t)|(?:i18n\.t))(?:\(|\s)\s*(?:(?:'((?:(?:\\')?[^']+)+[^\\])')|(?:"((?:(?:\\")?[^"]+)+[^\\])"))/g
Next
- If a new translation key is added check if a translation is available in the old file