babel-plugin-i18n
Multi language builds with Babel. Setup a different build for each language.
API
i18n(text: string, language?: string) : string
-
text
original text. -
language
(optional) overrides the language set by the plugin.
Setup
npm install --save-dev @joaofranciscosantos/babel-plugin-i18n
and add to .babelrc.js
:
module.exports = api => ({
plugins: [["@joaofranciscosantos/babel-plugin-i18n"]]
});
to override options:
module.exports = api => ({
plugins: [["@joaofranciscosantos/babel-plugin-i18n", {
source: ["translations.json", "translations/canada.json", "yo.json"],
target: "i18n",
language: "pt"
}]]
});
-
source
(array) the path with translations files. They will be all merged during resolution. Defaults to.dictionary.json
. -
target
(string) function that will apply the translation. Defaults toi18n
. -
language
(string) set the translation language. Defaults toen
.
Examples
-
i18n("dog", "es")
transpiles to"perro"
-
i18n("dog")
transpiles to"doggy"
-
i18n("none", "?")
transpiles to"none"
(returns the original text because no translation was provided)
and translations files must have a similar structure:
{
"keyword": {
"language": "translation"
},
"dog": {
"en": "doggy",
"pt": "cão",
"es": "perro",
"it": "cane"
}
}
FAQ
ReferenceError: i18n is not defined
Check your build folder. You should not see the 'i18n' function. Review your babel configuration.
TS2304: Cannot find name 'i18n'
You should declare var i18n: any;