[RUBY] [Rails] How to use Gem'rails-i18n' for Japanese support


Use Gem'rails-i18n'to support Japanese. Make a note of the clogged part when introducing this Gem.

Development environment

・ Ruby 2.6.4 ・ Rails 5.2.3

Reference material

Rails Internationalization (i18n) API

Japanese localization by Rails i18n

[I18n :: InvalidLocaleData Locale file cannot be translated] (https://qiita.com/niwa1903/items/10154b8cbe1cd0416495)

Installation procedure

① Install gem'rails-i18n'.


gem 'rails-i18n', '~> 5.1'
$ bundle install

This is the usual flow when installing Rails Gem.

(2) Set the default language to Japanese. Then, pass the path so that i18n's multiple locale files are read.


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]

Add two lines in the module. Without this description, it seems that even if you install Gem, it will not support Japanese.

③ Create a ja.yml file in the locales folder

・ Japanese support for model


       user: 'user'
       board: 'Bulletin board'
         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.

・ Japanese support for view and controller


       title: 'user registration'
       to_login_page: 'Go to login page'
      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

④ Display the set Japanese with view or controller

      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' %>

You should now be able to speak Japanese using Gem'rails-i18n'!

Recommended Posts

[Rails] How to use Gem'rails-i18n' for Japanese support
[Rails] How to use enum
[Rails] How to use enum
How to use rails join
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to use Scope
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
[Rails] How to use flash messages
[Rails] How to write in Japanese
How to use Ruby on Rails
[Rails] How to use Active Storage
[Introduction to Rails] How to use render
How to use binding.pry for view files
How to use custom helpers in rails
[Rails] How to use rails console with docker
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
How to use MySQL in Rails tutorial
How to build SquashTM and how to support Japanese
[Ruby] How to use slice for beginners
[Ruby on Rails] How to use redirect_to
[Note] How to use Rails 6 Devise + cancancan
[Ruby on Rails] How to use kaminari
[Rails] How to use video_tag to display videos
[Rails] How to use helper method, confimartion
How to use credentials.yml.enc introduced in Rails 5.2
[Rails] How to translate devise into Japanese
How to use Map
How to write Rails
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
[Rails] How to use select boxes in Ransack
How to use map
How to use rails g scaffold, functions, precautions
How to use an array for HashMap keys
How to use collection_select
How to translate Rails into Japanese in general
How to use Twitter4J
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to uninstall Rails
How to use identity
How to use hashes
(For beginners) [Rails] Time saving tech! How to install and use slim
How to use enum (introduction of Japanese notation)
How to use JUnit 5
How to use JQuery in js.erb of Rails6
How to use nginx-ingress-controller with Docker for Mac
[For Rails beginners] Summary of how to use RSpec (get an overview)
How to use Dozer.mapper
How to use Gradle
[For super beginners] How to use autofocus: true
[Ruby on Rails] How to use session method
[Rails] How to use PostgreSQL in Vagrant environment