【问题标题】:I18n for model-specific Rails submit buttonI18n 用于特定于模型的 Rails 提交按钮
【发布时间】:2012-11-12 21:14:02
【问题描述】:

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

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

但是,我希望仅为一种特定型号更新 create 值。我希望文本读作“上传 %{model}”或只是“上传”。如何仅对一个模型进行此更改(例如:Photo 模型)?

【问题讨论】:

    标签: ruby-on-rails internationalization rails-i18n


    【解决方案1】:
    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}"
    

    Source @ actionview/lib/action_view/helpers/form_helper.rb

    【讨论】:

      【解决方案2】:

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

      [i18n-debug] en.helpers.submit.post.create => nil
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多