## 國際化
* <a name="locale-texts"></a>
不應在視圖、模型或控制器里添加語言相關的設置,應在 `config/locales` 目錄下進行設置。
<sup>[[link](#locale-texts)]</sup>
* <a name="translated-labels"></a>
當 ActiveRecord 模型的標簽需要被翻譯時,應使用`activerecord` scope:
<sup>[[link](#translated-labels)]</sup>
```
en:
activerecord:
models:
user: Member
attributes:
user:
name: "Full name"
```
然后 `User.model_name.human` 會返回 "Member" ,而 `User.human_attribute_name("name")` 會返回 "Full name"。這些屬性的翻譯會作為視圖中的標簽。
* <a name="organize-locale-files"></a>
把在視圖中使用的文字與 ActiveRecord 的屬性翻譯分開。把模型使用的語言文件放在 `models` 目錄下,把視圖使用的文字放在 `views` 目錄下。
<sup>[[link](#organize-locale-files)]</sup>
* 當使用額外目錄來設置語言文件時,應在 `application.rb` 文件里列出這些目錄以加載設置。
```Ruby
# config/application.rb
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
```
* <a name="shared-localization"></a>
把共享的本地化選項,如日期或貨幣格式,放在 `locales` 的根目錄下。
<sup>[[link](#shared-localization)]</sup>
* <a name="short-i18n"></a>
應使用精簡形式的 I18n 方法:
使用 `I18n.t` 而非 `I18n.translate`;
使用 `I18n.l` 而非 `I18n.localize`。
<sup>[[link](#short-i18n)]</sup>
* <a name="lazy-lookup"></a>
應使用 "懶惰" 查詢來獲取視圖中使用的文本。假設我們有以下結構:
<sup>[[link](#lazy-lookup)]</sup>
```
en:
users:
show:
title: "User details page"
```
`users.show.title` 的數值能這樣被 `app/views/users/show.html.haml` 獲取:
```Ruby
= t '.title'
```
* <a name="dot-separated-keys"></a>
應在控制器與模型中使用點分隔的鍵,而非指定 `:scope` 選項。點分隔的調用更容易閱讀,也更易追蹤層級關系。
<sup>[[link](#dot-separated-keys)]</sup>
```Ruby
# 差
I18n.t :record_invalid, :scope => [:activerecord, :errors, :messages]
# 好
I18n.t 'activerecord.errors.messages.record_invalid'
```
* <a name="i18n-guides"></a>
更詳細的 Rails i18n 信息可以在 [Rails Guides](http://guides.rubyonrails.org/i18n.html) 找到。
<sup>[[link](#i18n-guides)]</sup>