Docs / Configuration / i18n.ts
3.6. i18n.ts
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, spool-i18n uses the i18next npm module.
// config/i18n.ts
export const i18n = {
lng: 'en',
resources: {
en: require('./locales/en'),
de: require('./locales/de')
}
}
// config/locales/en.json
{
"hello": {
"world": "hello world"
},
"customHello": "hello ! What's up?"
}
// config/locales/de.json
{
"hello": {
"world": "hallo Welt"
},
"customHello": "hallo ! Wie geht's?"
}
Usage
The i18n translate method this.__ is available in Controllers, Policies, and Services.
// api/controller/MessageController.ts
export 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: 'fabrix' })
})
}
}
GET /message/say?language=en
{
"messageA": "hello world",
"messageB": "hello fabrix! What's up?"
}
GET /message/say?language=de
{
"messageA": "hallo Welt",
"messageB": "hallo fabrix! Wie geht's?"
}