During the production of Oriap, I translated the error message into Japanese, so I will post it so that I will not forget it. Isn't it difficult to tell where the error message is flawed in English when the user isn't filling out the form properly? I felt that, so I decided to implement Japanese localization.
Prior status: devise is introduced for user registration etc. / Single error code has been described As the flow of this time, we will translate English by creating a yaml file for Japanese conversion in the locale file. (Locale file: language file for multilingualization)
** 1. In order to translate the error message into Japanese, write a gem called "rails-i18n" in the Gemfile and bundle install it. This is the one that translates the words and phrases described in the link below into Japanese. ** ** https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/ja.yml
Gemfile
gem 'rails-i18n'
** 2. Describe config.i18n.default_locale =: ja in .config/application.rb and set the language **
config/appllication.rb
module IdeaApp
class Application < Rails::Application
#Omission
config.i18n.default_locale = :ja
#Omission
end
end
Since devise has been introduced for user registration, etc., the related items will be translated into Japanese. ** 1. Create devise.ja.yml in .config/locales. ** ** →config / locales / devise.ja.yml
** 2. Paste the code described in the link below into .devise.ja.yml. ** ** By pasting, the written words and phrases are translated into Japanese. https://github.com/tigrish/devise-i18n/blob/master/rails/locales/ja.yml
With the implementation so far, the words and phrases described in ** rails-i18n ** and ** devise.ja.yml ** can be translated into Japanese, but the other words and phrases are in Japan as shown in the image below. I haven't been able to put it into words.
Therefore, it is necessary to specify words (Nickname, Title, Category) here and convert them to Japanese.
** 1. Create ja.yml in .config/locales ** →config / locales / ja.yml
** 2. In ja.yml, describe the words you want to convert before and after conversion as follows **
config/locales/ja.yml
ja:
activerecord:
attributes:
user: #User model
nickname:nickname
phone_number:phone number
first_name:name
last_name:Last name
first_name_kana:Name katakana
last_name_kana:Last name Kana
idea: #Idea model
title:Idea name
idea:Content of the idea
price:price
category_id:Category
errors:
messages:
other_than: 「--Please select other than
Now the error message can be translated into Japanese.
If you have already written the unit test code, the expected error message will also be converted to Japanese, so you will need to rewrite it. Describe the Nickname error code as an example.
spec/models/user_spec.rb before rewriting
#Omission
context 'When new registration does not go well' do
it 'Cannot register if nickname is empty' do
@user.nickname = ''
@user.valid?
expect(@user.errors.full_messages).to include("Nickname can't be blank")
end
end
#Omission
spec/models/user_spec.rb after rewriting
#Omission
context 'When new registration does not go well' do
it 'Cannot register if nickname is empty' do
@user.nickname = ''
@user.valid?
expect(@user.errors.full_messages).to include("Please enter a nickname")
end
end
#Omission
I was able to implement it so that even people who are not good at English can read the error message. I would like to continue to be conscious of creating apps that are easy for anyone to use.
Recommended Posts