I am studying to become an engineer from inexperienced.
When I was writing a simple application, the validation error message got stuck in English When I was investigating how to translate the error message into Japanese, I couldn't find an article that contains all the information I want to know. I created an article as a memorandum of my own, hoping that it will be useful for those who are studying programming in the same way. I think there are still some points that have yet to be reached, but if you have any notices, please let us know in the comments.
If nothing is set, the validation error message will be output in English as shown in the image below.
After the change, an error message will be output in Japanese as shown in the image below.
To translate the error message into Japanese, use the gem rails-i18n. Add the following sentence to your Gemfile and run bundle install in your terminal.
gem 'rails-i18n'
#Put the above code in Gemfile
Next, add the following sentence to config / application.rb to change the language setting. If you restart the server after adding, a part of the error message will be translated into Japanese as shown in the image below.
config/application.rb
class Application < Rails::Application
config.load_defaults 6.0
#Below this is the code below
config.i18n.default_locale = :ja
#Describe the above code
end
However, as it is, the attribute name of model remains in English. So, next time, I will change it to Japanese.
In order to change the attribute name of model, it is necessary to make a correspondence table in Japanese. Create a file called ja.yml in config / locales / models / and describe the column name and the corresponding Japanese as shown below.
config/locales/models/ja.yml
ja:
activerecord:
models:
item:Product
attributes:
item:
image:Product image
name:Product name
details:Product description
price:price
I wanted to change the attribute name of the model called item, so I put it as item in the code. ** Please change the item item in the above code according to the model you want to change **.
The file set above will not be read automatically. So, add a sentence to read this yml file to config / application.rb.
config/application.rb
class Application < Rails::Application
config.load_defaults 6.0
config.i18n.default_locale = :ja
#Below this is the code below
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.yml').to_s]
#Describe the above code
end
If you have set up so far, everything should be changed to Japanese notation as shown below.
If you use a gem called rails-i18n, most error messages will be converted to Japanese, but When you set the validation yourself, as shown below The output may not be obvious at first glance. In such a case, set the error message yourself. The following is an example.
When using ActiveHash to select the prefecture name in a pull-down format and validating it by yourself
Code in the validated model
#abridgement
with_options presence: true do
#abridgement
validates :prefecture_id, numericality: { other_than: 1 }
#abridgement
end
Code in the validated model
#abridgement
with_options presence: true do
#abridgement
validates :prefecture_id, numericality: { other_than: 1 ,message: 'please choose'}
#abridgement
end
You can change it to easier-to-understand Japanese by setting options such as ** message:'message content' ** as described above.
If you want to add more column names, please add them yourself as shown below.
config/locales/models/ja.yml
ja:
activerecord:
models:
item:Product
attributes:
item:
image:Product image
name:Product name
details:Product description
price:price
#From here, write the correspondence table between the column name and Japanese by yourself as shown below.
category_id:Category
status_id:Product condition
prefecture_id:Shipping area
If you have made the above settings, you do not need to do anything. If not, follow the above procedure to prepare (install Gem, etc.) and create a correspondence table (ja.yml). Let's read the correspondence table (added to config / application.rb).
Add config / locales / models / ja.yml as follows.
config/locales/models/ja.yml
ja:
activerecord:
models:
item:Product
attributes:
item:
image:Product image
name:Product name
price:price
details:Product description
#Add the following code below from here
activemodel:
attributes:
user_purchase:
city:Municipality
postal_code:Postal code
phone_number:phone number
prefecture_id:Prefectures
house_number:address
building:Building name
#Add the above code (please change the column name etc. by yourself)
To translate the Form object defined by yourself into Japanese, write ** activemodel: ** and set it under it. If you do not set it properly, it will not be changed to Japanese.
Since it is set to ActiveModel :: Model, it does not respond even if you put the code in attributes: under activerecord :.
Code in the model that set the Form object
class UserPurchase
include ActiveModel::Model #ActiveModel by itself::Model is set
#abridgement
end
config/locales/models/ja.yml
ja:
activerecord:
models:
item:Product
attributes:
item:
image:Product image
name:Product name
price:price
details:Product description
#Since the setting is not ActiveRecord, writing code below from here does not respond
The default setting for devise, which allows you to easily implement user registration functions, is English. This can also be changed to Japanese.
To translate devise error messages into Japanese, use the devise-i18n and devise-i18n-views gems. Add the following sentence to your Gemfile and run bundle install in your terminal.
gem 'devise-i18n'
gem 'devise-i18n-views'
#Put the above code in Gemfile
Generate a Japanese translation file with the command. Execute the following command in the terminal.
rails g devise:views:locale ja
#If the execution result is displayed as shown below, it is successful.
create config/locales/devise.views.ja.yml
Make sure that the file is generated properly.
ruby:config/locales/devise.views.ja.yml
ja:
activerecord:
errors:
models:
user:
attributes:
email:
taken: "Is already in use."
#Omitted because it is too long
unlocks:
new:
resend_unlock_instructions: "Resend account unfreeze method"
#The above is the last line
If you have already set it, this step is not necessary. If not, add the code below
config/application.rb
class Application < Rails::Application
config.load_defaults 6.0
#Below this is the code below
config.i18n.default_locale = :ja
#Describe the above code
end
After completing the work so far, restart the server and it should be changed to Japanese.
If you want to add more column names, please add them yourself as shown below.
config/locales/models/ja.yml
ja:
activerecord:
errors:
#Since it is long below, it is omitted
confirmation: "Does not match the content."
attributes:
user:
current_password: "Current password"
name:name
email: "mail address"
password: "password"
password_confirmation: "Confirmation password"
remember_me: "login automatically from now on"
#Add column name and corresponding Japanese by yourself if necessary below
nickname: "nickname"
first_name: "name"
last_name: "Last name"
birthday: "Birthday"
#Described as above
models:
user: "User"
#Since it is long below, it is omitted
The above is the Japanese localization of the error message I made. If necessary, we will make corrections and additions. Thank you for visiting our website. If you have any questions, please let us know in the comments.
Japanese localization of Rails validation error message Japaneseization of Rails Form object Change the format of error messages displayed in ActiveRecord validates [Rails5] Japaneseize with Devise-i18n I referred to the above article. This article may be written in more detail, so Please refer to that as well.
Recommended Posts