rails-i18n不适用于头错误

时间:2013-12-19 07:49:45

标签: ruby-on-rails ruby-on-rails-4 rails-i18n

我正在使用rails-i18n gem:hr用作我的主要语言。

宝石有效但header message没有。

4 errors prohibited this list from being saved:部分)

这是我在提交包含无效属性的表单时得到的结果:

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

至于代码,我只将config.i18n.default_locale = :hr添加到config/application.rb

在文档中说:

  

以下区域设置已完成:

     

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

使用其他区域设置进行测试,但仍未翻译4 errors prohibited this list from being saved:部分。

我做错了什么或.yml文件中缺少翻译?

注意:我使用rails 4.0.0

更新

<%= 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 %>

2 个答案:

答案 0 :(得分:7)

由于某些字段明确翻译,我认为您必须查找“禁止错误...”消息。此消息应该在您的activerecord.hr.yml文件中,并且应该看起来像

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

(但接着用你的语言)

现在在视图中更改代码

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

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

为了将Report.model_name.human中提到的“Report”翻译成匈牙利语,您应该在hr.yml文件中添加一些内容,如

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

那应该给你完全的灵活性......

答案 1 :(得分:2)

您可以尝试更改<%= pluralize(@report.errors.count, "error") %> prohibited this list from being saved: <%= t( :errors, :count => @report.errors.count, :model => t("model.Report")) %>,以获取以下地点的模型和错误

hr:
  model:
    Report: "Report"
  errors:
    one: "%{model} can't be saved... 1 error custom message."
    other: "%{model} can't be saved for %{count} custom errors"