Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding model attribute names using I18n - can't get this working

I'm trying to override the string used to describe the 'login' attribute of my User model to be "User name" instead. I thought that this was set in vendor/rails/activerecord/lib/active_record/locale/en.yml. I've tried changing it here and in my config/locales/en.yml file, and in neither case does it work (i restart the server after every change).

In both cases it's done like this:

en:
...
  activerecord:
    attributes:
         user:
           login: User Name

When i test it out, eg having f.label :login in my new user form, it comes out as "Login" not "User Name". Am i labouring under some fundamental error about how this stuff works, or is it genuinely not working? Either way, can anyone tell me how to fix it? thanks, max

like image 487
Max Williams Avatar asked Sep 14 '10 16:09

Max Williams


2 Answers

In case if any of you have trouble the translation aren't loaded, I debugged like this:

First I use the script in https://stackoverflow.com/a/10211540/474597 to setup logging. Then as I run the server and render the pages, I can see in that log the keys used to get the translations. Then I can check if the keys I use are correct or not.


One pitfall is that, the first key isn't necessarily the key they will use. I have seen cases where it loads the correct key, and then looks for another key which does not have translation, resulting in the English translation. I had to put the translation in those two keys.

For example: my Foo has_many Bar, and Bar has_many Duu, and Duu has a price column. In order to translate that price column, I have to have the following for it to work:

  activerecord:
    attributes:
      'foo/bar/duu':
        price: "price"

Even though the log has this:

:duu
:"activerecord.attributes.duu.text"
:"activerecord.errors.models.duu.attributes.text.blank"
:"activerecord.errors.models.duu.blank"
:"activerecord.errors.messages.blank"
:"errors.attributes.text.blank"
:"errors.messages.blank"
:"activerecord.attributes.foo/bars/duus.text"
like image 137
lulalala Avatar answered Oct 09 '22 07:10

lulalala


I'm answering this here on request of someone... I did fix this and didn't update my question, sorry. I was on the right lines in my question but didn't have the right "path" down to the keys: the names of fields, as they are displayed, are called "labels" and they have their own section outside of the :activerecord part of the yaml tree.

The correct way to do it is

en:
  user:
    labels:
      login: "User Name"
      email: "Email Address"

where "user" is a lowercased model name and login and email are fields.

like image 38
Max Williams Avatar answered Oct 09 '22 07:10

Max Williams