Skip to content

Latest commit

 

History

History
65 lines (41 loc) · 1.92 KB

File metadata and controls

65 lines (41 loc) · 1.92 KB

Internationalization

We use Rails' built-in internationalization (i18n) to support multiple languages, read the general documentation here.

To create new locale files for a model and/or its views, you can run:

make locale MODEL=MyModel

Organization of locale files

Placing translations for all parts of an application in one file per locale can become hard to manage. We've chosen to organize our translations into multiple YAML files, using the following hierarchy

  • defaults - fallback errors, date, and time formats
  • models - model and attribute names
  • views - strings specifically rendered in views, partials, or layouts

Caution

Be aware that YAML interprets the following case-insensitive strings as booleans: true, false, on, off, yes, no. Therefore, these strings must be quoted to be interpreted as strings. For example: "yes": yup and enabled: "ON"

Routes and links

The active locale is based on the URL. For example, to use the Spanish locale, visit: /es-US.

When adding a new route, ensure it's nested within the localized block so that it can be viewed at the appropriate locale.

To ensure links preserve the locale, it's important for them to include the locale param, which happens automatically when using link_to or url_for helpers:

<%= link_to "Apply", controller: "claims", action: "new" %>

Rendering content

To render content, use I18n.t:

I18n.t("hello")

In views, this is aliased to just t:

<%= t("hello") %>

Dates

To format dates, use the custom local_time helper:

<%= local_time(foo.created_at) %>

To include the time, use the :format option:

<%= local_time(foo.created_at, format: :long) %>

Adding a new language

  1. Add new YAML file(s) in config/locales
  2. Update available_locales in config/application.rb to include the new locale