i18n [@since 2024.04.2]
i18n bundles are stored in the /i18n directory using a JSON format. The name of the file decides the language.
A RESTApp inherits locale from the context, i.e the locale of the website.
Bundle
// en.json
{
"settings": "Settings",
"welcomeTo": "Welcome to {0}!",
"bye": "Bye!"
}
// sv.json
{
"settings": "Inställningar",
"welcomeTo": "Välkommen till {0}!",
"bye": "Hejdå!"
}
Retrieving the translations
Using i18n
to retrieve the localized value.
i18n.get(key [, varSubstitution...])
const i18n = require('i18n');
const logWelcome = () => {
console.log(i18n.get('welcomeTo', 'Sitevision'));
// Welcome to Sitevision!
// - or -
// Välkommen till Sitevision!
};
const logBye = () => {
console.log(i18n.get('bye'));
// Bye!
// - or -
// Hejdå!
};
i18n.forLocale(language)
Retrieves a new i18n instance for a specific locale/language.
Note! Using a specific locale will also affect the "raw" i18n behaviour. Use either the raw i18n or a i18n with a specific locale. Do not mix the raw and specific ones.
const i18n = require('i18n');
const logSwedishWelcomeAndGoodbye = () => {
const svI18n = i18n.forLocale('sv');
console.log(svI18n.get('welcomeTo', 'Sitevision'));
// Välkommen till Sitevision!
console.log(svI18n.get('bye'));
// Hejdå!
};