Rails错误消息中的转换属性(Rails 2.3.2,I18N) [英] Translated attributes in Rails error messages (Rails 2.3.2, I18N)
问题描述
我已经在转换文件中定义了转换的属性和模型名称,并且Modelname.human_attribute_name(...)返回正确转换的属性名称,但是错误消息中的属性名称未转换. 转换错误消息中的属性名称需要什么?
I have defined translated attributes and model names in the translation file and Modelname.human_attribute_name(...) returns the correctly translated attribute name, but the attribute names in the error messages are untranslated. What is needed that the attribute names in the error messages are translated?
推荐答案
从指南主题,则需要使用模型名称和属性名称正确设置本地化文件:
From the Guide on the subject, you will need to set up the localization file properly with your model names and attribute names:
en:
activerecord:
models:
user: Dude
attributes:
user:
login: "Handle"
由于这是YAML,因此请确保您所有的选项卡"实际上都是两个软空间.然后,您可以使用User.human_name
和User.human_attribute_name(:login)
删除它们.
Since this is YAML, make sure all your "tabs" are actually two soft spaces. Then you can get them out with User.human_name
and User.human_attribute_name(:login)
.
我没有把它当作福音-可能有一个错误.我对其进行了测试,并且效果很好.我创建了一个名为Model
且具有属性title
的模型.这是我在config/locales
中的fr.yml
文件的摘要:
I didn't take that for gospel -- there could have been a bug. I tested it, and it works fine. I made a model named Model
with an attribute title
. Here is a snippet of my fr.yml
file in config/locales
:
fr:
activerecord:
models:
model: "Sumfink"
attributes:
model:
title: "Tiltile"
以下是相关的查看代码:
Here's the relevant view code:
<h1>New <%= Model.human_name %></h1>
<% form_for(@model) do |f| %>
<%= f.error_messages %>
<%= Model.human_attribute_name("title") %><br />
<%= f.text_field :title %>
<%= f.error_message_on :title %>
...
以及正确翻译的输出的屏幕截图: http://screencast.com/t/et5FhVe1Gp
And a screenshot of the properly translated output: http://screencast.com/t/et5FhVe1Gp
这篇关于Rails错误消息中的转换属性(Rails 2.3.2,I18N)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!