简体   繁体   English

翻译模型属性栏

[英]translating model attributes rails

I got the following problem with my rails application: I want to translate some model attributes like this, so that they show up correctly in the validation error messages for example.我的 rails 应用程序遇到以下问题:我想翻译一些这样的模型属性,以便它们正确显示在验证错误消息中。

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

Now in the rails console I can check现在在rails控制台中我可以检查

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

which returns "Email" and everything seems to be fine.返回“电子邮件”,一切似乎都很好。

But in if I go to the browser and post a form, I get the error但是如果我去浏览器并发布一个表格,我会得到错误

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

How can I resolve this?我该如何解决这个问题? What translation data does rails expect to get? rails 期望获得哪些翻译数据?

Thanks谢谢

Model names and Attribute names both have their own i18n scope. 模型名称和属性名称都有各自的i18n范围。

Try 尝试

activerecord.attributes.account.email

instead of 代替

activerecord.models.account.attributes.email

(and adjust your yaml file accordingly). (并相应地调整您的Yaml文件)。

See also the handy Rails Guide on the I18n API . 另请参见有关I18n API的便捷Rails指南

HTH! HTH!

Use the Rails helper method:使用 Rails 辅助方法:

Model.human_attribute_name(attribute)

Your translation yaml just look like so:你的翻译 yaml 看起来像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM