Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Internationalization

Vera Clemens edited this page Jun 5, 2018 · 5 revisions

Internationalization refers to translation of text elements as well as localization of numeric, date and time formats.

Translating text elements in views

Text elements in views should never be hardcoded in one language. Instead, use the Rails Internationalization (I18n) API so the text is displayed in the correct language according to the user's preference.

Before: <h1>Job Offers</h1>

After: <h1><%= t("job_offers.headline") %></h1>

The correct translations for the text element need to be supplied. In this case, they should be added to the following two files: config/locales/views/job_offers/en.yml and config/locales/views/job_offers/en.yml.

Follow the following structure:

en:
  job_offers:
    headline: "Job Offers"

de:
  job_offers:
    headline: "Stellenangebote"

Translating model attributes

For each model a de.yml and en.yml file must be created in config/locales/models/, where is the name of the model in singular form. The files should contain all the model attributes with their corresponding translations.

Before: <%= label_tag :chair %>

After : <%= label_tag t("activerecord.attributes.job_offer.chair") %>

Translating basic bootstrap elements

The two files in the /config/locales/defaults folder supply translations for simple element names like "Edit", "New", etc.

Internationalizing dates

If a date is displayed, it is possible to internationalize the format of the date by doing this:

Before: <%= job_offer.start_date %>
After : <%= l(job_offer.start_date) %>