Skip to content

Latest commit

 

History

History
79 lines (63 loc) · 1.51 KB

i18n.md

File metadata and controls

79 lines (63 loc) · 1.51 KB

Docs / Configuration / i18n.js

3.6. 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?"
}

Usage

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' })
    })
  }
}

GET /message/say?language=en

{
  "messageA": "hello world",
  "messageB": "hello Trails.js! What's up?"
}

GET /message/say?language=de

{
  "messageA": "hallo Welt",
  "messageB": "hallo Trails.js! Wie geht's?"
}