Rails 3 I18n label_tag translation

You might think that the following code will have access to I18n:

= label_tag(:person_name)

and find en.helpers.label.person_name or something like that. However, the rails code does not seem to use I18n:

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

therefore, 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 work on the rails patch? Any input is appreciated.

+3
source share
1 answer

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

= label_tag(t(:person_name))

Then you can add the translation:

Then in the file en.yml:

en: 
  person_name: John

You can also bind the translation to the form in which it is located:

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

= label_tag(t('.person_name'))

en.yml:

 en
  something
    index
      person_name: John
+1

Source: https://habr.com/ru/post/1781065/


All Articles