[Ruby] [Rails] Japanese localization of error message

less than 1 minute read

Overview

Here are the steps to localize error messages in Rails applications. The following message will be output if there is any input failure or incompleteness on the settlement screen.

bcb5b66aaa2336c94683fe2b94ccc49d.png

Environment

Ruby 2.6. Ruby on Rails 6.0.0

Procedure

① Described in config/application.rb

module Pictweet
 class Application <Rails::Application
   # Initialize configuration defaults for originally generated Rails version.
   config.load_defaults 6.0


   # Japanese language setting
   config.i18n.default_locale = :ja
# Omitted
 end
end

② Described in gemfile

gem rails-i18n

run bundle install

  • Up to this point, all columns are in Japanese except the column name.

③ Copy the code of the following URL to config/locales/devise.en.yml https://github.com/tigrish/devise-i18n/blob/master/rails/locales/ja.yml

  • So far, the columns created for devise (for confirming emai, password, password) are in Japanese.

④Create ja.yml in config/locales

ja:
 activerecord:
   attributes:
     user:
       nickname: Nickname
     tweet:
       text: Text
       image: Image

With this description, Nickname, Text and Image are translated into Japanese!