Docs / Configuration / i18n.js
Setup internationalization, define multi-language mappings, etc. If your app will touch people all over the world, i18n (or internationalization) will be an important part of your application design. By default, trailpack-i18n uses the i18next npm module.
// config/i18n.js
module.exports = {
lng: 'en',
resources: {
en: require('./locales/en'),
de: require('./locales/de')
}
}
// config/locales/en.json
{
"hello": {
"world": "hello world"
},
"customHello": "hello {{name}}! What's up?"
}
// config/locales/de.json
{
"hello": {
"world": "hallo Welt"
},
"customHello": "hallo {{name}}! Wie geht's?"
}
The i18n translate method this.__
is available in Controllers, Policies, and Services.
// api/controller/MessageController.js
module.exports = class MessageController extends Controller {
/**
* Return a message in the specified language
*/
say (request, reply) {
const { lng } = request.query.language
reply({
messageA: this.__('hello.world', { lng }),
messageB: this.__('customHello', { lng, name: 'Trails.js' })
})
}
}
{
"messageA": "hello world",
"messageB": "hello Trails.js! What's up?"
}
{
"messageA": "hallo Welt",
"messageB": "hallo Trails.js! Wie geht's?"
}