ActiveRecord Error Messages: Field Translation

I used the instructions given at http://guides.rubyonrails.org/i18n.html to translate the fields of my model, but these labels are not translated. What am I doing wrong.

I have a model Userwith a field name, and I would like it to be translated into Brazilian Portugal (pt_br), so I got my 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"

when I got to the page with the form:

<% form_for(@usuario) do |f| %>
<%= f.error_messages %>
<%= f.label :name %>
<%= f.text_field :name %>
<% end %>

I have a field labeled as “name” and not as “Nome” as I would like. I also have

config.i18n.default_locale = :pt_br 

in my .rb environment

What is missing?

+5
source share
3 answers

user, ...

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

i18n_label .

+5

<%= f.label :name, t('activerecord.attributes.name') %>.

0

How about human_attribute_name? Is there any with Rails 2.1.0.

I think this is better than any other solutions:

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

All Articles