ruby-on-rails-3 – Rails i18n:更改de.errors.format:“%{at
我们对我们的应用程序使用了一些自编写的验证.这些不使用已经翻译的那些,如’空’或’无效’.它们以默认方式“%{attribute}%{message}”格式化
但是,我们的客户要求在“%{attribute}:%{message}.”中格式化它们,只是添加了一些标点符号. 这些是客户i18n中的错误消息: errors: models: foo: attributes: bar: steak_missing: sie haben Ihr Schnitzel vergessen beer_missing: sie haben Ihr Bier vergessen 导致丑陋: Bar sie haben Ihr Schnitzel vergessen 由于这些可以链接,我们需要像这样: Bar: sie haben Ihr Schnitzel vergessen,sie haben Ihr Bier vergessen. 在i18n基础上改变了这个: errors: &errors format: ! "%{attribute}: %{message}." 完全没有效果.也没有完全删除或其他任何东西.我们正在使用formtastic及其semantic_errors,它是否为(默认)错误消息提供了自己的i18n? 解决方法
如果我理解你的问题 – 你在表单中使用这样的东西:
<%= f.semantic_errors :bar %> 要更改semantic_errors行为,您可以修补此方法.要执行此操作,只需添加内容为{RAILS_ROOT} /config/initializers/semantic_errors_patch.rb的文件: Formtastic::Helpers::ErrorsHelper.class_eval do def semantic_errors(*args) html_options = args.extract_options! args = args - [:base] full_errors = args.inject([]) do |array,method| attribute = localized_string(method,method.to_sym,:label) || humanized_attribute_name(method) errors = Array(@object.errors[method.to_sym]).to_sentence errors.present? ? array << [attribute,errors].join(": ") : array ||= [] end full_errors << @object.errors[:base] full_errors.flatten! full_errors.compact! return nil if full_errors.blank? html_options[:class] ||= "errors" template.content_tag(:ul,html_options) do Formtastic::Util.html_safe(full_errors.map { |error| template.content_tag(:li,Formtastic::Util.html_safe(error)) }.join) end end end 这个补丁适用于formtastic 2.2.1和rails 3.2.13. 此补丁将生成两个错误的下一个字符串:
如果有更多错误,它将产生如下内容:
你可以在这一行上改变这种行为: errors = Array(@object.errors[method.to_sym]).to_sentence @ object.errors [method.to_sym] – 是产生最终错误字符串的错误集合. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |