Ruby on Rails internationalization, rubyrails

Source: Internet
Author: User

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]


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.