[RAILS] Japanese localization of error messages

Introduction

During the production of Oriap, I translated the error message into Japanese, so I will post it so that I will not forget it. Isn't it difficult to tell where the error message is flawed in English when the user isn't filling out the form properly? I felt that, so I decided to implement Japanese localization.

Prior status: devise is introduced for user registration etc. / Single error code has been described As the flow of this time, we will translate English by creating a yaml file for Japanese conversion in the locale file. (Locale file: language file for multilingualization)

1. Introduction of gem

** 1. In order to translate the error message into Japanese, write a gem called "rails-i18n" in the Gemfile and bundle install it. This is the one that translates the words and phrases described in the link below into Japanese. ** ** https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/ja.yml

Gemfile


gem 'rails-i18n'

** 2. Describe config.i18n.default_locale =: ja in .config/application.rb and set the language **

config/appllication.rb


module IdeaApp
  class Application < Rails::Application
    #Omission
    config.i18n.default_locale = :ja
    #Omission
  end
end

2. Devise related Japanese localization

Since devise has been introduced for user registration, etc., the related items will be translated into Japanese. ** 1. Create devise.ja.yml in .config/locales. ** **  →config / locales / devise.ja.yml

** 2. Paste the code described in the link below into .devise.ja.yml. ** ** By pasting, the written words and phrases are translated into Japanese. https://github.com/tigrish/devise-i18n/blob/master/rails/locales/ja.yml

3. Japaneseization of the specified word

With the implementation so far, the words and phrases described in ** rails-i18n ** and ** devise.ja.yml ** can be translated into Japanese, but the other words and phrases are in Japan as shown in the image below. I haven't been able to put it into words.

Therefore, it is necessary to specify words (Nickname, Title, Category) here and convert them to Japanese.

** 1. Create ja.yml in .config/locales **  →config / locales / ja.yml

** 2. In ja.yml, describe the words you want to convert before and after conversion as follows **

config/locales/ja.yml


ja:
  activerecord:
    attributes:
      user: #User model
        nickname:nickname
        phone_number:phone number
        first_name:name
        last_name:Last name
        first_name_kana:Name katakana
        last_name_kana:Last name Kana
      idea: #Idea model
        title:Idea name
        idea:Content of the idea
        price:price
        category_id:Category
    errors:
     messages:
       other_than:  「--Please select other than

4. After converting to Japanese

Now the error message can be translated into Japanese.

5. Supplement: Error code correction

If you have already written the unit test code, the expected error message will also be converted to Japanese, so you will need to rewrite it. Describe the Nickname error code as an example.

spec/models/user_spec.rb before rewriting


#Omission
context 'When new registration does not go well' do
  it 'Cannot register if nickname is empty' do
    @user.nickname = ''
    @user.valid?
    expect(@user.errors.full_messages).to include("Nickname can't be blank")
  end
end
#Omission

spec/models/user_spec.rb after rewriting


#Omission
context 'When new registration does not go well' do
  it 'Cannot register if nickname is empty' do
    @user.nickname = ''
    @user.valid?
    expect(@user.errors.full_messages).to include("Please enter a nickname")
  end
end
#Omission

Finally

I was able to implement it so that even people who are not good at English can read the error message. I would like to continue to be conscious of creating apps that are easy for anyone to use.

Recommended Posts

Japanese localization of error messages
Japanese localization of error messages
[Rails] Japanese localization of error messages
Japanese localization of error messages (rails)
Introduction of Rspec and Japanese localization of error messages
Japanese localization of Ubuntu20.04
Japanese localization of Eclipse
Rails: Japanese localization of validation messages including devise
[Rails] Japanese localization of validation error message ~ ja.yml ~
[Japanese localization of gem: devise]
Animated display of Swift error messages
[Rails] Japanese localization of error message when using Form object
Japanese localization of CentOS 8 and Japanese keyboard layout
How to display error messages in Japanese
Rails error messages cannot be translated into Japanese
Convert error messages to Japanese Supports multiple tables
[Ruby on Rails] Individual display of error messages
About Ruby error messages
Implementation of flash messages
Japaneseization of Rails error messages [devise / Form objects, etc.]
Cleansing processing of Japanese sentences
[Rails] Japanese localization using rails-i18n
From Ruby on Rails error message display to Japanese localization
Japanese localization of Docker container of CentOS7 / CentOS8 and Japan time setting
Summary of java error processing
About error handling of comment function
[Rails] Display form error messages asynchronously
Validation of log messages using mockito
[Rails] Validation settings and Japanese localization