2012-05-30 44 views
11

我已经在我的模型下面的验证(用户):如何在Rails 3.2中翻译ActiveRecord属性名称?

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

我在阳明语言环境中的以下文件:

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

现在,如果我开始rails console,并编写以下:

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

我会看到以下错误:

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

正如您所看到的,属性名称也被预置为字段错误。到目前为止,无处不在我的代码,我已经使用model.errors[:field],这将始终告诉我我在YML文件中的字符串,但我想字符串更改为:

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

,并使用full_messages版。问题是,我不知道如何翻译属性名称。比方说,例如,我希望名称优先,而不是名字。我会怎么做?

回答

1

在Rails 5中,在attributes之后,我不得不使用模型名称的下划线命名空间。就像这样:

pt-BR: 
    activerecord: 
    attributes: 
     my_model_name_in_underscore: 
     attribute_name: 'String' 

来源:https://stackoverflow.com/a/5526812/1290457

相关问题