簡體   English   中英

翻譯模型屬性欄

[英]translating model attributes rails

我的 rails 應用程序遇到以下問題:我想翻譯一些這樣的模型屬性,以便它們正確顯示在驗證錯誤消息中。

de:
  activerecord:
    models:
      account:
        attributes:
          email:
            one: 'Email'
            other: 'Emails'

現在在rails控制台中我可以檢查

I18n.t 'activerecord.models.account.attributes.email', count: 1

返回“電子郵件”,一切似乎都很好。

但是如果我去瀏覽器並發布一個表格,我會得到錯誤

I18n::InvalidPluralizationData in AccountsController#create
translation data {:attributes=>{:email=>{:one=>"Email", :other=>"Emails"}}} can not be used with :count => 1

我該如何解決這個問題? rails 期望獲得哪些翻譯數據?

謝謝

模型名稱和屬性名稱都有各自的i18n范圍。

嘗試

activerecord.attributes.account.email

代替

activerecord.models.account.attributes.email

(並相應地調整您的Yaml文件)。

另請參見有關I18n API的便捷Rails指南

HTH!

使用 Rails 輔助方法:

Model.human_attribute_name(attribute)

你的翻譯 yaml 看起來像這樣:

activerecord:
  models:
    model_name:
      attributes:
        attribute: attribute name

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM