Search code examples
ruby-on-railsruby-on-rails-4rails-i18n

rails-i18n not working for header error


I'm using the rails-i18n gem to use :hr for my main language.

The gem works but for header message it doesn't.

(The 4 errors prohibited this list from being saved: part)

This is what I get when I submit the form with invalid atributes:

4 errors prohibited this list from being saved:             #doesn't translate
    Field1 ne smije biti prazan                              #translates/presence
    Field2 ne smije biti prazan                              #translates/presence
    Field3 ne smije biti prazan                              #translates/presence
    Filed4 nije odgovarajuće duljine (treba biti 11 znakova) #translates/length

As for the code, I only added config.i18n.default_locale = :hr to config/application.rb.

In the documentation in says:

Following locales are complete:

bs, da, en, en-US, es-PA, hr, is, ja, nl, sr, ur, zh-HK

Tested with other locales but is still doesn't translate the 4 errors prohibited this list from being saved: part.

Am I doing something wrong or there are translations missing in the .yml files ?

Note: I use rails 4.0.0

Update:

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

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

  <div class="form-container">

    <div class="inline half">
      <div class="field">
        <%= f.label :field1 %><br>
        <%= f.text_field :field1 %>
      </div>
      <div class="field">
        <%= f.label :field2 %><br>
        <%= f.text_field :field2 %>
      </div>
      <div class="field">
        <%= f.label :field3 %><br>
        <%= f.text_field :field3 %>
      </div>
      <div class="field">
        <%= f.label :field4 %><br>
        <%= f.text_field :field4 %>
      </div>
    </div>
    <div class="inline half">

      <div class="actions">
        <%= f.submit "Create", class: "continue-button" %>
      </div>
    </div>
  </div>
<% end %>

Solution

  • As some fields clearly translate, I think you have to look for the "errors prohibited..." message. This message should be in your activerecord.hr.yml file, and should look like

    hr:
      errors:
        messages:
          not_saved:
            one: '1 error prohibited this %{resource} from being saved:'
            other: '%{count} errors prohibited this %{resource} from being saved:'
    

    (but then in your language)

    Now change in your view the code

    = pluralize(@report.errors.count, "error") %> prohibited this list from being saved:
    

    into

    t('errors.messages.not_saved', count: @report.errors.count, resource: Report.model_name.human)
    

    In order to get the "Report", mentioned in Report.model_name.human translated into Hungarian, you should add something to your hr.yml file like

    activerecord:
      models:
        report: Translation of report in Hungarian
        reports: Translation of reports in Hungarian
    

    That should give you complete flexibility...