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
Related
I have a ruby on rails application. I have spanish and english support in my application. However, I get a translation missing exception in case spanish mode.
I have the following model:
class Company < ApplicationRecord
validates :name, length: { in: 5..15, message: :bad_name }
end
en.yml
en:
activerecord:
errors:
models:
company:
attributes:
name:
bad_name: 'message in english'
sp.yml
sp:
activerecord:
errors:
models:
company:
attributes:
name:
bad_name: 'message in spanish'
Just in case the error when I open the application in english. I get the "message in english" message and that's ok.
On the other hand, when I open and test it in spanish, I get the following error.
ActiveRecord::RecordInvalid:
translation missing: sp.activerecord.errors.messages.record_invalid
I can't see what I'm missing,
Any suggestions,
Thanks.
Are other translations work in this file?
Try to rename sp.yml to es.yml, because this is the iso-code for "espanol".
Also try to include a message for activerecord.errors.messages.record_invalid in your spansih file. will this be displayed?
Actually, to use Spanish within your locale you would need to set I18n.locale = :es. I think you mixed up your locales. The correct language file should be named es.yml and look like:
es:
activerecord:
errors:
models:
company:
attributes:
name:
bad_name: 'message in spanish'
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 -.
In my locale file, there is the following translation:
de:
activerecord:
errors:
models:
user:
attributes:
email:
taken: "Die E-Mail Adresse wird bereits benutzt."
When i open the desired page in my browser, the error message looks like the following:
Email Die E-Mail Adresse wird bereits benutzt.
So does anybody know why there is another "Email" in front of the translated string?
The correct yml structure should be:
de:
activerecord:
models:
user: Dude
attributes:
user:
email: "mail"
errors:
template:
header:
one: "1 error prohibited this %{model} from being saved"
other: "%{count} errors prohibited this %{model} from being saved"
body: "There were problems with the following fields:"
errors:
format: ! '%{attribute} %{message}'
messages:
taken: "Email Die E-Mail Adresse wird bereits benutzt."
Note that there are two "errors" keys, one inside activerecord and another one outside. Use the later for validation messages.
You could get a more full detailed translation file from https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml
You might find the devise translation file at https://github.com/mcasimir/devise-i18n-views/blob/master/locales/en.yml
For my projects, I usually have several translation files for each language:
rails.en.yml: messages used by rails (inspired by the svenfuchs file)
devise.en.yml: messages related to authentication (from the devise project itself)
en.yml: messages I create on my views that doesn't belong to other gems (gems like "simple_form" usually have also their own file)
Edit
From the Rails Internationalization guide, validation messages will be searched in this order:
activerecord.errors.models.user.attributes.name.blank
activerecord.errors.models.user.blank
activerecord.errors.messages.blank
errors.attributes.name.blank
errors.messages.blank
So it's correct to use what you posted on the question:
de:
activerecord:
errors:
models:
user:
attributes:
email:
taken: "http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models"
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
What's wrong with my locale:
I'm getting:
Hotel translation missing: pt-BR.activemodel.errors.models.hotel_selector.attributes.id.must_be_filled
And my yml is like this:
pt-BR:
activemodel:
attributes:
csv_invite:
file_path: Arquivo
group_id: Grupo
hotel_selector:
id: Hotel
errors:
models:
hotel_selector:
id:
must_be_filled: deve ser preenchido
What am I doing wrong?
I forgot the attributes key.
Silly me.