Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Rails I18n.t to translate an ActiveRecord attribute?

Trying to use my active record translations in config/locales/de.yml also in my views. I thought I am clever using this:

de:
  activerecord: 
    attributes:
      user:
        login: "Benutzerkennung"
        comment: "Bemerkungen"

And in my view this:

<%= label_tag :login, t('activerecord.attributes.user.login') %> 

But instead of the translation value ("Benutzerkennung") I am getting the infamous "translation missing: de, activerecord, attributes, user, login"

Has anybody got this working (not using the label translation plugin (I am wary of potential side effects), or User.humanize_attribute_name)? What am I missing? (it does work when I use "activerecord1" or something else than activerecord, so my setup seems to be fine)

Thanks!

like image 665
juwalter Avatar asked Dec 07 '09 23:12

juwalter


2 Answers

Ok, my bad, it does work just fine. I fell in the YML formatting trap :(

To help you debug along the way, use "script/console" and the following statements: - I18n.locale --> should output the locale you want to examine - I18n.t('activerecord.attributes') --> should give you all the key/value pairs for your translation, if not, you made a formatting error in your YML file or it could not be found

And btw - the plugin works quite well http://github.com/iain/i18n_label/ if you don't like the result of ".human_name" (which the plugin uses), just fall back to I18n.t('your key')

like image 65
juwalter Avatar answered Sep 19 '22 19:09

juwalter


Another method:

<%= label_tag :login, User.human_attribute_name(:login) %>
like image 26
Dinatih Avatar answered Sep 19 '22 19:09

Dinatih