Note that I (@valscion) don't currently use this plugin, so this is unsupported. If you'd like to start using this and maintain it, feel free to open an issue and share your motivation.
Babel plugin to statically compile keypath denoted translation calls to literals.
Compiles these...
const myString = __("my.translation.path");
const anEnum = __obj("example.countries");
const lyrics = __arr("song");
const veryComplex = __dump("complex");
...with this kind of translation...
{
"my": {
"translation": {
"path": "Hei, maailma!"
}
},
"example": {
"countries": {
"finland": "Suomi",
"sweden": "Ruotsi"
}
},
"song": [
"Hey!",
"Ho!",
"Let's go!"
],
"complex": [[["multilevel array"], 123], null]
}
...into this:
const myString = "Hei, maailma!";
const anEnum = {
finland: "Suomi",
sweden: "Ruotsi"
};
const lyrics = ["Hey!", "Ho!", "Let's go!"];
const complex = [[["multilevel array"], 123], null];
$ npm install babel-plugin-i18n --save-dev
.babelrc
{
"plugins": ["i18n", {
"translationLoader": "./path/to/translationLoader"
}]
}
TODO: How does one actually pass options via CLI?
require("babel-core").transform("code", {
plugins: ["i18n", {
"translationLoader": "./path/to/translationLoader"
}]
});
const join = require('path').join;
const readFileSync = require('fs').readFileSync;
const TRANSLATIONS_PATH = join(__dirname, 'completeTranslations.json');
module.exports = function myTranslationLoader() {
const translationJSON = readFileSync(TRANSLATIONS_PATH, 'utf8');
return JSON.parse(translationJSON);
};