开发者

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_n开发者_高级运维ame)). This seems unnecessary, so am I missing something here or should I be working on a rails patch? Any input is appreciated.


= 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜