Multilingual

All the languages files are in the config/locales/ folder.

There should be a sample file named en.yml there. All the language files use the YAML format.

Take the “Home” wording as example. We can put the following code in the en.yml file and then access it again with the translate method, or t as alias.

1en:
2  home: "Home"

Using the locale in view:

1<li><%= link_to translate('home'), root_path %></li>

Or we can have better organization by grouping the locales. This ensures we do not create conflicts with the same keys. Now in the en.yml file.

1en:
2  nav:
3    home: "Home"

And the view becomes:

1<li><%= link_to translate('nav.home'), root_path %></li>

By grouping the locales, it ensures we do not create conflicts with the same keys.

And typing the translate() method every time can be annoying. That’s why rails comes with an alias t():

1<li><%= link_to t('nav.home'), root_path %></li>

Common Locales Setting

By default, the locale files comes with empty setting (Ignoring that hello world example). If you need some common local settings such as date and time. You can find it in this repo.

For date/time locals, please use `localize` method, or `l`, instead of the `translate`.

What’s next? We’re going to take a look at “Adding chinese”.

overlaied image when clicked on thumbnail

Makzan | Ruby on rails 101 | Table of Content