In order to use built in translation extractor you can run:
yarn translations:extract
The script will auto extract all translation keys wrapped inside translate
, translateRaw
and translateMarker
functions.
It will merge new keys, without modifying old ones and sort them alphabetically.
It will update all *.json files inside src/translations/lang/*.json
, meaning all languages will be updated with new keys.
To add new translation key, just wrap the key inside translate
, translateRaw
or translateMarker
functions.
And run yarn translations:extract
, to update the *.json files. Find the key in *.json file,
and insert the value(Do this at least for English language(en.json
)).
- Correct:
translateRaw('SUCCESS_MESSAGE')
- Incorrect:
translateRaw('successMessage')
- Correct:
translateRaw('SUCCESS_0')
ORtranslateRaw('0_SUCCESS')
- Incorrect:
translateRaw('0')
ORtranslateRaw('123456789')
- Correct, use map function:
const mapTranslationKey = (message: string) => {
switch(message) {
case 'success':
return translateMarker('SUCCESS_MESSAGE');
case 'error':
default:
return translateMarker('ERROR_MESSAGE');
}
};
translateRaw(mapTranslationKey(message));
- Incorrect:
translateRaw(message)
, where message is a variable
If all translation keys were added via this script, just remove all merging conflicts tag <<<<<<<
,
=======
and >>>>>>>
so that the .json
file is valid. And rerun yarn translations:extract
.
To add a new translation create a file <language-name>.json
and use the following boilerplate:
{
"code": "<language-name>",
"data": {
}
}