我已經在我的模型下面的驗證(用戶):如何在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版。問題是,我不知道如何翻譯屬性名稱。比方說,例如,我希望名稱優先,而不是名字。我會怎麼做?
似乎沒有被工作軌道4.2 – AjaxLeung
它的工作原理上軌(5.1.3)。 – hackvan