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.

Take profit of model columns' translations

#2758

With the incorpored I18n is possible to specify the right transalations for model names, model columns and model errors.
I was surprised when I defined them in one of my projects and realized that the "label" function didn't cared about my columns transalation.

Example, in a view:

<% form_for @user do |f| %>

<%= f.label :name %>
<%= f.text_field :name %>

<%= f.submit t('create') %>

<% end %>

The text of the label is by default "Name", no matter which language I have selected.

The easy solution:

<%= f.label :name, User.human_attribute_name("name") %>

I just thought it would be interesting that Rails tryed to translate it by default.
I changed the behaviour with a little patch, but maybe there's a better solution. My patch:

module ActionView
module Helpers

module FormHelper
  InstanceTag.class_eval do
    alias_method :to_label_tag_old, :to_label_tag
    def to_label_tag(text = nil, options = {})
      begin
        text ||= object_name.to_s.classify.constantize.human_attribute_name(method_name)
      rescue NameError => e; end
      to_label_tag_old(text,options)
    end
  end
end

end end

Reported by Hector Parra · June 4th, 2009 @ 02:26 PM

State: duplicate
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Ronald Sacher
  2. Hector Parra
    Hector Parra

    Yes, you are right. I'm sorry I didn't see it before. This ticket can be closed :)

    June 5th, 2009 @ 10:58 AM

  3. CancelProfileIsBroken
    CancelProfileIsBroken
    • State changed from new to duplicate

    June 5th, 2009 @ 12:16 PM