helpwaves package for internationalization that creates localized and typesafe translation based on ARB files.
Create a .arb file with your translations:
{
"priceInfo": "The price is {price}{currency, select, usd{$USD} eur{€} other{}}.",
"@priceInfo": {
"placeholders": {
"price": {
"type": "number"
},
"currency": {}
}
}
}And get a translation:
import {combineTranslation} from "./combineTranslation";
translations["en-US"].priceInfo(price, currency)
const t = combineTranslation([translation1, translation2], "en-US")
// v still typesafe on both function parameters
t("priceInfo", { price, currency })npm install -D @helpwave/internationalization
The file structure could look like this:
/locales
-> de-DE.arb
-> en-US.arb
/locales/time
-> de-DE.arb
-> en-US.arb
Then run:
npx build-intl By default ./locales will be translated to ./i18n, the input directory and output file are configurable with:
Usage: i18n-compile [options]
Options:
-i, --in <dir> Input directory containing .arb files
-o, --out <file> Output file (e.g. ./i18n/translations.ts)
-f, --force Overwrite output without prompt
-h, --help Show this help message
`)
The lexer, parser and compiler are all tested with jest, see our tests
Example translation files and the resulting translation can be found in the examples folder.
Rebuild the examples:
npm run build
node dist/scripts/compile-arb.js --force -i ./examples/locales -o ./examples/translations/translations.ts -n "exampleTranslation"