2012-05-30 43 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

相關問題