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

给你一囗甜甜゛ 提交于 2019-12-09 16:55:40

问题


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!


回答1:


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')




回答2:


Another method:

<%= label_tag :login, User.human_attribute_name(:login) %>



回答3:


You should upgrade Rails gem to v2.3.11 (I tried to use 2.3.9, but now days it is not available so I suggest you 2.3.11)!

gem install -v=2.3.11 rails

You can find this issues documented here: Rails 2.3.9 Release notes



来源:https://stackoverflow.com/questions/1863526/how-to-use-rails-i18n-t-to-translate-an-activerecord-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!