Public Lab Wiki documentation



Internationalization | Tutorial

This is a revision from August 23, 2016 15:52. View all revisions
2 | 9 | | #13380

Internationalization or "I18n"

Internationalization Basics

i18n—short for "internationalization." (There are 18 letters between the "i" and the "n" in "internationalization.") Internationalization means architecting and developing an application in order to bring support for a second locale—possibly at a future time.

The default tool for internationalizing a rails application is with the ruby i18n gem.

Basically, the i18n gem creates a directory called config/locales. Rails then stores the application's various languages in a yaml file, such as en.yml for English or fr.yml for French.

The I18n gem ships with a feature called variable interpolation that allows you to use variables in translation definitions and pass the values for these variables to the translation method.

Example en.yml file:

en:
  hello_world: "Hello world"
  hello: "Hello %{name}"

Example fr.yml file:

fr:
  hello_world: "Bonjour le monde"
  hello: "Bonjour %{name}"

Possible ERB code:

<p>
   <%= I18n.translate("hello_world", :locale => "en") %>
</p>
<p>
   <%= I18n.translate("hello", :locale => "en", :name => "James") %>
</p>
 <p>
    <%= I18n.translate("hello_world", :locale => "fr") %>
 </p>
 <p>
    <%= I18n.translate("hello", :locale => "fr", :name => "James") %>
</p>

The output would be:

Hello world
Hello James
Bonjour le monde
Bonjour James