针对特定模型的Rails提交按钮国际化(I18n)

40

我发现Rails允许通过在config/locales/en.yml中使用以下内容对提交按钮进行通用化的i18n翻译:

en:
  helpers:
    submit:
      create: "Create %{model}"
      submit: "Save %{model}"
      update: "Update %{model}"

然而,我只想为一个具体的模型更新create的值。我希望文本显示为“上传%{model}”或者仅为“上传”。如何仅针对一个模型(例如:Photo 模型)进行更改?

2个回答

58
Those labels can be customized using I18n under the +helpers.submit+ key 
and using %{model} for translation interpolation:

  en:
    helpers:
      submit:
        create: "Create a %{model}"
        update: "Confirm changes to %{model}"

It also searches for a key specific to the given object:

  en:
    helpers:
      submit:
        post:
          create: "Add %{model}"

源代码 @ actionview/lib/action_view/helpers/form_helper.rb


10
如果您使用,Rails服务器将在控制台上打印翻译查找尝试的信息,例如:i18n-debug gem
[i18n-debug] en.helpers.submit.post.create => nil

6
这个答案提到的 rails-18n-debug gem 很好,但已经过时了,作者现在建议使用替代 gem,即 https://github.com/fphilipe/i18n-debug。 - DannyB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接