Rails 3 I18n label_tag 翻译

发布于 2024-10-08 21:09:20 字数 774 浏览 0 评论 0 原文

人们可能会认为以下代码将访问 I18n:

= label_tag(:person_name)

并查找 en.helpers.label.person_name 或类似的内容。然而,rails 代码似乎没有使用任何 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

所以似乎唯一的选择是显式调用 label_tag(:person_name, I18n.t(:person_name))。这似乎没有必要,所以我在这里遗漏了一些东西还是应该在 Rails 补丁上工作?任何意见都会受到赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的那么颓废 2024-10-15 21:09:21

= label_tag(:person_name) 将不起作用。但是您可以使用 t() 方法来实现
工作。

= label_tag(t(:person_name))

然后可以添加翻译:

然后在文件 en.yml 中:

en: 
  person_name: John

您还可以将翻译与其所在的视图联系起来:

app/views/something/index.html.haml< /code>

= label_tag(t('.person_name'))

将在文件 en.yml 中查找此翻译:

 en
  something
    index
      person_name: John

= 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文