Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 I18n label_tag translation

One would think the following code would access I18n:

= label_tag(:person_name)

and look up en.helpers.label.person_name, or something of the sort. However, the rails code does not seem to use I18n whatsoever:

159:       def label_tag(name = nil, content_or_options = nil, options = nil, &block)
160:         options = content_or_options if block_given? && content_or_options.is_a?(Hash)
161:         options ||= {}
162:         options.stringify_keys!
163:         options["for"] = sanitize_to_id(name) unless name.blank? || options.has_key?("for")
164:         content_tag :label, content_or_options || name.to_s.humanize, options, &block
165:       end

so it seems the only option is to explicitly call label_tag(:person_name, I18n.t(:person_name)). This seems unnecessary, so am I missing something here or should I be working on a rails patch? Any input is appreciated.

like image 264
Garrett Lancaster Avatar asked Nov 15 '22 04:11

Garrett Lancaster


1 Answers

= label_tag(:person_name) will not work. But you can use the t() method to get this to work.

= label_tag(t(:person_name))

The translation can then be added:

Then in file en.yml :

en: 
  person_name: John

You could also tie the translation to the view it is in:

In app/views/something/index.html.haml

= label_tag(t('.person_name'))

would look for this translation in file en.yml :

 en
  something
    index
      person_name: John
like image 162
Zabba Avatar answered Dec 18 '22 18:12

Zabba