Internationalize your Rails app with Ruby I18n gem
Tags: rails, webdev, i18n
Published: 2021-03-15 02:36:45

![Designed by pch.vector / Freepik](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s8nqep1bdf1vjxa9f7od.jpg)<figcaption>Designed by pch.vector / Freepik</figcaption>

If you plan to create a Rails application in languages other than English, let Rails help you with the translation. [Ruby I18n gem](https://github.com/ruby-i18n/i18n) provides an easy-to-use framework for translating your application to a single custom language or for providing multi-language support in your application so that you can focus on the development. 

In this article, I will walk you through its installation and configuration, as well as demonstrate its usage.

***
##Installation and configuration##
Let's configure Japanese translations in our application.

We start with adding `i18n` to your `Gemfile`:
```ruby
# Gemfile

gem 'i18n'

```

First, let's change the default locale to `ja` in `config/application.rb`:
```ruby
# config/application.rb

config.i18n.default_locale = :ja

```
The locale key for Japanese is `:ja`. For other languages, check [ISO 639-1 codes](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes).

If you would like to use a different location for your translations, you will need to add load path in `config/application.rb`:
```ruby
# config/application.rb

config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}')]

```

Now we need to store our translations in `config/locales/` directory.

You can use YAML (.yml) or plain Ruby (.rb) files, but YAML is preferred among developers. It would be hard to manage if you put all the translations in one file. Instead, it's better to separate models from views and organize files in a hierarchy.

For example:

```ruby
config
└── locales
    ├── model.ja.yml
    └── views
        ├── admin
        │   ├── user_sessions
        │   │   └── ja.yml
        │   └── users
        │       └── ja.yml
        ├── shared
        │   └── ja.yml
        ├── user_sessions
        │   └── ja.yml
        └── users
            └── ja.yml
```

Now it is time to do some translation:
```ruby
# config/locales/model.ja.yml

ja:
  activerecord:
    models: # translation for model name
      user: ユーザー 
    attributes: # translation for attributes of each model
        user: # translation for each attribute of User model
          id: ID
          first_name: 名前
          last_name: 姓
          email: メールアドレス
          password: パスワード
  attributes: # translation for attributes that are common for all the models
    created_at: 作成日
    updated_at: 更新日
```

```ruby
# config/locales/views/users/ja.yml

ja:
  users:
    index:
      title: 'ユーザ一覧'
    show:
      title: '%{user_name}さんのユーザー情報' # You can send parameters to interpolation.
    edit:
      title: '%{user_name}さんのユーザー情報を編集'
```
***
##Usage##
Rails provides convenient helper methods to look up the locale inside views.

###Views###
We can look up the `users.index.title` value inside `app/views/users/index.html.erb` template like this:
```erb
# app/views/users/index.html.erb

<%= t '.title' %>

# This is equivalent without automatic translation scoping
<%= t 'users.index.title' %>
```
###Models###
For models, there are helper methods: `model_name.human` and `human_attribute_name`.
```erb
# app/views/users/index.html.erb

# This will return activerecord.models.user, "ユーザー"
<li><%= User.model_name.human %></li>

# This will return activerecord.attributes.user.id, "ID"
<li><%= User.human_attribute_name(:id) %></li>

# This will return activerecord.attributes.user.email, "メールアドレス"
<li><%= User.human_attribute_name(:email) %></li>
```
***
The strength of Ruby/Rails is that there are a lot of gems provided by the community that do heavy lifting for you, and I18n is one of them.

Reference:
[Rails Guides "Rails Internationalization (I18n) API"](https://guides.rubyonrails.org/i18n.html)