# Restful authentication by\_password not translated

**URL:** <https://www.ruby-forum.com/t/restful-authentication-by-password-not-translated/203094>\
**Category:** Rails I18n\
**Created:** [February 12, 2011, 3:14pm UTC](https://www.ruby-forum.com/t/restful-authentication-by-password-not-translated/203094 "2011-02-12T15:14:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 12, 2011, 3:14pm UTC](https://www.ruby-forum.com/t/restful-authentication-by-password-not-translated/203094/1 "2011-02-12T15:14:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [February 12, 2011, 3:16pm UTC](https://www.ruby-forum.com/t/restful-authentication-by-password-not-translated/203094/2 "2011-02-12T15:16:30Z")

</div>

Replace {{}} with %{}

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [February 12, 2011, 9:03pm UTC](https://www.ruby-forum.com/t/restful-authentication-by-password-not-translated/203094/3 "2011-02-12T21:03:12Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [February 12, 2011, 10:14pm UTC](https://www.ruby-forum.com/t/restful-authentication-by-password-not-translated/203094/4 "2011-02-12T22:14:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [February 13, 2011, 1:01am UTC](https://www.ruby-forum.com/t/restful-authentication-by-password-not-translated/203094/5 "2011-02-13T01:01:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [February 13, 2011, 9:43am UTC](https://www.ruby-forum.com/t/restful-authentication-by-password-not-translated/203094/6 "2011-02-13T09:43:05Z")

</div>

> 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/](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).
