2016-07-26 85 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