Rails 中操作按钮的 I18n 翻译的哪个 yaml 标签?
Which yaml tag for I18n translation of action buttons in Rails?
在我的 RoR 5.2 应用程序中,我为特定操作创建了按钮:
<%= button_to t('MakeCurrent'), make_current_business_rule_path(@business_rule.id), class: "buttons mid_menu" %>
我试图在助手 yaml 文件中插入翻译后的标签:
en:
helpers:
submit:
business_area:
create: "Create Business Area"
update: "Update Business Area"
MakeCurrent: "Activate this version"
它适用于作为标准表单一部分的创建和更新标签,但不适用于传递给 button_to 方法的 MakeCurrent 标签。哪个是 yaml 结构中的正确入口点?
非常感谢!
这不是 I18n 查找的工作方式。表单助手将自动使用范围键,因为这是一个有限的用例。如果 Rails 只是胡乱猜测任意按钮与提交表单是一回事,那将是非常令人惊讶的,而且坦率地说是愚蠢的。
如果您想使用该键,您可以使用完整路径或将范围传递给 I18n.t
:
t('helpers.submit.business_area.MakeCurrent')
t('MakeCurrent', scope: 'helpers.submit.business_area')
如果您想根据模型进行动态查找,您可以使用 ActiveModel::Naming 获取翻译键:
t('MakeCurrent', scope: [:helpers, :submit, @object.class.model_name.i18n_key])
您还应该在 snake_case 中命名您的密钥,以遵循最少意外的原则。
在我的 RoR 5.2 应用程序中,我为特定操作创建了按钮:
<%= button_to t('MakeCurrent'), make_current_business_rule_path(@business_rule.id), class: "buttons mid_menu" %>
我试图在助手 yaml 文件中插入翻译后的标签:
en:
helpers:
submit:
business_area:
create: "Create Business Area"
update: "Update Business Area"
MakeCurrent: "Activate this version"
它适用于作为标准表单一部分的创建和更新标签,但不适用于传递给 button_to 方法的 MakeCurrent 标签。哪个是 yaml 结构中的正确入口点?
非常感谢!
这不是 I18n 查找的工作方式。表单助手将自动使用范围键,因为这是一个有限的用例。如果 Rails 只是胡乱猜测任意按钮与提交表单是一回事,那将是非常令人惊讶的,而且坦率地说是愚蠢的。
如果您想使用该键,您可以使用完整路径或将范围传递给 I18n.t
:
t('helpers.submit.business_area.MakeCurrent')
t('MakeCurrent', scope: 'helpers.submit.business_area')
如果您想根据模型进行动态查找,您可以使用 ActiveModel::Naming 获取翻译键:
t('MakeCurrent', scope: [:helpers, :submit, @object.class.model_name.i18n_key])
您还应该在 snake_case 中命名您的密钥,以遵循最少意外的原则。