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.
Gemfile
gem 'rails-i18n'
$ bundle install
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
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)
ʻ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