![](/img/trans.png)
[英]rails tutorial literred with hard coded error messages, how to localize?
[英]How to localize a generic error messages partial 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 %>
在我決定使用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.class.to_s.underscore.humanize.downcase %>
我感到困惑。 我嘗試了類似<%= t 'activerecord.errors.template.header', :model => object.model_name.human %>
但是沒有任何運氣。
有人可以幫忙嗎?
我已經閱讀了三次關於本地化的Rails指南,但是我被困在這里。
謝謝你的幫助!
首先,您應該通過以下方式設置語言環境:
config.i18n.default_locale = :de
其次,您的語言環境文件應如下所示:
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: не может быть пустой
好的,由於這個線程 ,我這次自己找到了答案。
我只是將_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 %>
現在它可以工作了,我很高興:-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.