Skip to content

Internationalization guide

Luke edited this page Nov 30, 2022 · 5 revisions

Internationalization

We use I18n to make our service available in German and English. The locale is determined in ApplicationController from the browser preferences and defaults to en.

Using translations

Most internationalization looks something like this:

<h1><%= t 'hello_world' %></h1>

In English this should be

<h1>Hello, World!</h1>

and in German

<h1>Hallo, Welt!</h1>

t is a helper for the function I18n.t or I18n.translate in views. 'hello_world' is a unique identifier that represents the text with both translations.

These translations are defined in config/locales as YAML files like so:

en.yml:

en:
  hello_world: 'Hello, World!'

de.yml:

de:
  hello_world: 'Hallo, Welt!'

Translating with interpolation

I18n supports string interpolation with named variables:

<h1><%= t('greeting', name: @first_name) </h1>
en:
  greeting: 'Hello, %{name}!'
de:
  greeting: 'Hallo, %{name}!'

There is special support for Pluralization

Naming conventions

The YAML files allow for hierarchical identifiers like 'views.landing_page.title'.

Our locales should be build similar to this example:

de:
  # 'defaults' for all terms that are unspecific to any concept 
  defaults:
    back: "Zurück"
    delete: "Löschen"
    ...
  # views names for terms that are inherent to them
  views:
    landing_page:
      title: "Bookkeeper Blau"
      read_more: "Mehr lesen"
      ...
  # Properties of models are independent of their views
  models:
    item:
      # use the model name again to refer to the model itself
      item: "Medium"
      # the keys should match the property names
      name: "Name"
      category: "Kategorie"
      description: "Beschreibung"
      location: "Ort"
      ...
  ...
Clone this wiki locally