Language-related settings and strings should not be used in views, models, and controllers. These words should be moved to a language file under the Config/locales.
When the label of the ActiveRecord model needs to be translated, use the ActiveRecord scope:
En:
ActiveRecord:
models:
user:member
attributes:
User:
name: "Full Name"
User.model_name.human then returns "Member", and User.human_attribute_name ("name") returns "full name". The translations of these properties are used by the view as a label.
Separate the text used in the view from the ActiveRecord attribute translation. Place the language file that is used for the model in a folder named models, and place the text in the view in a folder named views.
When the language files that use the additional directories are organized, the directories are described in the Application.rb file in order to load them.
# config/application.rb
Config.i18n.load_path + = dir[rails.root.join (' config ', ' locales ', ' * * ', ' *.{ RB,YML} ')]
Place the shared localization options, such as dates or currency formats, in the root directory of the locales.
Use the simplified form of i18n method: i18n.t to replace i18n.translate and use I18N.L to replace I18n.localize.
Use the text used in the Lazy query view. Suppose we have the following structure:
En:
users: Show
:
title: "User Details page"
The value of the Users.show.title can be app/views/users/show.html.haml by this query:
Replace the specified: Scope option by using a dot-delimited key for the controller and model. Point-delimited calls are easier to read and track levels.
# so the child calls
i18n.t ' Activerecord.errors.messages.record_invalid '
and not so
i18n.t:record_invalid,: scope = > [: ActiveRecord,: Errors,: Messages]