How to translate default error messages in rails? - ruby-on-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:"

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'

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"

Rails I18n Pluralization Error

I wanted to set a custom error message for when an email was already taken so I edited the config/locales/en.yml file. It looked like this:
en:
activerecord:
models:
user:
email:
taken: "already being used"
When I submit the form with email in it, I get this error:
translation data {:email=>{:taken=>"already being used"}} can not be used with :count => 1
I've only just started looking into i18n so this may be a really simple mistake but I can't find an answer.
Try:
en:
activerecord:
errors:
models:
user:
attributes:
email:
taken: "already being used"
See section 5.1.1 in Rails Guide.
Hope this helps!

What does the percent sign mean in Yaml?

I noticed that in the i18n rails guide, they have the following code:
en:
activerecord:
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:"
What do the %{...} mean? How is this different from #{...}?
If you read the guide a little more, you'll come across the Passing variables to translations section:
You can use variables in the translation messages and pass their values from the view.
# app/views/home/index.html.erb
<%=t 'greet_username', :user => "Bill", :message => "Goodbye" %>
# config/locales/en.yml
en:
greet_username: "%{message}, %{user}!"
and the Interpolation section:
In many cases you want to abstract your translations so that variables can be interpolated into the translation. For this reason the I18n API provides an interpolation feature.
All options besides :default and :scope that are passed to #translate will be interpolated to the translation:
I18n.backend.store_translations :en, :thanks => 'Thanks %{name}!'
I18n.translate :thanks, :name => 'Jeremy'
# => 'Thanks Jeremy!'
So the %{...} stuff doesn't have anything to do with YAML, that's I18n's way of handling variable interpolation in the messages.

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!

Resources