On the internationalization _ruby topic of Ruby on Rails

Source: Internet
Author: User
Tags i18n ruby on rails

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:

= T '. Title '

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]


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.