Japaneseization of Rails error messages [devise / Form objects, etc.]

background

I am studying to become an engineer from inexperienced.

When I was writing a simple application, the validation error message got stuck in English When I was investigating how to translate the error message into Japanese, I couldn't find an article that contains all the information I want to know. I created an article as a memorandum of my own, hoping that it will be useful for those who are studying programming in the same way. I think there are still some points that have yet to be reached, but if you have any notices, please let us know in the comments.

environment

Screen before change (example)

If nothing is set, the validation error message will be output in English as shown in the image below. スクリーンショット 2020-10-28 13.30.14.png

Screen after change (example)

After the change, an error message will be output in Japanese as shown in the image below. スクリーンショット 2020-10-28 13.33.32.png

Preparation for Japanese localization

・ Preparation ① (Installation of Gem)

To translate the error message into Japanese, use the gem rails-i18n. Add the following sentence to your Gemfile and run bundle install in your terminal.

gem 'rails-i18n'
#Put the above code in Gemfile

・ Preparation ② (change the language setting of locale)

Next, add the following sentence to config / application.rb to change the language setting. If you restart the server after adding, a part of the error message will be translated into Japanese as shown in the image below.

config/application.rb


class Application < Rails::Application
    config.load_defaults 6.0
    #Below this is the code below
    config.i18n.default_locale = :ja
    #Describe the above code
  end

スクリーンショット 2020-10-28 13.55.18.png

However, as it is, the attribute name of model remains in English. So, next time, I will change it to Japanese.

・ Change the column name to Japanese

In order to change the attribute name of model, it is necessary to make a correspondence table in Japanese. Create a file called ja.yml in config / locales / models / and describe the column name and the corresponding Japanese as shown below.

config/locales/models/ja.yml


ja:
  activerecord:
    models:
      item:Product
    attributes:
      item:
        image:Product image
        name:Product name
        details:Product description
        price:price

I wanted to change the attribute name of the model called item, so I put it as item in the code. ** Please change the item item in the above code according to the model you want to change **.

・ Read the above correspondence table

The file set above will not be read automatically. So, add a sentence to read this yml file to config / application.rb.

config/application.rb


class Application < Rails::Application
    config.load_defaults 6.0
    config.i18n.default_locale = :ja
#Below this is the code below
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.yml').to_s]
#Describe the above code
  end

If you have set up so far, everything should be changed to Japanese notation as shown below.

スクリーンショット 2020-10-28 13.33.32.png

・ Change to an easy-to-understand error message

If you use a gem called rails-i18n, most error messages will be converted to Japanese, but When you set the validation yourself, as shown below The output may not be obvious at first glance. In such a case, set the error message yourself. The following is an example.

[Example]

When using ActiveHash to select the prefecture name in a pull-down format and validating it by yourself スクリーンショット 2020-10-28 15.14.20.png

"Change before"

スクリーンショット 2020-10-28 15.09.18.png

Code in the validated model


   #abridgement
with_options presence: true do
   #abridgement
    validates :prefecture_id, numericality: { other_than: 1 }
   #abridgement
  end

«After change»

スクリーンショット 2020-10-28 15.14.02.png

Code in the validated model


   #abridgement
with_options presence: true do
   #abridgement
    validates :prefecture_id, numericality: { other_than: 1 ,message: 'please choose'}
   #abridgement
  end

You can change it to easier-to-understand Japanese by setting options such as ** message:'message content' ** as described above.

・ If you want to add more column names

If you want to add more column names, please add them yourself as shown below.

config/locales/models/ja.yml


ja:
  activerecord:
    models:
      item:Product
    attributes:
      item:
        image:Product image
        name:Product name
        details:Product description
        price:price
        #From here, write the correspondence table between the column name and Japanese by yourself as shown below.
        category_id:Category
        status_id:Product condition
        prefecture_id:Shipping area

When Japaneseizing a Form object defined independently

・ Preparation

If you have made the above settings, you do not need to do anything. If not, follow the above procedure to prepare (install Gem, etc.) and create a correspondence table (ja.yml). Let's read the correspondence table (added to config / application.rb).

-Edit config / locales / models / ja.yml

Add config / locales / models / ja.yml as follows.

config/locales/models/ja.yml


ja:
  activerecord:
    models:
      item:Product
    attributes:
      item:
        image:Product image
        name:Product name
        price:price
        details:Product description
#Add the following code below from here
  activemodel:
    attributes:
      user_purchase:
        city:Municipality
        postal_code:Postal code
        phone_number:phone number
        prefecture_id:Prefectures
        house_number:address
        building:Building name
#Add the above code (please change the column name etc. by yourself)

To translate the Form object defined by yourself into Japanese, write ** activemodel: ** and set it under it. If you do not set it properly, it will not be changed to Japanese.

Supplement

Since it is set to ActiveModel :: Model, it does not respond even if you put the code in attributes: under activerecord :.

Code in the model that set the Form object


class UserPurchase
  include ActiveModel::Model #ActiveModel by itself::Model is set
  #abridgement
end

config/locales/models/ja.yml


ja:
  activerecord:
    models:
      item:Product
    attributes:
      item:
        image:Product image
        name:Product name
        price:price
        details:Product description
#Since the setting is not ActiveRecord, writing code below from here does not respond

Japanese localization of devise

The default setting for devise, which allows you to easily implement user registration functions, is English. This can also be changed to Japanese.

Screen before change (example)

スクリーンショット 2020-10-28 16.08.08.png

Screen after change (example)

スクリーンショット 2020-10-28 16.08.16.png

・ Installation of Gem

To translate devise error messages into Japanese, use the devise-i18n and devise-i18n-views gems. Add the following sentence to your Gemfile and run bundle install in your terminal.

gem 'devise-i18n'
gem 'devise-i18n-views'
#Put the above code in Gemfile

・ Creation of Japanese correspondence table

Generate a Japanese translation file with the command. Execute the following command in the terminal.

rails g devise:views:locale ja
#If the execution result is displayed as shown below, it is successful.
  create  config/locales/devise.views.ja.yml

Make sure that the file is generated properly.

ruby:config/locales/devise.views.ja.yml


ja:
  activerecord:
    errors:
      models:
        user:
          attributes:
            email:
              taken: "Is already in use."
              #Omitted because it is too long
    unlocks:
      new:
        resend_unlock_instructions: "Resend account unfreeze method"
              #The above is the last line

・ Change the language setting of locale

If you have already set it, this step is not necessary. If not, add the code below

config/application.rb


class Application < Rails::Application
    config.load_defaults 6.0
    #Below this is the code below
    config.i18n.default_locale = :ja
    #Describe the above code
  end

After completing the work so far, restart the server and it should be changed to Japanese.

・ If you want to add more column names

If you want to add more column names, please add them yourself as shown below.

config/locales/models/ja.yml


ja:
  activerecord:
    errors:
      #Since it is long below, it is omitted
              confirmation: "Does not match the content."
    attributes:
      user:
        current_password: "Current password"
        name:name
        email: "mail address"
        password: "password"
        password_confirmation: "Confirmation password"
        remember_me: "login automatically from now on"
     #Add column name and corresponding Japanese by yourself if necessary below
        nickname: "nickname"
        first_name: "name"
        last_name: "Last name"
        birthday: "Birthday"
     #Described as above
    models:
       user: "User"
     #Since it is long below, it is omitted

Summary

The above is the Japanese localization of the error message I made. If necessary, we will make corrections and additions. Thank you for visiting our website. If you have any questions, please let us know in the comments.

References

Japanese localization of Rails validation error message Japaneseization of Rails Form object Change the format of error messages displayed in ActiveRecord validates [Rails5] Japaneseize with Devise-i18n I referred to the above article. This article may be written in more detail, so Please refer to that as well.

Recommended Posts

Japaneseization of Rails error messages [devise / Form objects, etc.]
[Rails] Japanese localization of error messages
[Rails] Display form error messages asynchronously
Japanese localization of error messages (rails)
The road to Japaneseizing Rails devise error messages
Rails: Japanese localization of validation messages including devise
[Ruby on Rails] Individual display of error messages
Japanese localization of error messages
Japanese localization of error messages
[rails] error during devise installation
[Rails] Japanese localization of error message when using Form object
[Rails] Sign-up function using devise error number of arguments (given 0, expected 1)
Animated display of Swift error messages
[Rails] How to display error messages individually
Testing for Error Messages: Rails Tutorial Notes-Chapter 7
[Rails] devise
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Ruby on Rails] How to display error messages
Rails error messages cannot be translated into Japanese
[Rails] Japanese localization of validation error message ~ ja.yml ~
Introduction of Rspec and Japanese localization of error messages
[Note] Summary of rails login function using devise ①
[Rails] How to get success and error messages