📜 ⬆️ ⬇️

Validation messages in Russian in Rails 3

The topic is simple, but I had to spend some time before I understood from disparate sources how to get validation error messages in Russian most quickly and simply. This post will help save some time for beginners.

Localization files are in config / locales. How it may look for the Russian language can be found here github.com/svenfuchs/rails-i18n/blob/master/rails/locale/ru.yml

If we copy this file to our project and write the default Russian language in config / application.rb:

config.i18n.default_locale = :ru

Then we will receive messages of the form:
')
Name
Phone


But I would also like to see Name and Phone in Russian. To do this, create in the config / locale the models folder with the file ru.yml:

 ru: activerecord: models: user:  attributes: profile: name: " " phone:  


Thus we set Russian names for the model and its properties. In order for Rails to pick up the file in application.rb, add the line:

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

And now our error message will be like this:



Own error messages


If for some reason you are not satisfied with the built-in error messages, you can add your own:

 #encoding: utf-8 #  « » class User < ActiveRecord::Base validates :name, :presence => {:message => '!     !'} validates_length_of :phone, :minimum => 7, :maximum => 10, :too_short => "    %{count} ", :too_long => "    %{count} " end 


Types of validation messages can be found here: guides.rubyonrails.org/i18n.html#error-message-interpolation

That's all the basics of localization of validation messages in RoR.

Source: https://habr.com/ru/post/128951/


All Articles