[RUBY] [Rails] Japaneseize model attribute names

When the Rails error message is translated into Japanese, only the attribute name of the model is written in English alphabet. .. .. Since I fell into the situation, I will record the way to solve it.

① Change the default message from the dictionary to Japanese

スクリーンショット 2021-01-14 16.46.27.png Create a new file under config/initializers/(name the file locale.rb for the time being). Described as follows.

config/initializers/locale.rb


I18n.config.available_locales = :ja
I18n.default_locale = :ja

② Create a Japanese dictionary file

Create a file called ja.yml in config/locales /. You can write the contents to be described in it by yourself, but since there are already created ones, save here by copying or downloading.

This will restart the Rails server. If you press the registration button intentionally with the blank on the new creation screen (validation that will cause an error if you try to register with a blank) so that an error will appear ... スクリーンショット 2021-01-14 16.06.48.png It's definitely in Japanese, but ... Task name, Detail, Deadline are still in alphabetical notation.

As I wrote in the title, these are not the default Japanese notation, but the attribute names of the model I named, so they are treated differently. In order to make these into Japanese, you have to prepare another file.

③ Change the model attribute to Japanese

This time, create a dictionary "model.ja.yml" that converts the model into a Japanese table in config/locales /.

config/locales/model.ja.yml


ja:
  activerecord:
    models:
      task:blog
      user:user
    attributes:
      task:
        task_name:Task name
        detail:Details
        deadline:Deadline
      user:
        title:title
        name:name
        email:mail address
        current_password:Current password
        password:password
        password_confirmation:Confirmation password
        remember_me:Remember login

In this way, describe the Japanese corresponding to the model attribute. Now restart the server and try to get the error in the same way as before ... スクリーンショット 2021-01-14 16.36.41.png The model attribute part is also in Japanese.

Recommended Posts

[Rails] Japaneseize model attribute names
[Rails] Model Association (Association)
Model association in Rails
[Rails] Register by attribute of the same model using Devise
Japaneseize using i18n with Rails
About naming Rails model methods
Rails model and table naming conventions