Rails i18n: change de.errors.format: "% {attribute}% {message}" does not affect

We use some proprietary written confirmations for our applications. They do not use one of the ones already translated, such as "empty" or "invalid". They are formed by default "% {attribute}% {message}"

However, our clients require that they be formatted in "% {attribute} : % {message} . ", Only some punctuation added.

These are the error messages in the i18n client:

errors: models: foo: attributes: bar: steak_missing: sie haben Ihr Schnitzel vergessen beer_missing: sie haben Ihr Bier vergessen 

Turning into ugliness:

 Bar sie haben Ihr Schnitzel vergessen 

Since they can be connected by a chain, we should have them like this:

 Bar: sie haben Ihr Schnitzel vergessen, sie haben Ihr Bier vergessen. 

After that, this changed in the i18n database:

  errors: &errors format: ! "%{attribute}: %{message}." 

Shows the effect in general. Neither one nor the other is removed. We use formtastic and semantic_errors , does it provide its i18n error messages for (default)?

+7
source share
1 answer

If I understand your question correctly, you are using something like this in your forms:

 <%= f.semantic_errors :bar %> 

To change the behavior of semantic_errors , you can defuse this method. To do this, simply add the file {RAILS_ROOT}/config/initializers/semantic_errors_patch.rb with the content:

 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 

This patch works well with formtastic 2.2.1 and rails 3.2.13 .

This patch will cause the following line for two errors:

Bar: sie haben Ihr Schnitzel vergessen und sie haben Ihr Bier vergessen.

If there are more errors, this will produce something like:

Amount: not a number, cannot be empty and too short (minimum 2 characters)

You can specify this behavior on this line:

 errors = Array(@object.errors[method.to_sym]).to_sentence 

@object.errors[method.to_sym] is a set of errors that produce the final string of errors .

0
source

All Articles