Use Gem'rails-i18n'to support Japanese. Make a note of the clogged part when introducing this Gem.
・ Ruby 2.6.4 ・ Rails 5.2.3
Rails Internationalization (i18n) API
Japanese localization by Rails i18n
[I18n :: InvalidLocaleData Locale file cannot be translated] (https://qiita.com/niwa1903/items/10154b8cbe1cd0416495)
Gemfile.
gem 'rails-i18n', '~> 5.1'
$ bundle install
This is the usual flow when installing Rails Gem.
config/application.rb
module App
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2
#2 lines added:The default locale is Japanese(:ja)To.
config.i18n.default_locale = :ja
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]
end
end
Add two lines in the module. Without this description, it seems that even if you install Gem, it will not support Japanese.
config/locales/activerecord/ja.yml
ja:
activerecord:
models:
user: 'user'
board: 'Bulletin board'
attributes:
user:
email: 'mail address'
password: 'password'
password_confirmation: 'Password confirmation'
last_name: 'Surname'
first_name: 'Name'
To make it correspond to the entire model, create a hierarchy of models and When associating column names, create a hierarchy of attributes.
config/locales/view/ja.yml
ja:
users:
new:
title: 'user registration'
to_login_page: 'Go to login page'
create:
success: 'User registration is complete'
fail: 'User registration failed'
By describing it in view/ja.yml, it is also applied to controller. However, if you do not create a hierarchical structure with exactly two indents, Japanese will not be applied.
For how to write ja.yml, the following Rails guide article will be helpful. Lazy lookup
ja:
books:
index:
title: "Hogehoge"
With the above hierarchical structure, you can access the books.index.title value inside the app/views/books/index.html.erb view template as follows:
<%= t '.title' %>Or<%= t ('.title') %>
If you write it in full, it will be as follows.
<%= t 'books.index.title' %>
Recommended Posts