Wilster-trans
It's always bugged me how difficult translations in applications ends up being, so this is an attempt on making that task as simple as possible. It's based on some really nice projects e.g. node-polyglot.
The only thing I would have like to do better, is the flexibility of extraction - currently you are required to use import Trans from 'wilster-trans'
syntax - due to my poor regex skills..
nevertheless, it works :)
Installation
npm i --save wilster-trans
and for CLI command npm i -g wilster-trans
Getting started
- Import the repo (
import Trans from 'wilster-trans'
) - Run the
Trans.init({config: {...}, locales: {...})
(see example) - Start using it (
Trans.t("foo")'
). NB:Trans
is the mandatory name for the extractor to work. - Run
wilster-trans run -s PATH_TO_SRC -o PATH_TO_OUTPUT_DIR -l COMMA_SEPARATED_LOCALES
. This will extract all the translation keys and put them into your translation directory.
NB: If you want to run the extractor from the local scope, just call ./node_modules/wilster-trans/bin/wilster-trans.js run
instead.
Use example
./src/application.js
// Init's the translatorTrans extends Component { return <div> <h1>Trans</h1> <p>Trans</p> </div> }
Then run wilster-trans run -s ./src -o ./src/translations -l en,da
./src/translations/en.json
CLI Command(s)
wilster-trans run -s PATH_TO_SRC -o PATH_TO_OUTPUT_DIR -l COMMA_SEPARATED_LOCALES
Runs the extractor.
-o || --output is the output directory.
-s || --source is the code source directory (where we'll look for "Trans.t" matches)
-l || --locales is the locales that will be created.
Documentation
-
Trans.t(translationKey: String, params: Object)
If you want to use placeholders, you should use the following syntaxHi there %name%!
in the translation string, and reference it using in theparams
object e.g.Trans.t("...", {name: "John Doe"})
-
Trans.setLocale('LOCALE_KEY') Run this method if you want to change from the current language. Then re-render your UI and it'll use the newly set locale.