Chen Yangjian's Blog

Carpe diem - Seize the day

定制 Authlogic 的错误信息

| Comments

Authlogic 是 Rails 项目中比较常用的 Gem,用来方便添加账户系统,登陆校验、密码哈希,都一并做掉了,Redmine 似乎也用的它。有个不足处是,HTTPS 的方式比较麻烦,还需要许多额外的工作。

另一个经常需要改动的是它的错误提示信息,其实可以在 locale 中很方便地定制,在 config/locales/zh.yml 里头复制进

en.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

authlogic:
    error_messages:
      login_blank: can not be blank
      login_not_found: is not valid
      login_invalid: should use only letters, numbers, spaces, and .-_@ please.
      consecutive_failed_logins_limit_exceeded: Consecutive failed logins limit exceeded, account is disabled.
      email_invalid: should look like an email address.
      password_blank: can not be blank
      password_invalid: is not valid
      not_active: Your account is not active
      not_confirmed: Your account is not confirmed
      not_approved: Your account is not approved
      no_authentication_details: You did not provide any details for authentication.
      general_credentials_error: Login/Password combination is not valid
    models:
      user_session: UserSession (or whatever name you are using)
    attributes:
      user_session: (or whatever name you are using)
        login: login
        email: email
        password: password
        remember_me: remember me

然后提供你自己的翻译就成了。记得项目的 locale 设成 zh 吧。其他语言,自然照搬即可。

Comments