2016-07-26 46 views
0

我试着翻译了以下错误:的Rails 3.2.21国际化 - 翻译错误的问题

error

,它说:

1 error prohibited this project from being saved:

这是我在我的恩我从Rails Internationalization (I18n) API获得的yml文件:

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" 

这是我在我的形式显示我的错误的方式

<% 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在我的YML文件,或做什么,我需要改变,这样,当我更改为西班牙语

1 error prohibited this project from being saved:

更改

Un error prohibido este proyecto de ser salvado


更新

这是我最终使用:

在我.yml文件

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

,并在我的形式:

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

回答

1

此消息

1 error prohibited this project from being saved: 

在支架被硬编码在这里

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

,你正在寻找在指南的部分引用旧版本使用error_messages_for方法轨 。

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'.

我不会推荐这个选项,因为那个宝石没有被维护。

+0

哇我怎么没有注意到...我很尴尬 –

1

你没有使用模板助手(#error_messages_for),它会利用YAML结构。该帮手在Rails 3之前被提取出来,您必须安装dynamic_form gem才能使用它。

你基本上是在助手在你的第二个片段中做同样的事情。要使用它,你可以用<%= error_messages_for @project %>代替整个事物,但我不知道你是否最终得到了你想要的HTML标记。要直接回答您的问题,您应该在您提供的链接上显示相同的YML结构。当然,这必须在你的西班牙洋芋上。

我认为更改第二个片段以转换错误标头消息更容易。

如果您不关心将您的应用翻译成其他语言,或者使用国际化助手(2)进行操作,则可以在就地(1)上执行该操作,在西班牙语I18n文件中添加并输入错误。

1.

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

2.

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

对于这最后的办法,翻译添加到您的YML文件。

es: 
    error_header_message: prohibido este proyecto de ser salvado