如何在Rails 3.2.3中本地化一般错误消息?
我使用以下部分显示Rails 3.2.3中我的大多数模型的错误消息:
# _error_messages.html.erb prohibited this from being saved:
There were problems with the following fields:
这很有效,直到我决定使用I18n
本地化我的应用程序。
我为德语内容创建了一个新文件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.
现在我如何在错误消息中使用此内容?
特别是这一行让我很困惑。 我试过像
object.model_name.human %>
但没有任何运气。
有人可以帮忙吗?
我已经三次阅读有关本地化的Rails指南,但我被困在这里。
谢谢你的帮助!
首先,您应该通过以下方式设置您的语言环境:
application.rb中
config.i18n.default_locale = :de
其次,您的语言环境文件应如下所示:
de.yml
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: не может быть пустой
好的,这次我找到了答案,感谢这个post 。
我只是将我的_error_messages.html.rb
改为:
<% if object.errors.any? %> <%= t('errors.template.header', :model => object.class.model_name.human, :count => object.errors.count) %>
<%= t('errors.template.body') %>
<% object.errors.full_messages.each do |msg| %> - <%= msg %>
<% end %>
<% end %>
现在它有效,我很高兴:-)