Simple and efficient i18n for babel and babel-plugin-macros. It replaces strings at build time.
Input
t`Hello ${name}!`;
t`Hello ${name}, welcome to ${place}!`;
t("default")`Hello ${name}!`;
t("informal")`Hello ${name}!`;
Output
`Salut ${name}!`;
`Bine ai venit la ${place}, ${name}}! ${name}, ma bucur ca esti aici!`;
`Salut ${name}!`;
`Yo ${name}!`;
Multiple expressions in template literals are allowed. Translations can contain repeated parameters.
const translations = {
// Without context
"Welcome to {place}, {name}!": "Salut {name}, bine ai venit la {place}!",
// With context
"Hello {name}, welcome to {place}!": {
default: "Salut {name}, bine ai venit la {place}!",
home: "Waaazzaaaa {name}, bine te-am gasit bro' la {place}!"
},
// Repeated parameters
"Offer available from {min} {currency} to {max} {currency}":
"Oferta maxima este de {max} {currency}. Oferta minima este in moneda {currency}, iar valorea este {min}. Deci de la {min} la {max} {currency} pentru dummies.",
// With strings resembling parameters
"Hello {country}": "Salutare {Budapest}, pardon, {country}"
};
module.exports = translations;
If using ESLint, make sure to register "t" as a global variable in your config file.
"globals": {
"t": true
}
Example using a .babelrc file:
{
"plugins": [
["yate", {
"translationsFile": "./translations.js",
"tagName": "t"
}]
]
}
There are two options available, both are optional:
translationsFile
Path to a module that exports a translations object. defaults to "./translations.js"
tagName
The name of the tag expression that wraps the template literals. defaults to "t"
Translation file path is ./translations.js
You can rename the translation function as you need.
Example:
import t from "babel-plugin-yate/macro";
const App = () => <span>{t`String to be translated`}</span>;