Ruby on Rails internationalization, rubyrails
Language-related settings and strings should not be used in views, models, and controllers. The text should be moved to the language file under config/locales.
When the tags of the ActiveRecord model need to be translated, the scope of activerecord is used:
en: activerecord: models: user: Member attributes: user: name: "Full name"
Then User. model_name.human will return "Member", and User. human_attribute_name ("name") will return "Full name ". The translation of these attributes will be used by the view as a label.
The text used in the view is separated from the attribute translation of ActiveRecord. Put the language file used for the model in the folder named models, and put the text used for the view in the folder named views.
When the language files of the additional directories are organized, describe these directories in the application. rb file to load these directories.
# config/application.rb config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
Place the shared localization options, such as the date or currency format, in the root directory of locales.
Use the simplified I18n method: I18n. t to replace I18n. translate and use I18n. l to replace I18n. localize.
Use "lazy" to query the text used in the view. Suppose we have the following structure:
en: users: show: title: "User details page"
The value of users. show. title can be queried by app/views/users/show.html. haml as follows:
= t '.title'
In the controller and model, use the dot-separated key to replace the specified: scope option. Calls separated by points are easier to read and track.
# Call I18n. t 'activerecord. errors. messages. record_invalid' # Instead of I18n. t: record_invalid,: scope => [: activerecord,: errors,: messages]