Can't Customize ActiveRecord Error Message Header - ruby-on-rails

For some reason, I can successfully customize the specific error messages in ActiveRecord, but I can't seem to change the error message header.
en:
activerecord:
errors:
template:
header:
one: "Custom message goes here. 1 error prohibited this %{model} from being saved"
other: "Customer message goes here. %{count} errors prohibited this %{model} from being saved"
messages:
blank: "custom :blank message goes here"
models:
complaint: "Complaint"
attributes:
complaint:
city: "Custom city name"
neighborhood: "Custom neighborhood name"
If I leave city and neighborhood blank, I get the following error message:
2 errors prohibited this complaint from being saved:
Custom city name custom :blank message goes here
Custom neighborhood name custom :blank message goes here
For some reason the actual error messages are changed, but the error header is not. In debugging, I changed the YML file to the following, which did not change the default message:
en:
activerecord:
errors:
template:
header:
one: "blah"
other: "blah blah"
body: "blah blah blah"
Does anyone have any idea why this simple change is not working? The only thing I can think of is that the "header" needs to be contextual to the model. Not sure.
References that I used:
[http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models][1]
[http://guides.rubyonrails.org/v2.3.8/i18n.html#translations-for-active-record-models][1]

There seems to be a wrong spacing in your example. Please, check that there's always two spaces as indentation.
If that doesn't fix the problem, check the version of Rails that you're using:
Rails 2.3 example
pt:
activerecord:
errors:
template:
header:
one: "não foi possível guardar este %{model} porque encontramos um erro"
other: "não foi possível guardar este %{model} porque encontramos%{count} erros"
# The variable :count is also available
body: "Encontramos problemas nos seguintes campos:"
# The values :model: "attribute and :value are always available for interpolation
# The value :count is available when applicable. Can be used for pluralization.
messages:
inclusion: "não esta incluído na lista"
exclusion: "está reservado"
invalid: "não é válido"
Rails 3 example
es:
errors: &errors
format: ! '%{attribute} %{message}'
messages:
...
taken: ya está en uso
too_long: es demasiado largo (%{count} caracteres máximo)
too_short: es demasiado corto (%{count} caracteres mínimo)
wrong_length: no tiene la longitud correcta (%{count} caracteres exactos)
template:
body: ! 'Se encontraron problemas con los siguientes campos:'
header:
one: No se pudo guardar este/a %{model} porque se encontró 1 error
other: No se pudo guardar este/a %{model} porque se encontraron %{count} errores
activemodel:
errors:
<<: *errors
activerecord:
errors:
<<: *errors

The problem was that the error message header is actually hard-coded starting in Rails 3.0. From a resource I found:
Look at the app/views/users/_form.html.erb generated by scaffold. The
error message is hard coded. So, changing the locale file has no
effect.
Actually, the Rails 3.0 does not utilize the
activerecord.errors.template.header translation. In the Rails 2.x,
this translation is used in the error_messages method of the form
builder, but this method is deprecated and extracted as a plugin.
If you are curious, do the following:
-rails plugin install git://github.com/joelmoss/dynamic_form.git
-Edit app/views/_form.html.erb as follows:
<%= form_for(#user) do |f| %>
<%= f.error_messages %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Source https://github.com/svenfuchs/rails-i18n/issues/118

Related

i18n dont find the translation

I18n cant find the translation even if the translation is present in my YML,
translation missing: fr.activerecord.errors.models.user.attributes.email.not_found
errors:
messages:
already_confirmed: a déjà été confirmé(e)
confirmation_period_expired: doit être confirmé(e) en %{period}, veuillez en demander un(e) autre
expired: est expiré, veuillez en demander un autre
not_found: n’a pas été trouvé(e)
not_locked: n’était pas verrouillé(e)
not_saved:
one: 'une erreur a empêché ce (cet ou cette) %{resource} d’être enregistré(e) :'
other: "%{count} erreurs ont empêché ce (cet ou cette) %{resource} d’être enregistré(e) :"
It must follows correctly the "pattern", the locale is the root, in this case fr:
fr:
activerecord:
errors:
models:
user:
attributes:
email:
not_found: Email not found

Devise Internationalization

I have setup Devise for my application and have created an fr.yml file in my locales folder in order to get error messages translated.
Here is my fr.yml file at the moment.
fr:
activerecord:
attributes:
client:
password: "Mot de passe"
email: "Email"
password_confirmation: "Confirmation du mot de passe"
remember_me: "Se souvenir de moi"
log_in: "Connection"
errors:
models:
client:
attributes:
password_confirmation:
confirmation: "Confirmation du mot de passe"
(It is pretty sketchy at the moment but I will develop it later on. )
Though a fun thing is happening: when I try to create a new user of the client model and let's say I forget to input the password confirmation, Devise returns the following error :
"Confirmation du mot de passe Confirmation du mot de passe"
It seems the error message is duplicated.
I have removed all French translations for 'password_confirmation' in my fr.yml file and got the following error :
"Password confirmation translation missing: fr.activerecord.errors.models.client.attributes.password_confirmation.confirmation"
Not sure what I can do to get the fr.yml right
I honestly don't know exactly why this is happening but you aren't following Devise localization standards.
Please check devise fr.yml from Devise-i18n project here : https://github.com/tigrish/devise-i18n/blob/master/rails/locales/fr.yml - you don't have to install the gem -.

How to translate default error messages in rails?

This not worked for me when i tried to change error_messages_for messages in translation.yml file:
activerecord:
errors:
template:
header:
one: "Oops error"
other: "Many errors"
body: "There were problems:"
What can i do to translate "1 error prohibited this product from being saved:"? What file contain their text?
I believe you only need the activerecord: part if you're on Rails 2.x. The problem may be that your top level wasn't a language. As of Rails 3.x (which uses the separate dynamic_form plugin now to handle this), the defaults are:
en:
errors:
template:
header:
one: "1 error prohibited this %{model} from being saved"
other: "%{count} errors prohibited this %{model} from being saved"
# The variable :count is also available
body: "There were problems with the following fields:"

Rails: translate ActiveRecord error template headers for a single model

I'm trying to rename the authlogic error messages in a Rails 3 app.
The general format I found out working in Rails 3:
de:
errors:
template:
header:
one: "Konnte {{model}} nicht speichern: ein Fehler."
other: "Konnte {{model}} nicht speichern: {{count}} Fehler."
body: "Bitte überprüfen Sie die folgenden Felder:
But I want to change this for the authlogic user session model (and only for this one) because when the Login fails, the message "Could not save user session" does not make very much sense.
How can I do that?
I had the same problem and I fixed it this way:
Put in your view (like: _form.html.erb)
<div id="error_explanation">
<h2><%= I18n.t('activerecord.errors.template.header', :count => #user.errors.size, :model => #user.class) %></h2>
<h4><%= I18n.t('activerecord.errors.template.body', :count => #user.errors.size) %></h4>
It should work fine!

How to change validation messages on forms

the website I'm developing will be in spanish. Therefore, I'll need the error messages in that language.
I created a file under Configuration directory called 'en.yml' in order to accomplish this. And I added the following code in it:
es:
activerecord:
errors:
models:
announcement:
attributes:
title:
blank: "El título no puede estar vacío."
"El título no puede estar vacío" means "The title cannot be blank".
When I go and run this code I see a message like the following:
"Title El título no puede estar
vacío."
Where "Title" is the field's name. But I don't want it to be displayed. I just want to display the error message I created.
You have to specify the translation after the attribute
es:
activerecord:
models:
announcement: "Anuncio"
attributes:
announcement:
title: "Título" # <= here
errors:
models:
announcement:
attributes:
title:
blank: "no puede estar vacío."
See 5.1 Translations for ActiveRecord Models for more information

Resources