[Rails] Let's translate the error message into Japanese

There are times when the error text when creating a user, logging in, or posting something is kept in English in a cool way, but there are times when you want to make it easy to understand in Japanese, right?

I will tell you such a solution using Gem.

Install Gem

Gemfile


gem 'rails-i18n'
$ bundle install

Set error message to Japanese

config/application.rb


module SampleApp
  class Application < Rails::Application

    config.i18n.default_locale = :ja
    config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]
  end
end

Set which code corresponds to which Japanese

We will set it using a dedicated file.

python


$ mkdir config/locales/models
$ touch config/locales/models/ja.yml

ja.yml


ja:
  activerecord:
    models:
      user:User
    attributes:
      user:
        name:name
        email:mail address
        password:password
        password_confirmation:Re-enter password)

Manually add error messages errors.add

ʻUser.errors.add (: base, "additional error") `

Of course, if you do not get out after generating an error, you can not add it, so the flow is as follows.

python


> user = User.new
> user.errors
> user.errors.add(:base, "Additional error")
> user.errors.full_messages
=> ["Additional error"]

Recommended Posts

[Rails] Let's translate the error message into Japanese
[Rails] validates How to translate error sentences into Japanese
[Rails] What was the error message?
[Rails] How to translate devise into Japanese
How to translate Rails into Japanese in general
Rails error messages cannot be translated into Japanese
[Rails] Japanese localization of validation error message ~ ja.yml ~
Rails ~ Understanding the message function ~
How to translate the error message into Japanese (What to do if you cannot log in for some reason)
[Rails] I implemented the validation error message by asynchronous communication!
From Ruby on Rails error message display to Japanese localization
[Rails] Japanese localization of error message when using Form object
[Rails] Japanese localization of error messages
Japanese localization of error messages (rails)
I tried to translate the error message when executing Eclipse (Java)
About the error message Invalid redeclaration of'***'
Get the error message using the any? method
Let's make an error screen with Rails
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
The road to Japaneseizing Rails devise error messages
The devise error message is not displayed properly.
Rails Flash Message
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Install Eclipse on Mac and translate it into Japanese
[Rails] What to do if you can't get an error message with the errors method