A Babel transform plugin to replace strings with their translations.
.babelrc
{
"plugins": [
["transform-i18n", {
"dictionary": {
"Hello": "Bonjour",
"Hello, {name}!": "Bonjour, {name}!"
}
}]
]
}
In
const name = 'Brad';
const hello = t('Hello');
const helloWithName = t('Hello, {name}!', {
name
})
Out
const name = 'Brad';
const hello = 'Bonjour';
const helloWithName = 'Bonjour, ' + name + '!';
npm install babel-plugin-transform-i18n
{
"plugins": [
["transform-i18n", {
"functionName": "t",
"dictionary": {}
}]
]
}
require('babel-core').transform('code', {
plugins: [
['transform-i18n', {
functionName: 't',
dictionary: {}
}]
]
});
There are two options available, both are optional:
A mapping of the strings passed to the translation function to their translated versions. If no dictionary is passed, calls to the translation function will be replaced with the original string.
The name of the function that wraps the strings. Defaults to t
.