[英]How to localize a generic error messages partial in Rails 3.2.3?
I am using the following partial to display error messages for most of my models in 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") %>
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
. 在我决定使用
I18n
本地化我的应用程序之前,这非常有效。
I created a new file de.yml
for the German language content which contains this (among many other things): 我为德语内容创建了一个新文件
de.yml
,其中包含以下内容:
# 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. 特别是行
<%= object.class.to_s.underscore.humanize.downcase %>
我感到困惑。 I tried something like <%= t 'activerecord.errors.template.header', :model => object.model_name.human %>
but without any luck. 我尝试了类似
<%= t 'activerecord.errors.template.header', :model => object.model_name.human %>
但是没有任何运气。
Can anybody help? 有人可以帮忙吗?
I read the Rails guide on localization three times already, but I am stuck here. 我已经阅读了三次关于本地化的Rails指南,但是我被困在这里。
Thanks for any help! 谢谢你的帮助!
First of all you should set your locale by this: 首先,您应该通过以下方式设置语言环境:
config.i18n.default_locale = :de
Second, your locale file should look like this: 其次,您的语言环境文件应如下所示:
de:
activerecord:
models:
order: Заказ
attributes:
order:
link: Ссылка на модель часов
name: Имя
phone: Телефон
accept_message: Комментарий к заказу
decline_message: Причина отказа
finish_message: Комментарий к заказу
errors:
models:
order:
attributes:
link:
blank: не может быть пустой
phone:
invalid: должен содержать только цифры. Пример: 9208003020 или 2716070.
blank: не может быть пустым
name:
blank: не может быть пустым
decline_message:
blank: не может быть пустой
OK, I found the answer myself this time, thanks to this thread . 好的,由于这个线程 ,我这次自己找到了答案。
I simply changed my _error_messages.html.rb
partial to: 我只是将
_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>
<% end %>
</ul>
</div>
<% end %>
Now it works and I am happy :-) 现在它可以工作了,我很高兴:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.