如何在ActiveModel错误消息中本地化字段名称?

时间:2011-01-19 14:32:09

标签: ruby-on-rails-3 localization activemodel

# in the login form in new.html.haml
= f.label :email, t('.email')

# in fi.yml
fi:
  activemodel:
    errors:
      models:
        user:
          attributes:
            email: 
              taken: "on jo käytössä." # means "is in use"

# The actual error message generated
=>"Email on jo käytössä."

因此,它会在错误消息的其余部分之前添加字段名称..但字段名称未翻译..

我可以为标签电子邮件添加翻译吗?如果可以,它应该去哪里以便ActiveModel找到它? 如果没有,解决方案是什么?

4 个答案:

答案 0 :(得分:4)

我有类似的问题

fi:
  activemodel:
    attributes:
      user:
        email: "Sähköposti"

你的例子对我不起作用,但是把我送上了正确的轨道。 我更改了activemodel:for activerecord:在我的情况下有效。

我也意识到我可以改变

f.label :email, t('.email')

f.label :email

仍然保留我的翻译。

没有用haml试过。我在我的案例中使用了erb。不知道是否有话要说。

答案 1 :(得分:3)

在Rails 3.2中,您必须更改activemodel的{​​{1}}密钥:

activerecord

另外,请注意,如果模型位于模块内,则必须指明它:

fi:
  activerecord:
    attributes:
      user:
        email: "Sähköposti"
    errors:
      models:
        user:
          attributes:
            email:
              taken: "on jo käytössä."

这是link to the official Rails documentation

答案 2 :(得分:2)

显然,就像这样

fi:
  activemodel:
    attributes:
      user:
        email: "Sähköposti"
    errors:
      models:
        user:
          attributes:
            email:
              taken: "on jo käytössä."

答案 3 :(得分:1)

以下内容适用于我以模型的形式翻译f.label。

在config / locales / your language.yml文件中:

fr:
  helpers:
    label:
      mymodel:
        attribute1: 'translation1'
        attribute2: 'translation2'
相关问题