Rails 3:在 i18n 表单辅助翻译中使用 HTML

发布于 2024-10-06 12:17:18 字数 805 浏览 4 评论 0原文

我正在使用自动表单标签助手来创建表单标签并通过 i18n 支持进行翻译,但是,我希望在标签中包含 HTML,但我不知道如何使其成为 HTML 安全的。

例如:

en:
  helpers:
    label:
      product:
        name: 'Your Product Name <small>Try to be creative</small>'

最终为:

<label for="product_name">Your Product Name &lt;Try to be creative&gt;</label>

但我希望它是:

<label for="product_name">Your Product Name <small>Try to be creative</small></label>

有没有办法让我将翻译指定为 html_safe,以便在输出之前不会对其进行编码?

另外,这似乎是 HTML 设置的最语义化的方式,但如果我完全错误地处理这个问题,我愿意接受建议。

谢谢:)

更新:

<%= form_for @product do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
<% end %>

I am using the automatic form label helper for creating my form labels and having them translated via the i18n support, however, I want to have HTML in the label and I can't figure out how to make it HTML safe.

For example:

en:
  helpers:
    label:
      product:
        name: 'Your Product Name <small>Try to be creative</small>'

Ends up as:

<label for="product_name">Your Product Name <Try to be creative></label>

But I want it to be:

<label for="product_name">Your Product Name <small>Try to be creative</small></label>

Is there a way for me to specify the translation as html_safe so that it doesn't get encoded before the output?

Also, this seems like the most semantic way for the HTML to be setup but I am open to suggestions if I am approaching this entirely the wrong way.

Thanks :)

Updated:

<%= form_for @product do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
<% end %>

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

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

发布评论

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

评论(4

归途 2024-10-13 12:17:18

在rails3中使普通密钥自动html安全的方法是将_html或.html附加到密钥名称。例如name_html。这只能通过视图中的 translate/t 方法起作用,即不能通过 I18n.t 起作用。无论如何,这在这里不起作用,因为标准表单助手不会尝试该版本的密钥。

按照建议,创建您自己的表单生成器是正确的方法。 您也可以这样做。

f.label :name, t('helpers.label.product.name_html')

如果这只发生在几个地方,

The way to make a normal key automatically html safe in rails3 is to append _html or .html to the key name. E.g. name_html. This only works via the translate/t method in the view, i.e. not via I18n.t This isn't going to work here anyway though as the standard form helper doesn't try that version of the key.

Creating your own form builder is the way to go as suggested. You could also do

f.label :name, t('helpers.label.product.name_html')

If this only happens in a couple of places.

别挽留 2024-10-13 12:17:18

不知道您的实际帮助程序如何,但您可以使用 html_safe 轻松做到这一点(前提是您的标签值不会被其他用户输入)。

类似于: t("helpers.label.product.name").html_safe

如果这不起作用,请提供您的辅助方法的实现,或者仅提供用于输出结果的行。

======已更新======

感谢您更新的代码,现在我知道最好的答案是什么:D

我也不知道您是否真的想要helpers.label.product.name.

但我认为还有另一种更好的方法,其定义如下:

en:
  activerecord:
    attributes:
      product:
        labels:
          name: "Your Product Name <small>Try to be creative</small>"

如果您不想构建自己的自定义表单生成器,请使用此:

= f.label :name, Product.human_attribute_name("labels.name").html_safe

实际上,如果您定义自己的表单生成器,很容易重新定义标签方法自动生成它的文本。

Don't know how your actual helper is, but you could use html_safe to do that easily (provided that your label's value won't be input by other users).

something like: t("helpers.label.product.name").html_safe

If this is not working, please give your implementation of your helper method, or only the lines for outputting the result.

====== UPDATED ======

Thanks to your updated code, now I know what's the best answer :D

I don't know too if you really want helpers.label.product.name.

But there is another way I think would be better, which is define as he following:

en:
  activerecord:
    attributes:
      product:
        labels:
          name: "Your Product Name <small>Try to be creative</small>"

If you don't want to build your own custom form builder, use this:

= f.label :name, Product.human_attribute_name("labels.name").html_safe

Actually if you define your own form builder, it easy redefine the label method to generate it's text automatically.

不甘平庸 2024-10-13 12:17:18

我用一个新函数解决了这个问题,我将其放置在一个辅助文件中:

def labeltext_with_html(model,field)
  I18n.t("helpers.label.#{model}.#{field}").html_safe
end

我这样称呼它:

label(my_model, my_field, labeltext_with_html(my_model, my_field))

只需调整变量以适合您的用例。标签标记可能还需要 .html_safe,具体取决于用例。

I solved this with a new function, which I placed in a helper file:

def labeltext_with_html(model,field)
  I18n.t("helpers.label.#{model}.#{field}").html_safe
end

I call it like this:

label(my_model, my_field, labeltext_with_html(my_model, my_field))

Just adjust the variables to suit your use case. The label tag may also need .html_safe, depending on the use case.

是你 2024-10-13 12:17:18

另一种选择是执行以下操作:

    name: '<span>Your Product Name <small>Try to be creative</small></span>'

The other option is to do the following:

    name: '<span>Your Product Name <small>Try to be creative</small></span>'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文