如何使用Rails I18n.t转换ActiveRecord属性? [英] How to use Rails I18n.t to translate an ActiveRecord attribute?

查看:48
本文介绍了如何使用Rails I18n.t转换ActiveRecord属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的视图中,也尝试在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆