【问题标题】:i18n labels for nested models using simple_form使用 simple_form 的嵌套模型的 i18n 标签
【发布时间】:2011-12-02 01:27:06
【问题描述】:

如果是一个项目:

  • has_many :tasks
  • accepts_nested_attributes_for :tasks

我正在使用 simple_form 像这样(简化):

simple_form_for @project do |f|
  f.input :project_name
  f.simple_fields_for :tasks do |j|
    j.input :task_name
  end
  f.submit
end

如何将:task_name 的标签国际化?我在simple_form.it.yml 文件中尝试了很多组合,例如:

it:
  simple_form:
    labels:
      project:
        project_name: 'Nome progetto'
        task:
          task_name:  'Nome compito'

无法在文档中找到示例。 Google 指出了几个明显相关的已解决问题:

https://github.com/plataformatec/simple_form/issues/48

https://github.com/plataformatec/simple_form/issues/194

但到目前为止我很茫然......

谢谢! 朱塞佩

【问题讨论】:

    标签: ruby-on-rails-3 internationalization nested-attributes simple-form


    【解决方案1】:

    只要您的表单接受许多任务,您应该复数 task。它以 simple_form 工作:

    it:
      simple_form:
        labels:
          project:
            project_name: 'Nome progetto'
            tasks:
              task_name:  'Nome compito'
    

    【讨论】:

      【解决方案2】:

      使用默认的 Rails i18n。

      it:
        activerecord:
          attributes:
            task:
              task_name:  'Nome compito'
      

      【讨论】:

      • 在我的情况下它似乎不起作用,但我需要在测试中再次尝试您的建议,剥离应用程序。另请参阅以下内容:stackoverflow.com/questions/4479950/…
      • Simple_form 默认会查找活动记录属性的标签翻译。使用它会更好 - 如果您在表格上使用相同的标签,这样会更好 - 它都在一个地方定义,如果您真的只想在表单上自定义该标签,请仅使用已接受答案的建议。 (我猜它对你不起作用,如果因为你有一些古怪的任务复数,或者!!你正在使用一些服务器或宝石来缓存你的开发 - 最好的测试地方是在控制台中。我一直忘记 rails-dev -boost 缓存我的开发 - 并继续遇到同样的问题。)
      【解决方案3】:

      SimpleForm 应该回退到 it.simple_form.labels.task.task_name,但它没有。

      您可以使用一些 YAML 'hack' 来模拟它。

      it:
        simple_form:
          labels:
            task: &task_labels
              task_name:  'Nome compito'
            project:
              project_name: 'Nome progetto'
              tasks:
                <<: *task_labels
      

      您甚至可以在&lt;&lt;: *task_labels 行之后自定义项目任务的标签。

      【讨论】:

        【解决方案4】:

        如果这有帮助,我会徘徊:

        activerecord.errors.models.[model_name].attributes.[attribute_name]
        activerecord.errors.models.[model_name]
        activerecord.errors.messages
        errors.attributes.[attribute_name]
        errors.messages
        

        【讨论】:

          猜你喜欢
          • 2016-10-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-09
          • 2012-07-09
          • 1970-01-01
          相关资源
          最近更新 更多