Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Patch for internationalization (I18n) of label method when binding a form with form_for

#4035

Hello,

I am a newbie in Rails' community and this is my first suggestion to participate to the wonderful Rails' adventure.

The problem I have encountered is that the label method in the form_for helper is not internationalized.

Example

a view:

<% form_for @user, :url => { :controller => :users, :action => :send_signup } do |f| %>
  <%= f.label :name %><br />
  <%= f.text_field :name %>
  <%= f.submit I18n.t(:send) %>
<% end %>
Return label text: Name
But I want Nom because I use the french locale.

My suggestion is to add the support of I18n to the label method:

fr.yml (locale for french)

fr:
  activerecord:
    attributes:
      user:
        name: Nom

Original

Version: Rails 2.3.5
File: ../actionpack-2.3.5/lib/action_view/helpers/form_helper.rb

Line 742: content = (text.blank? ? nil : text.to_s) || method_name.humanize

Suggestion

Line 742: content = (text.blank? ? nil : text.to_s) || I18n.t(method_name, :default => method_name.humanize, :scope => [:activerecord, :attributes, object_name])

I hope this will be helpful.

Best regards,

A. Espinosa

Reported by Antonio Espinosa · February 22nd, 2010 @ 03:38 PM

State: duplicate
Milestone: none
Assigned to: José Valim José Valim
Importance: none

Activity

  1. Antonio Espinosa
    Antonio Espinosa
    • Title changed from Internationalization (I18n) of label method when binding a form with form_for to Patch for internationalization (I18n) of label method when binding a form with form_for

    February 22nd, 2010 @ 05:17 PM

  2. José Valim
    José Valim
    • State changed from new to duplicate

    It's on both Rails 2.3 and Rails 3.0. Please wait for the next release.

    February 23rd, 2010 @ 09:09 PM

  3. Sebastian
    Sebastian
    • Assigned user set to José Valim

    José, I'm using actionpack-2.3.5, line 742 in actionpack-2.3.5/lib/action_view/helpers/form_helper.rb still looks like this:

    content = (text.blank? ? nil : text.to_s) || method_name.humanize
    

    Or are you talking about 2.3.6?

    February 23rd, 2010 @ 10:06 PM

  4. José Valim
  5. Antonio Espinosa
    Antonio Espinosa

    v 2.3.6

    Line 808: I18n.t("helpers.label.#{object_name}.#{method_name}", :default => "").presence
    

    This version use the scope "helpers.label" in the locale file. Why not use the already existing scope "activerecord.attributes" used by Model#human_attribute_name ?

    February 24th, 2010 @ 07:07 AM

  6. José Valim
    José Valim

    Because it should be ORM agnostic. What if you are using Datamapper? Your own form object?

    February 24th, 2010 @ 07:08 AM

  7. Antonio Espinosa
    Antonio Espinosa

    So what's your recommandation for not have redundant text in activerecords.attributes and helpers.label scope ? Overload Model#human_attribute_name to use label.section instead of activerecords.attributes ?
    Thanks.

    February 24th, 2010 @ 07:35 AM

  8. José Valim
    José Valim

    The code on 2.3.6 and 3.0.0 searches first in helpers.label and then in activerecord.attribute. Please take a look at the code that you will see it.

    February 24th, 2010 @ 07:38 AM

  9. Antonio Espinosa
    Antonio Espinosa

    This is right.

    Thank you José.

    February 24th, 2010 @ 07:54 AM