Devise Internationalization - ruby-on-rails

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 -.

Related

how to locale field Devise?

I used gem Devise for authentication in my application . I want to change label text for below input
<%= f.input :remember_me, as: :boolean if devise_mapping.rememberable? %>
but I can not find this field for standard renaming either in the service files or in the devise locale
how to locale this field ?
presently
check-box "Remember me"
_
need
check-box "Запомнить меня"
en.yml
en:
activerecord:
attributes:
user:
email: Email
password: Password
remember_me: Remember me
create new file for any other locale and add translated text for that language
de.yml
de:
activerecord:
attributes:
user:
email: "email in de"
password: "Password in de"
remember_me: "Remember me in de"
You can read more about devise translation here
Here is the very useful site for translations - https://www.localeapp.com/projects/377/translations/5250293?in_locale=5291

Rails translation displaying translation name in front of translation string in browser

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"

Can't Customize ActiveRecord Error Message Header

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

Attribute translation in Rails

So the issue I've encountered days ago still makes me unconfortable with my code. I can't get proper translation for my application: Yml looks like that:
pl:
errors: &errors
format: ! '%{attribute} %{message}'
messages:
confirmation: nie zgadza się z potwierdzeniem
activemodel:
errors:
<<: *errors
activerecord:
errors:
<<: *errors
And model looks like that:
module Account
class User < ActiveRecord::Base
attr_accessor: password_confirmation
end
end
And flash in controller declared:
flash[:errors] = #user.errors.full_messages
I tried and read activerecord documentation and stack previous questions (How to use Rails I18n.t to translate an ActiveRecord attribute?, Translated attributes in Rails error messages (Rails 2.3.2, I18N)). Yet it still doesn't work as I wanted.
password_confirmation remains "Password Confirmation", and not "Potwierdzenie hasła" as it shoul be. The screenshot might explain it better: http://i42.tinypic.com/1glz5.png
Your User model is in a namespace, thus you have to declare the namespace in your translation file also. Try the following to get the correct translation for password_confirmation:
pl:
activerecord:
attributes:
account/user:
password_confirmation: "Potwierdzenie hasła"

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