Skip to content

Internationalization (i18n)

mde edited this page Oct 7, 2012 · 3 revisions

Geddy provides internationalization support out of the box, with localized error messages in the following languages:

  • Chinese (Simplified)
  • English (US)
  • German (Germany)
  • Japanese (Japan)
  • Portuguese (Brazil)
  • Spanish (Spain)

You can set a specific locale at the request level, or a default one for your entire app. The default locale if you don't set one is American English ('en-us').

Setting a locale for your app

Set a locale in your environment.js by setting the defaultLocale property on the i18n config option:

...
, i18n: {
    defaultLocale: 'ja-jp'
  }
...

Loading i18n data

Geddy will load i18n data according to the loadPaths property of your app's i18n config -- an array of paths to look in for JSON files containing i18n data. By default, it contains an entry for 'config/locales' in your app.

Geddy's i18n code loads the data into data structures based on the filename. (So the data in /foo/bar/en-uk.json gets loaded into the en-uk locale, etc.)

Data format

Internationalization data is loaded from JSON files in the following format:

{
  "model.validatesPresent": "「{name}」の入力が必要です。"
, "model.validatesAbsent": "「{name}」の入力は不要です。"
, "model.validatesConfirmed": "「{name}」と「{qual}」が一致しません。"
, "model.validatesFormat": "「{name}」のフォーマットが正しくありません。"
, "model.validatesExactLength": "「{name}」は{qual}文字でなければいけません。"
, "model.validatesMinLength": "「{name}」は{min}文字以上でなければいけません。"
, "model.validatesMaxLength": "「{name}」は{max}文字以内でなければいけません。"
, "model.validatesWithFunction": "「{name}」は有効ではありません。"
}

Using i18n text

Geddy controllers have an i18n property which has a getText method (shortcut alias t). Pass this method the desired key, and an optional data-object for parametric replacement, and an optional locale. If you don't pass a locale, it will fall back to the app's defaultLocale.

This i18n object is also available as a local variable in your templates.

Use it like this in a controller (assuming a defaultLocale of 'ja-jp'):

// Returns 「HOWDY」の入力が必要です。
this.i18n.t('model.validatesPresent', {name: 'HOWDY'});

// Returns "HOWDY" ist ein Pflichtfeld.
this.i18n.t('model.validatesPresent', {name: 'HOWDY'}, 'de-de');