I18n用于特定于模型的Rails提交按钮

Mat*_*ins 28 ruby-on-rails internationalization

我发现Rails通过以下方式允许通用的i18n提交按钮config/locales/en.yml:

en:
  helpers:
    submit:
      create: "Create %{model}"
      submit: "Save %{model}"
      update: "Update %{model}"
Run Code Online (Sandbox Code Playgroud)

但是,我希望create仅为一个特定型号更新该值.我希望文本显示为"上传%{model}"或只是"上传".如何仅针对一个模型(例如:Photo模型)进行此更改?

Chr*_*erg 45

源代码看,您应该能够这样做:

en:
  helpers:
    submit:
      post:
        create: "Upload %{model}"
Run Code Online (Sandbox Code Playgroud)

post模型的名称在哪里.该在线文档还提到,你可以做到这一点.


Jas*_*son 9

如果您使用i18n-debug gem,则rails服务器将打印对控制台的翻译查找尝试,例如:

[i18n-debug] en.helpers.submit.post.create => nil
Run Code Online (Sandbox Code Playgroud)

  • 这个答案链接到的rails-18n-debug gem很不错,但已弃用,作者现在建议在https://github.com/fphilipe/i18n-debug使用替代gem (5认同)