Skip to content

Localization using I18n

d-schlereth edited this page Feb 6, 2022 · 1 revision

Localization with I18n

There is currently support for the following languages:

  • English en
  • German de

Localization files

The localization files for each language are stored under config/locales as a YAML file named after the language code (i.e. de.yml for German, en.yml for English). The YAML files contain key-value pairs denoting a translation key and a string containing the actual translation. For example:

en:
    hello: 'Hello world'
    people: 'People'
    title-en: 'English'
    title-de: 'German'

All keys should be defined for all available languages. The YAML files can be organized to use specific scopes as shown in the I18n Guide or several YAML files can be used with a corresponding folder structure, see Guide

Server side (ruby)

We are using the I18n gem for server side localization.

See here for a guide on how to add translations to your code.

To translate a string use the t helper method in place of the string:

<h1><%= t('hello') %></h1>

The key ('hello') must be defined in the localization files.

You can get the current locale via I18n.locale.

Client side (javascript)

In the frontend we are using the i18njs and/or i18n library.

To translate a string, use the I18n.t method in the JavaScript code in place of a string:

I18n.t('hello')

The key ('hello') must be defined in the localization files.

You can get the current locale via I18n.locale.

Implementation

The current locale (en or de) is saved in the url query via the locale parameter. By default the english locale is used. All relative links should preserve this locale when using the link_to rails helper to create links. The locale can be added to the end of the url by hand to apply the corresponding locale.