I have already modified quite a few option error messages as shown below
en:
activerecord:
attributes:
user:
name: "Name"
email: "E-mail address"
password: "Password"
password_confirmation: "Password confirmation"
errors:
messages:
blank: "%{attribute} is required"
taken: "%{attribute} is already taken"
invalid: "%{attribute} is not valid"
models:
user:
attributes:
password:
confirmation: "%{attribute} doesn't match confirmation"
Yet, I don't know how I can modify the error message for the length when it's not within a specified range. I know it's possible to do so (http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models) but I don't know how?
Thanks,
I found these tags in my app. One of those should work for you!
equal_to: must be equal to %{count}
greater_than: must be greater than %{count}
greater_than_or_equal_to: must be greater or equal to %{count}
less_than: must be less than %{count}
less_than_or_equal_to: must be less or equal than %{count}
too_long:
one: 'is to short (max than: 1 character)'
other: 'is to long (max than: %{count} characteres)'
too_short:
one: 'is to short (min: 1 character)'
other: 'is to short (min: %{count} characteres)'
wrong_length:
one: doesn't have the right length (1 character)
other: doesn't have the right length (%{count} characteres)
other_than: must be different than %{count}
For test reasons i tried:
I18n.locale = "fehler_ausgabe"
puts I18n.translate "activerecord.attributes.diagnosis.sicherheit"
What worked correctly and returned E-mail addresse for this yaml-file:
fehler_ausgabe:
activerecord:
attributes:
diagnosis:
sicherheit: "E-mail addresse"
Next i tried to test a error that is in my model defined like this:
validates :sicherheit, inclusion: { in: ["U","V"]}, unless: :skip_fehler
I changed my yaml file:
fehler_ausgabe:
activerecord:
attributes:
diagnosis:
sicherheit: "E-mail addresse"
inclusion: "muss zugeorndet werden"
I tried again:
puts I18n.translate "activerecord.attributes.diagnosis.sicherheit"
But now somehow get this error:
I18n::InvalidLocaleData (can not load translations from C:/Sites/heroku
2/config/locales/fehler_ausgabe.yml: #<Psych::SyntaxError: (C:/Sites/heroku2/config/locales/fehler_ausgabe.yml):
did not find expected key while parsin
g a block mapping at line 5 column 9>):
What do i wrong? I dont understand why my code now throws a error! THANKS
Your change:
fehler_ausgabe:
activerecord:
attributes:
diagnosis:
sicherheit: "E-mail addresse"
inclusion: "muss zugeorndet werden"
makes your YAML file invalid. You can not have inclusion: "muss zugeorndet werden" if you tend to have sicherheit: "E-mail addresse". Either remove inclusion: "muss zugeorndet werden" or "E-mail addresse" to make your arrangement work.
However, you can checkout this answer to setup inclusion validation properly in your YAML.
There is a piece of some translation file:
errors:
format: "%{attribute} %{message}"
messages:
too_short: "must be greater then ..."
I want that my 'too_short' message must show message with length value from Active Record validation rule. Is it possible? Thanks.
Try this.
errors:
format: "%{attribute} %{message}"
messages:
too_short: "must be greater than %{count}"
I have got a form with a bunch of fields and model validations.
How can I return all possible validation errors that can be raised?
I need it to write locales for all of them.
I want to get a list like this:
password blank
password too_short
password confirmation
login blank
login invalid
email blank
email too_short
email invalid
etc
Basically what Pablo says, except that the page on the rails docs doesn't show how to override the messages for a particular model and field. here's an example from one of my apps:
activerecord:
errors:
full_messages:
format: "{{message}}"
#define standard error messages, which we can overide on per model/per attribute basis further down
messages:
inclusion: "{{attribute}} is not included in the list"
exclusion: "{{attribute}} is reserved"
invalid: "{{attribute}} is invalid"
confirmation: "{{attribute}} doesn't match confirmation"
accepted: "{{attribute}} must be accepted"
empty: "{{attribute}} can't be empty"
blank: "{{attribute}} can't be blank"
too_long: "{{attribute}} is too long (maximum is {{count}} characters)"
too_short: "{{attribute}} is too short (minimum is {{count}} characters)"
wrong_length: "{{attribute}} is the wrong length (should be {{count}} characters)"
taken: "{{attribute}} has already been taken"
not_a_number: "{{attribute}} is not a number"
greater_than: "{{attribute}} must be greater than {{count}}"
greater_than_or_equal_to: "{{attribute}} must be greater than or equal to {{count}}"
equal_to: "{{attribute}} must be equal to {{count}}"
less_than: "{{attribute}} must be less than {{count}}"
less_than_or_equal_to: "{{attribute}} must be less than or equal to {{count}}"
odd: "{{attribute}} must be odd"
even: "{{attribute}} must be even"
record_invalid: "Validation failed: {{errors}}"
models:
quiz:
blank: "{{attribute}} can not be blank"
user_session:
blank: "{{attribute}} can not be blank"
attributes:
login:
invalid: "Please enter your user name"
password:
invalid: "Please note that passwords are case sensitive"
I've also changed the basic format for error messages, as sometimes i didn't want the field name shoved at the start of the message. So, i changed
errors:
format: "{{attribute}} {{message}}"
to
errors:
format: "{{message}}"
Which is why i then specify {{attribute}} in my subsequent errors, to put it back in in most but not all cases.
Note also that i'm using the old syntax of {{var}} rather than %{var}. The same principles apply though.
The latest rails translations are at: rails-i18n.
ActiveRecord errors are under lang:errors and lang:active_record in each .yaml.
Also in your application under config/locales/en.yml is the default.
I am upgrading an application a rails application to 2.3.2 and I am finding that I can't display the default validation error messages for ActiveRecord because I don't have a translation file for it.
This is the error that is reported:
translation missing: en-US, activerecord, errors, template, header
translation missing: en-US, activerecord, errors, template, body
Email translation missing: en-US, activerecord, errors, models, user, attributes, email, taken
Does anyone know where I can find a default English translation file that would include all the strings that the validations might use?
This happened because my language setting was 'en-US' and not 'en'. There are translation files under activerecord/lib/locale. I copied these translations into a new file en_US.yml.
"en-US":
activerecord:
errors:
template:
body: There were problems with the following fields
header:
one: 1 error prohibited this {{model}} from being saved
other: "{{count}} errors prohibited this {{model}} from being saved"
messages:
inclusion: "is not included in the list"
exclusion: "is reserved"
invalid: "is invalid"
confirmation: "doesn't match confirmation"
accepted: "must be accepted"
empty: "can't be empty"
blank: "can't be blank"
too_long: "is too long (maximum is {{count}} characters)"
too_short: "is too short (minimum is {{count}} characters)"
wrong_length: "is the wrong length (should be {{count}} characters)"
taken: "has already been taken"
not_a_number: "is not a number"
greater_than: "must be greater than {{count}}"
greater_than_or_equal_to: "must be greater than or equal to {{count}}"
equal_to: "must be equal to {{count}}"
less_than: "must be less than {{count}}"
less_than_or_equal_to: "must be less than or equal to {{count}}"
odd: "must be odd"
even: "must be even"
Then I just added my custom strings after these.
You can avoid copying and pasting the standard translations by telling I18n to fallback to :en when it can't find a translation in :en_US. The example initializer below shows how we did it for falling back from 'en-GB' and 'it-IT' to the standrd 'en', throwing in pluralizations for good measure
# config/initializer/i18n.rb
I18n.backend = I18n::Backend::Chain.new(I18n.backend)
I18n::Backend::Chain.send(:include, I18n::Backend::Fallbacks)
I18n.fallbacks[:'en-GB'] << :en
I18n.fallbacks[:'it-IT'] << :en
require "i18n/backend/pluralization"
I18n::Backend::Chain.send(:include, I18n::Backend::Pluralization)
FYI, if you are including these in the old style hash format, use this;
:activerecord => {
:errors => {
:template => {
:body => 'There were problems with the following fields',
:header => {
:one => '1 error prohibited this {{model}} from being saved',
:other => "{{count}} errors prohibited this {{model}} from being saved"
}
},
:messages => {
:inclusion => "is not included in the list",
:exclusion => "is reserved",
:invalid => "is invalid",
:confirmation => "doesn't match confirmation",
:accepted => "must be accepted",
:empty => "can't be empty",
:blank => "can't be blank",
:too_long => "is too long (maximum is {{count}} characters)",
:too_short => "is too short (minimum is {{count}} characters)",
:wrong_length => "is the wrong length (should be {{count}} characters)",
:taken => "has already been taken",
:not_a_number => "is not a number",
:greater_than => "must be greater than {{count}}",
:greater_than_or_equal_to => "must be greater than or equal to {{count}}",
:equal_to => "must be equal to {{count}}",
:less_than => "must be less than {{count}}",
:less_than_or_equal_to => "must be less than or equal to {{count}}",
:odd => "must be odd",
:even => "must be even"
}
}
}