Rails I18N:缩短翻译电话(至少在视图中)
-
25-10-2019 - |
题
翻译某些字段的干燥方法是什么?
在我的宁静观点中,我有一些重复的片段,例如在节目视图中:
...
<dt><%= t("activerecord.attributes.user.firstname") %></dt>
<dd><%= @user.firstname %></dd>
...
现在,而不是写作 t("activerecord.attributes.user.attr_name")
一遍又一遍地,我只想写 t(:attr_name)
(如同 f.label :firstname
在表单视图中)。
基本上,这不是一个问题(至少对于静止视图),因为I18N模块可以查询 controller
推断型号,然后猜测正确的翻译字符串的方法。
我的问题:有人对这种方法有实践经验吗?甚至可以有红宝石吗?或者:我没想到吗?
解决方案
我似乎 ActiveModel::Translation#human_attribute_name
做到了(例如 <%= User.human_attribute_name :firstname %>
).
不隶属于 StackOverflow