Rails i18n not finding my custom error message - ruby-on-rails

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.

Related

Ruby on Rails i18n activerecord custom messages

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'

Translate activerecord attributes

I saw a lot of topics on Stack but still can't find solution.
I had to translate my model attribute so I followed:
Translations for Active Record Models
I want get something like this: "Pozycja nie może być puste" <= "%{attribute} %{message}
attribute => position
message => nie może być puste"
So I built this but it isn't working. How setup this interpolation?
errors:
format: "%{attribute} %{message}"
messages:
blank: "nie może być puste"
activerecord:
models:
user:
attributes:
position: "Pozycja"
The keys hierarchy should be proper,
It should be like this
pl:
errors:
format: "%{attribute} %{message}"
messages:
blank: "nie może być puste"
activerecord:
attributes:
user:
position: "Pozycja"
For more you can always refer rails en.yml

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"

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