DEV Community

Junko T.
Junko T.

Posted on • Edited on

Internationalize your Rails app with Ruby I18n gem

Designed by pch.vector / Freepik
Designed by pch.vector / Freepik

If you plan to create a Rails application in languages other than English, let Rails help you with the translation. Ruby I18n gem 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:

# Gemfile

gem 'i18n'

Enter fullscreen mode Exit fullscreen mode

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

# config/application.rb

config.i18n.default_locale = :ja

Enter fullscreen mode Exit fullscreen mode

The locale key for Japanese is :ja. For other languages, check 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:

# config/application.rb

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

Enter fullscreen mode Exit fullscreen mode

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:

config
└── locales
    ├── model.ja.yml
    └── views
        ├── admin
           ├── user_sessions
              └── ja.yml
           └── users
               └── ja.yml
        ├── shared
           └── ja.yml
        ├── user_sessions
           └── ja.yml
        └── users
            └── ja.yml
Enter fullscreen mode Exit fullscreen mode

Now it is time to do some translation:

# 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: 更新日
Enter fullscreen mode Exit fullscreen mode
# config/locales/views/users/ja.yml

ja:
  users:
    index:
      title: 'ユーザ一覧'
    show:
      title: '%{user_name}さんのユーザー情報' # You can send parameters to interpolation.
    edit:
      title: '%{user_name}さんのユーザー情報を編集'
Enter fullscreen mode Exit fullscreen mode

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:

# app/views/users/index.html.erb

<%= t '.title' %>

# This is equivalent without automatic translation scoping
<%= t 'users.index.title' %>
Enter fullscreen mode Exit fullscreen mode

Models

For models, there are helper methods: model_name.human and human_attribute_name.

# 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>
Enter fullscreen mode Exit fullscreen mode

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"

Top comments (3)

Collapse
 
sturpin profile image
Sergio Turpín

I love how you explain the articles, easy and with good images. The only thing I don't understand very well is Japanese 😅😂

Collapse
 
junko911 profile image
Junko T.

Thank you for reading! Japanese might be a little more difficult than Ruby, haha

Collapse
 
sturpin profile image
Sergio Turpín

"Might be a little" ?? haha ... OK! You will teach me hahaha