Restful authentication by_password not translated

44 views
Skip to first unread message

James Gaston

unread,
Feb 11, 2011, 12:55:03 PM2/11/11
to rails-i18n
I'm running rails 2.3.8 and restful_authentication and I find the
error messages generated in the by_password.rb are not appearing,
instead I get the user-hostile {{attribute}} {{message}} messages.
I've tried and tried to figure a fix and, aside from discarding
restful_authentication (which I may do, at a later date) I can't solve
the problem. Any suggestions?

thanks
- James

Henrik Nyh

unread,
Feb 12, 2011, 9:15:26 AM2/12/11
to rails...@googlegroups.com
Replace {{}} with %{}

Sent from my iPhone

> --
> You received this message because you are subscribed to the Google Groups "rails-i18n" group.
> To post to this group, send email to rails...@googlegroups.com.
> To unsubscribe from this group, send email to rails-i18n+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rails-i18n?hl=en.
>

James Gaston

unread,
Feb 12, 2011, 3:02:01 PM2/12/11
to rails...@googlegroups.com
Thanks, but I've gone down this route already. The key thing to note: all model errors in my app are printed correctly except one: the restful_authentication signup step. For example, here is my en.yml: 
  
en:
  use_only_lets:
    use only letters, numbers
  passwordpresent:
    password required
  confirmation:
    password doesn't match confirmation
  passwordlength:
    password must be 6-40 characters long
  activerecord:
    errors:
      template:
        body: There were problems with the following fields
        header:
          one: 1 error prohibited this %{model} from being saved
          other: "%{count} errors prohibited this %{model} from being saved"
        messages:
          inclusion: "is not included in the list"
          exclusion: "is reserved"
          invalid: "is invalid"
          confirmation: "doesn't match confirmation"
          accepted: "must be accepted"
          empty: "can't be empty"
          blank: "can't be blank"
          too_long: "is too long (maximum is %{count} characters)"
          too_short: "is too short (minimum is %{count} characters)"
          wrong_length: "is the wrong length (should be %{count} characters)"
          taken: "has already been taken"
          not_a_number: "is not a number"
          greater_than: "must be greater than %{count}"
          greater_than_or_equal_to: "must be greater than or equal to %{count}"
          equal_to: "must be equal to %{count}"
          less_than: "must be less than %{count}"
          less_than_or_equal_to: "must be less than or equal to %{count}"
          odd: "must be odd"
          even: "must be even"
          bad_email: "Email should look like an email address"
          bad_login: "Use only letters and numbers for your user name"
          bad_name: "Use only legal characters in your name"

Henrik Nyh

unread,
Feb 12, 2011, 4:13:28 PM2/12/11
to rails...@googlegroups.com
Could you give more detail on exactly what the problem is, what you've
tried and what you expect?

I looked at by_password.rb at
https://github.com/technoweenie/restful-authentication/blob/master/lib/authentication/by_password.rb
and it does things like

validates_presence_of :password, :if => :password_required?

Do you get the wrong message from those? What message do you get and
what message did you expect instead?

What's the deal with your custom keys like "en.passwordpresent"? Are
you attempting to have Rails use those? If so, how?

James Gaston

unread,
Feb 12, 2011, 6:59:56 PM2/12/11
to rails...@googlegroups.com
To summarize: In my rails 2.3.8 app, which uses restful_authentication, all model error messages (and there are a lot) work exactly as desired except for the scenario where a new user is signing up for the first time. Unfortunately this leaves potential new users with a really bad impression. 

For example, if the new user fails to enter a matching password - password confirmation, or makes any other entry error (no password, no login, etc), the message is like the following:

1 error prohibited this user from being saved

There were problems with the following fields

  • {{attribute}} {{message}}
              
The {{attribute}}{{message}} isn't translated. And yes I know to use %{} and not {{}} in en.yml.  Clearly there is something I am doing wrong but I've exhausted my ideas. Last I tried was as follows, which is an excerpt from by_password.rb: 

module Authentication
  module ByPassword
    # Stuff directives into including module
    def self.included(recipient)
      recipient.extend(ModelClassMethods)
      recipient.class_eval do
        include ModelInstanceMethods
        
        # Virtual attribute for the unencrypted password
        attr_accessor :password
        validates_presence_of     :password, :message=>I18n.t(:passwordpresent), :if => :password_required?
        validates_presence_of     :password_confirmation, :message=>I18n.t(:confirmation), :if => :password_required?
        validates_confirmation_of :password,  :message=>I18n.t(:confirmation), :if => :password_required?
        validates_length_of       :password, :within => 6..40, :message=>I18n.t(:passwordlength), :if => :password_required?
        before_save :encrypt_password
      end
    end # #included directives

# Note the addition of i18n.t() calls. 

thanks
- James 

Henrik Nyh

unread,
Feb 13, 2011, 3:41:21 AM2/13/11
to rails...@googlegroups.com
> For example, if the new user fails to enter a matching password - password
> confirmation, or makes any other entry error (no password, no login, etc),
> the message is like the following:
>
> 1 error prohibited this user from being saved
>
> There were problems with the following fields
>
> {{attribute}} {{message}}

Aha. Probably a Rails gem version issue - try this:
http://www.taylorbrooks.org/error-messages-in-rails-attribute-message/

> The {{attribute}}{{message}} isn't translated. And yes I know to use %{} and
> not {{}} in en.yml.  Clearly there is something I am doing wrong but I've
> exhausted my ideas. Last I tried was as follows, which is an excerpt from
> by_password.rb:

>         validates_presence_of     :password,
> :message=>I18n.t(:passwordpresent), :if => :password_required?

Not relevant anymore, but for good measure: if you use I18n.t in a
class context like that, IIRC it will always use the default locale in
production (since the class isn't reloaded for every request).
Instead, you just specify the symbol (:message => :passwordpresent)
and make sure to put the translation key in the expected place
(somewhere under activerecord.errors I believe).

قصص عربية

unread,
Dec 29, 2013, 12:51:20 PM12/29/13
to rails...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages