我怎么能翻译在Rails的3.2 ActiveRecord的属性名称? [英] How can I translate the ActiveRecord attribute name in Rails 3.2?

查看:86
本文介绍了我怎么能翻译在Rails的3.2 ActiveRecord的属性名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下验证在我的模型(用户):

I have the following validation in my model ( User ):

  validates :first_name,:length => {:minimum => 2,:maximum => 50},:format => { :with => /[a-zA-Z]+/ }

我在阳明区域设置以下文件:

I have the following in my yml locales file:

 attributes:
   first_name:
     too_short: "First name is too short"
     too_long: "First name is too long"
     invalid: "First name is not valid"

现在,如果我开始一个轨控制台,并写入以下内容:

Now, if I start a rails console, and write the following:

a = User.new
a.valid?
a.errors.full_messages

我会看到以下错误:

I will see the following errors:

["First name First name is too short", "First name First name is not valid"]

正如你所看到的,属性名也$ ppended到外地错误p $。到目前为止,到处都在我的code,我已经使用 model.errors [:现场] ,这将始终显示我的字符串我在阳明文件,但我想改变字符串:

As you can see, the attribute name is also prepended to the field error. So far, everywhere in my code, I have used model.errors[:field], and this will always show me the string I have in the yml file, but I'd like to change the strings to:

 attributes:
   first_name:
     too_short: " is too short"
     too_long: " is too long"
     invalid: " is not valid"

和使用full_messages版本。问题是,我不知道怎么翻译的属性名称。比方说,例如,我想,而不是名字,先有名称。我将如何做呢?

And use the full_messages version. The problem is, I don't know how to translate the attribute name. Let's say for example, that I'd like instead of First name, to have Name first. How would I do it?

推荐答案

您可以在这里<一找到答案href="http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models">http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models

在你的config /区域/(your_lang)使用.yml

in your config/locale/(your_lang).yml

en: 
  activerecord:
    models:
      user: Dude
    attributes:
      user:
        first_name: "Name first"

变恩:与您需要使用的语言符号

change "en:" with the language symbol that you need to use

欢呼声

这篇关于我怎么能翻译在Rails的3.2 ActiveRecord的属性名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆