【问题标题】:Rails 3.2.21 Internationalization - translating errors issueRails 3.2.21 国际化 - 翻译错误问题
【发布时间】:2016-11-30 12:55:31
【问题描述】:

我正在尝试翻译以下错误:

上面写着:

1 个错误禁止保存此项目:

这是我从 Rails Internationalization (I18n) API 获得的 en 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 %>

我几乎没有接触过 Rails 的本地化,所以如果我做的一切都完全错误,我很抱歉,但是我需要在我的 yml 文件中的[?]s 中添加什么,或者我需要更改什么,以便当我更改为西班牙语时

1 个错误禁止保存此项目:

改成

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

【问题讨论】:

    标签: ruby-on-rails ruby localization yaml


    【解决方案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 方法的rails。

    4.5.3 Active Record error_messages_for Helper 的翻译

    如果您使用 Active Record error_messages_for 助手,您将 想为其添加翻译。

    为了使用这个帮助程序,您需要安装 DynamicForm gem,方法是在您的 Gemfile 中添加以下行:gem 'dynamic_form'。

    但我不推荐此选项,因为该 gem 未维护。

    【讨论】:

    • 哇,我怎么没注意到...我很尴尬
    【解决方案2】:

    您没有使用可以利用该 YAML 结构的模板 Helper(#error_messages_for)。该助手是在 Rails 3 之前提取的,您必须安装 dynamic_form gem 才能使用它。

    基本上你在做的事情和助手在你的第二个 sn-p 上做的事情是一样的。要使用它,您可以将整个内容替换为:&lt;%= error_messages_for @project %&gt;,但我不知道您最终是否会获得您想要的 HTML 标记。要直接回答您的问题,您应该在您提供的链接上保持相同的 YML 结构。当然,那必须在您的西班牙语 yaml 中。

    我认为更改第二个 sn-p 以翻译错误标头消息更容易。

    如果您不关心将应用翻译成其他语言,您可以就地执行 (1),或者使用国际化帮助程序 (2) 执行此操作,在西班牙语 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
    

    【讨论】:

      猜你喜欢
      • 2011-02-08
      • 2014-09-24
      • 1970-01-01
      • 1970-01-01
      • 2019-06-08
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多