Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate active record model validations

When I submit a form with an error in it, it returns an error message. How can I translate these error messages with i18n? I already have translation for all the other texts in my views, so I know how l18n works in Rails. I now get this:

2 errors prohibited this user from being saved:

Email translation missing: nl.activerecord.errors.models.user.attributes.email.blank
Email translation missing: nl.activerecord.errors.models.user.attributes.email.blank

I want to translate both the title and the errors.

like image 606
John Avatar asked Oct 07 '11 10:10

John


People also ask

What is Active Record validations?

Active Record offers many pre-defined validation helpers that you can use directly inside your class definitions. These helpers provide common validation rules. Every time a validation fails, an error is added to the object's errors collection, and this is associated with the attribute being validated.

Can we save an object in DB if its validations do not pass?

Some methods will trigger validations, but some will not. This means that it's possible to save an object in the database in an invalid state if you aren't careful.

How does validation work in Rails?

Before saving an Active Record object, Rails runs your validations. If these validations produce any errors, Rails does not save the object. After Active Record has performed validations, any errors found can be accessed through the errors instance method, which returns a collection of errors.


3 Answers

The translation for the title would be:

nl:
  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:"

For translating the error messages, Rails will use the following order of translations:

activerecord.errors.models.user.attributes.name.blank
activerecord.errors.models.user.blank
activerecord.errors.messages.blank
errors.attributes.name.blank
errors.messages.blank

So you could add:

nl:
  activerecord:
    errors:
      models:
        user:
          attributes:
            email:
              blank: "foo blank in nl bar baz"

It's documented in the Rails Internationalization (I18n) API Guide, which might give you some more insight.

like image 158
tbuehlmann Avatar answered Oct 23 '22 03:10

tbuehlmann


Just use the Dutch translations file that you can find here. It contains translations for most (if not all) ActiveRecord validation messages.

Copy the file to config/locales/ in your project.

Alternate method

If you want to benefit from updated translations, add the following to your Gemfile instead of copying the translation files by hand:

gem 'rails-i18n'
like image 27
rdvdijk Avatar answered Oct 23 '22 04:10

rdvdijk


The rails I18n guide covers this pretty well.

You can put the following in config/locales/nl.yml to translate the attributes:

nl:
  activerecord:
    models:
      user: "User"
    attributes:
      email: "Email"

For the error messages, ActiveRecord will look them up in the following namespaces:

activerecord.errors.models.[model_name].attributes.[attribute_name]
activerecord.errors.models.[model_name]
activerecord.errors.messages
errors.attributes.[attribute_name]
errors.messages

model, attribute and value are interpolated and available in your translations, for example:

nl:
  errors:
    messages:
      blank: "Please fill the %{model}'s %{attribute}"
like image 14
Benoit Garret Avatar answered Oct 23 '22 03:10

Benoit Garret