如何在 Rails 3.2.3 中本地化通用错误消息部分? [英] How to localize a generic error messages partial in Rails 3.2.3?

查看:42
本文介绍了如何在 Rails 3.2.3 中本地化通用错误消息部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rails 3.2.3 中使用以下部分来显示大多数模型的错误消息:

# _error_messages.html.erb<% if object.errors.any?%><div id="error_explanation"><h3><%=pluralize(object.errors.count, "error") %>禁止这种<%= object.class.to_s.underscore.humanize.downcase %>从被保存:</h3><p>以下字段存在问题:</p><ul><% object.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>

<%结束%>

这很有效,直到我决定使用 I18n 本地化我的应用程序.

我为德语内容创建了一个新文件 de.yml,其中包含以下内容(以及许多其他内容):

# de.yml错误:&错误格式: !'%{attribute} %{message}'消息:空白:muss ausgefüllt werden模板:身体: !'Bitte überprüfen Sie die folgenden Felder:'标题:一: !'Konnte %{model} nicht speichern: ein Fehler.其他: !'Konnte %{model} nicht speichern: %{count} Fehler.等等等等等等

现在如何在错误消息中使用此内容?

特别是 <%= object.class.to_s.underscore.humanize.downcase %> 这行令我感到困惑.我尝试了类似 <%= t 'activerecord.errors.template.header', :model =>object.model_name.human %> 但没有任何运气.

有人可以帮忙吗?

我已经阅读了关于本地化的 Rails 指南三遍,但我被困在这里.

感谢您的帮助!

解决方案

好的,这次我自己找到了答案,感谢这个线程.

我只是将我的 _error_messages.html.rb 部分更改为:

<% if object.errors.any?%><div id="error_explanation"><h3><%=t('errors.template.header',:model=>object.class.model_name.human,:count=>object.errors.count)%></h3><p><%=t('errors.template.body')%></p><ul><% object.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>

<%结束%>

现在它起作用了,我很高兴:-)

I am using the following partial to display error messages for most of my models in Rails 3.2.3:

# _error_messages.html.erb

<% if object.errors.any? %>
<div id="error_explanation">
    <h3><%= pluralize(object.errors.count, "error") %>
    prohibited this <%= object.class.to_s.underscore.humanize.downcase %> from being saved:</h3>        
    <p>There were problems with the following fields:</p>
    <ul>
        <% object.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
        <% end %>
    </ul>
</div>
<% end %>

This worked great until I decided to localize my application using I18n.

I created a new file de.yml for the German language content which contains this (among many other things):

# de.yml

errors: &errors
  format: ! '%{attribute} %{message}'
  messages:
    blank: muss ausgefüllt werden
  template:
    body: ! 'Bitte überprüfen Sie die folgenden Felder:'
    header:
      one: ! 'Konnte %{model} nicht speichern: ein Fehler.'
      other: ! 'Konnte %{model} nicht speichern: %{count} Fehler.'

etc. etc. etc.

Now how can I use this content in my error messages?

Especially the line <%= object.class.to_s.underscore.humanize.downcase %> puzzles me. I tried something like <%= t 'activerecord.errors.template.header', :model => object.model_name.human %> but without any luck.

Can anybody help?

I read the Rails guide on localization three times already, but I am stuck here.

Thanks for any help!

解决方案

OK, I found the answer myself this time, thanks to this thread.

I simply changed my _error_messages.html.rb partial to:

<% if object.errors.any? %>
<div id="error_explanation">
    <h3><%= t('errors.template.header', :model => object.class.model_name.human, :count => object.errors.count) %></h3>
    <p><%= t('errors.template.body') %></p>
    <ul>
        <% object.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
        <% end %>
    </ul>
</div>
<% end %>

Now it works and I am happy :-)

这篇关于如何在 Rails 3.2.3 中本地化通用错误消息部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆