[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)

Summary of this article

-I was worried about the problem that the validation error of the Form object was not translated into Japanese, so I will leave it as a memorandum. ・ As an aside, I touched on the difference between ActiveRecord and ActiveModel.

Development environment

Mac OS Catalina 10.15.4 ruby 2.6 series rails 6.0 series

Prerequisites

I introduced gem'rails-i18n' and the default language setting is already Japanese. Also, I have already created a ja.yml file in the locals directory of config.

Causes of failure to translate error messages into Japanese and countermeasures

In conclusion, the Form object created this time is a model that inherits the activemodel by include ActiveModel: Model. It seems that the problem was trying to translate it into Japanese as a model that inherited activerecord.

ja.yml


ja:
  activerecord:
    models:
      detail:Detailed information
      posts_tag:Post
    attributes:
      user:
        name:name
      detail:
        age:age
        pr:PR statement
        area_id:residence
        occupation_id:Profession
        interest_id:Interest
      posts_tag: 
        title:title
        content:Overview
        name:tag

ja.yml



ja:
  activerecord:
    models:
      detail:Detailed information
    attributes:
      user:
        name:name
      detail:
        age:age
        pr:PR statement
        area_id:residence
        occupation_id:Profession
        interest_id:Interest
  
  activemodel:
    models:
      posts_tag:Post
    attributes:
      posts_tag: 
        title:title
        content:Overview
        name:tag

Digression

By the way, the difference between ActiveRecord and ActiveModel inherited by the default model generated by rails g model is whether or not it is possible to communicate with the DB.

The former is possible, so you can pull the data already saved with model name.find etc., but the latter ActiveModel usually cannot use the find method.

Besides, in validation```validates uniqueness: true``Etc. can not be described in ActiveModel because it is judged whether it is unique by referring to the data stored in the DB.

While it's convenient like that, Active Model is rather complicated.... Anyway, thank you for reading to the end!

Recommended Posts