如何使用 Rails I18n.t 转换 ActiveRecord 属性? [英] How to use Rails I18n.t to translate an ActiveRecord attribute?
问题描述
尝试在我的视图中使用我在 config/locales/de.yml 中的活动记录翻译.我认为我很聪明地使用了这个:
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"
在我看来:
<%= label_tag :login, t('activerecord.attributes.user.login') %>
但我得到的不是翻译值(Benutzerkennung")"翻译缺失:de、activerecord、属性、用户、登录"
But instead of the translation value ("Benutzerkennung") I am getting the infamous "translation missing: de, activerecord, attributes, user, login"
有没有人得到这个工作(不使用标签翻译插件(我担心潜在的副作用)或 User.humanize_attribute_name)?我错过了什么?(当我使用activerecord1"或除 activerecord 之外的其他东西时它确实有效,所以我的设置似乎很好)
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)
谢谢!
推荐答案
好吧,我的错,它确实工作得很好.我掉进了 YML 格式的陷阱 :(
Ok, my bad, it does work just fine. I fell in the YML formatting trap :(
为了帮助您进行调试,请使用脚本/控制台"和以下语句:- I18n.locale --> 应该输出你想检查的语言环境- I18n.t('activerecord.attributes') --> 应该为您提供翻译的所有键/值对,如果没有,您在 YML 文件中出现格式错误或找不到它
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
顺便说一句 - 该插件运行良好 http://github.com/iain/i18n_label/如果您不喜欢.human_name"(插件使用)的结果,只需回退到 I18n.t('your key')
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')
这篇关于如何使用 Rails I18n.t 转换 ActiveRecord 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!