Ruby on Rails 上的 i18n,<和>被替换为 &gt; <<当无意时

发布于 2024-09-28 11:36:40 字数 428 浏览 0 评论 0 原文

我正在 Rails 应用程序中创建用于国际化的语言环境文件,并且有一个我想要使用包含的标签进行翻译的 url,例如

html.erb

<%= t(foo.bar.xxxx) %>

yml 文件

富:酒吧: xxxx:“xxxx”

结果

xxxx

这破坏了我的链接。我的 ruby​​ 部分没有 h,所以这不应该起作用吗? 或者我应该在 yml 文件中不包含 html 标签?

Rails 版本是 3.0.1 Ruby 版本是 1.8.7 p249

I am creating locale files for internationalization in a rails app, and have a url that I want translated with tags included , for example

html.erb

<%= t(foo.bar.xxxx) %>

yml file

foo: bar:
xxxx: "xxxx"

result

< ;a href=
"/info/index.html"> ;xxxx</a> ;

which breaks my links. I do not have an h on the ruby part, so shouldn't this work?
Or should I just not have html tags within the yml file?

Rails version is 3.0.1
Ruby version is 1.8.7 p249

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

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

发布评论

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

评论(2

眼趣 2024-10-05 11:36:40

您的 HTML YAML 键需要有一个 _html 后缀:

foo:
  bar:
    xxxx_html: "<strong>Some HTML Here</strong>"

执行此操作,Rails 会将字符串标记为 html_safe 并将渲染出 HTML,而不是将其转换为 & 。 gt;<

您还需要使用完整的键名来引用它,当您调用 xxxx 时,Rails 不会自动看到 _html 后缀。

<%= t 'foo.bar.xxxx_html' %>

Your HTML YAML keys need to have a _html suffix:

foo:
  bar:
    xxxx_html: "<strong>Some HTML Here</strong>"

Doing this Rails will mark the string has html_safe and will render out the HTML instead of converting it to > and <.

You need to reference it with the full key name as well, Rails doesn't automatically see the _html suffix when you call xxxx.

<%= t 'foo.bar.xxxx_html' %>
回眸一遍 2024-10-05 11:36:40

Rails 通过阻止模型数据显示为实际标记来防止注入攻击。 raw 函数会阻止这种转换。

有效吗

<%= raw t(foo.bar.xxxx) %> 

Rails is preventing injection attacks by preventing model data from being displayed as actual markup. The raw function prevents that conversion.

Does

<%= raw t(foo.bar.xxxx) %> 

work?

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