How to translate form labels on devise?

716 views
Skip to first unread message

GWL

unread,
Jun 27, 2012, 12:01:10 AM6/27/12
to plataforma...@googlegroups.com

Hi,
How do I do translation on devise form labels?
I did the following but it doesn't work.

The form

<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %><p class="h2"></p>
  <div class ="row"><%= f.label(:email,:class=>"form-label")%>
  <%= f.email_field(:email,:class=>"wide-textbox")%></div>

  <div class ="row"><%= f.label(:password,:class=>"form-label")%>
  <%= f.password_field(:password,:autocomplete => "off",:class =>"wide-textbox")%></div>

  <% if devise_mapping.rememberable? -%>
    <div class ="row"><%= f.check_box(:remember_me,:class=>"remember-me-checkbox")%><%= f.label(:remember_me,:class=>"remember-me")%></div>
  <% end -%>
  <div class="row"><%= f.submit(:value=>"",:class=>"button-signin")%></div>
<% end %>
            <div class="row">
              <span class="forgot-password">
                <%= link_to "Forgot password?", new_password_path(resource_name)%><br />
                <%= link_to "Didn't receive confirmation email?", new_confirmation_path(resource_name)%><br />
              </span>
            </div><!-- /.row -->
          </form>

My en-US yml file

en-US:
helpers:
  label:
    email: Email Address
    password: Password
    remember_me: Remember Me

But it still doesn't render the correct form labels.

Carlos Antonio da Silva

unread,
Jun 27, 2012, 8:19:21 AM6/27/12
to plataforma...@googlegroups.com
Translation should work similarly as any other form in Rails: you can either use helpers.label.object_name.attribute_name, where object_name could be user, and attribute_name could be email - so you'd end up with helpers.label.user.email.

And the other possibility is to just use the human attribute name of the fields, just by changing their names using the locale, under activerecord.attributes.user.email, for instance.

-- 
At.
Carlos Antonio

GWL

unread,
Jun 28, 2012, 7:15:53 AM6/28/12
to plataforma...@googlegroups.com
I found the crux of the solution is this
en-US:
helpers:
<object>

 label: email: Email Address password: Password remember_me: Remember Me
You need to specify the 'object'.
In devise if you are doing the sign_in page object would be session.
If you are doing the registration page you would need to use active model to specify attributes, using registration as the object will not work.

Carlos Antonio da Silva

unread,
Jun 28, 2012, 8:50:12 AM6/28/12
to plataforma...@googlegroups.com
You can just look at the html code to see what's the object name you should be using as namespace, or to the console log when you post the params.

Cheers.

-- 
At.
Carlos Antonio

Reply all
Reply to author
Forward
0 new messages