ActiveRecord错误消息:字段的翻译

我已经使用http://guides.rubyonrails.org/i18n.html中指定的指令来翻译我的模型的字段,但标签不会被翻译。 我做错了什么。

我有一个带有字段nameUser模型,我想把它翻译成巴西葡萄牙语(pt_br),所以我得到了我的pt_br.yml:

 pt_br: errors: "Erro!" activerecord: models: user: "Usuário" attributes: name: "Nome" address: "Endereço" errors: template: body: "Por favor, corrija os campos assinalados" header: "Dados inválidos" messages: blank: "é obrigatório" taken: "já existe" too_short: "incompleto" 

当我到达带有表单的页面时:

      

我将这个字段标记为“名称”,而不是像我希望的那样标记为“Nome”。 我也有

 config.i18n.default_locale = :pt_br 

在我的环境中.rb

缺什么?

您需要在user下命名属性,即:

 pt_br: activerecord: attributes: user: name: "Nome" address: "Endereço" 

您还可以安装i18n_label插件以自动翻译标签。

您可以尝试<%= f.label :name, t('activerecord.attributes.name') %>

那么human_attribute_name怎么样? 自从Rails 2.1.0开始以来。

我认为它比任何其他解决方案更好:

 <%= f.label :name, User.human_attribute_name(:name) %>