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'
First, let's change the default locale to ja
in config/application.rb
:
# config/application.rb
config.i18n.default_locale = :ja
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}')]
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
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: 更新日
# 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:
# 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
.
# 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"
Top comments (3)
I love how you explain the articles, easy and with good images. The only thing I don't understand very well is Japanese 😅😂
Thank you for reading! Japanese might be a little more difficult than Ruby, haha
"Might be a little" ?? haha ... OK! You will teach me hahaha