Santi Gallego
Santi Gallego

Reputation: 202

Rails 3.2.21 Internationalization - translating errors issue

I'm trying to translate the following error:

error

where it says:

1 error prohibited this project from being saved:

this is what i have in my en yml file that I got from Rails Internationalization (I18n) API:

en:
  activerecord:
    errors:
      [?]:
        [?]:
          one:   "1 error prohibited this %{model} from being saved"
          other: "%{count} errors prohibited this %{model} from being saved"
        [?]:    "There were problems with the following fields:"
      messages:
        record_invalid: "Validation failed: %{errors}"
        restrict_dependent_destroy:
          has_one: "Cannot delete record because a dependent %{record} exists"
          has_many: "Cannot delete record because dependent %{record} exist"

And this is an example of the way I display my errors in my forms

<% if @project.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>

    <ul>
      <% @project.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %>

I haven't hardly touched localization with rails at all, so I'm sorry if I'm doing everything completely wrong, but what do I need to put in the [?]s I have in my yml file, or what do I need to change so that when I change to spanish the

1 error prohibited this project from being saved:

changes to

Un error prohibido este proyecto de ser salvado


Update

This is what I ended up using:

In my .yml file

en:
  activerecord:
    form_errors:
      one: One error prohibited this %{model} from being saved
      other: "%{count} errors prohibited this %{model} from being saved"

and in my forms:

    <% if @project.errors.any? %>
        <div id="error_explanation">
      <h2><%= t('activerecord.form_errors', :count => @project.errors.count,
                       :model => t('models.project').downcase) %></h2>

          <ul>
            <% @project.errors.full_messages.each do |msg| %>
              <li><%= msg %></li>
            <% end %>
          </ul>
        </div>
      <% end %>

Upvotes: 0

Views: 388

Answers (2)

Alexandre Angelim
Alexandre Angelim

Reputation: 6753

You're not using the template Helper(#error_messages_for) which would take advantage of that YAML structure. That helper was extracted out before Rails 3 and you'd have to install the dynamic_form gem to use it.

You're basically doing the same thing the helper does on your second snippet. To use it you could replace the whole thing with: <%= error_messages_for @project %>, but I don't know if you'd end up getting the HTML markup you intended. To answer your question directly, you should keep the same YML structure displayed on the link you provided. That would have to be on your spanish yaml, of course.

I think it's easier to change your second snippet to translate the error header message.

You can do it inplace(1) if you don't care about translating your app to other languages, or do it using the internationalization helper(2), adding and entry for the error in your spanish I18n file.

1.

<%= pluralize(@project.errors.count, "error") %> prohibido este proyecto de ser salvado:</h2>

2.

<%= pluralize(@project.errors.count, "error") %> <%= t(:error_header_message) %>:</h2>

For this last approach, add the translation to your yml file.

es:
  error_header_message: prohibido este proyecto de ser salvado

Upvotes: 1

nikkypx
nikkypx

Reputation: 2005

This message

1 error prohibited this project from being saved:

is hardcoded in the scaffold right here

<h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>

That section of the guide you are looking at is referencing older versions of rails using the error_messages_for method.

4.5.3 Translations for the Active Record error_messages_for Helper

If you are using the Active Record error_messages_for helper, you will want to add translations for it.

In order to use this helper, you need to install DynamicForm gem by adding this line to your Gemfile: gem 'dynamic_form'.

I wouldn't recommend this option though because that gem is not maintained.

Upvotes: 1

Related Questions