This project is archived and is in readonly mode.

#2758 ✓duplicate
Hector Parra

Take profit of model columns' translations

Reported by Hector Parra | June 4th, 2009 @ 02:26 PM | in 2.x

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 = {})
        text ||= object_name.to_s.classify.constantize.human_attribute_name(method_name)
      rescue NameError => e; end

end end

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>