开发者

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

开发者 https://www.devze.com 2023-01-15 22:40 出处:网络
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/loca

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 c开发者_StackOverflow中文版hange).

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


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"


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.


I think you can find what you are looking for here: http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models


You may store your "User Name" string under any name in your en.yml file:

en:
 user:
   user_name: User Name

and in your view

<%= f.label :login, t('user.user_name') %> 
0

精彩评论

暂无评论...
验证码 换一张
取 消