5

I've used the instructions specifies in http://guides.rubyonrails.org/i18n.html to translate fields of my model, but the labels doesn't come translated. What I'm doing wrong.

I have a User model with the field name and I'd like to have it translated to Brazilian Portugues (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 a form:

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

I sill have the field labeled as "name" and not as "Nome" as I'd like it to be. I also have

config.i18n.default_locale = :pt_br 

in my environment.rb

What is missing?

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141
Daniel Cukier
  • 11,502
  • 15
  • 68
  • 123

3 Answers3

5

You need to namespace your attributes under user, i.e.:

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

You can also install the i18n_label plugin to automatically translate the labels.

davegson
  • 8,205
  • 4
  • 51
  • 71
eremite
  • 1,886
  • 15
  • 18
0

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

theIV
  • 25,434
  • 5
  • 54
  • 58
  • It works for the field, but I still get untranslated texts in the errors frame (when I leave blank a mandatory field, for example) – Daniel Cukier Sep 06 '09 at 23:14
0

What about human_attribute_name? Is there since Rails 2.1.0.

I think it's better then any other solutions:

<%= f.label :name, User.human_attribute_name(:name) %>
KARASZI István
  • 30,900
  • 8
  • 101
  • 128