简体   繁体   中英

Use I18n with Rails ActiveRecord Vaildation Message

I am building an app, using I18n for different languages display. In this moment, the validation message shown in this way.

en

USERNAME CAN'T BE BLANK

zh-CN

USERNAME 不能为空字符

I would like to translate the attribute USERNAME to some other string. So I followed the instruction in Rails Guide and added the following lines in my locale files.

en:
  activerecord:
    attributes:
      user:
        username: "User Name"


zh-CN:
  activerecord:
    attributes:
      user:
        username: "用户名"

When I run the human_attribute_name command, it can be translated.

ruby-1.9.2-p290 :001 > User.human_attribute_name("username")
 => "Username"

But in the validation message, it just capitalized all the characters of attribute name.

CURRENT_PASSWORD 不能为空字符

Did I missed anything? How can I fix it?

Thank all. :)

I think you may need

en:
  activerecord:
    errors:
      models: 
        user:
          attributes:
            username: "User Name"

I made a mistake. I added a custom helper to handle the validation message. The I18n translation in question is correct. I can use human_attribute_name to translate the arribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM