【问题标题】:Rails: render ruby logic in i18n stringRails:在 i18n 字符串中呈现 ruby​​ 逻辑
【发布时间】:2011-07-28 09:37:00
【问题描述】:

我目前正在翻译一个应用程序,但我偶然发现了一个问题。我在我的观点中使用它:

t("page.text")

这是我的 yaml 文件:

page:
  text: "This is my tekst with a #{link_to "pages index, pages_path}"

这个字符串在没有link_to逻辑的情况下被输出,就像这样:“这是我的带有#{link_to“pages index,pages_path}”的tekst。这不是我想要的,我想要用link_to解析的字符串像这样的功能:“这是我的带有 pages index 的 tekst”,其中 pages index 链接到 /pages 路由...

谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby internationalization yaml


    【解决方案1】:

    Rails 的 i18n API 允许您在 YAML 文件中使用基于变量的插值。做你想做的事:

    # config/en.yml
    page:
      text: "This is my test with a %{link}"
    
    # view.html.erb
    <%= t("page.text", :link => link_to("pages index", pages_path))
    

    您可以在官方 Rails 指南中了解更多信息:http://guides.rubyonrails.org/i18n.html#interpolation

    【讨论】:

    • 我必须放置一个“_html”后缀来防止浏览器转义 html 标签。类似 text_html: "This is my test with a %{link}"
    • 这不会处理链接上“页面索引”文本的翻译。处理翻译的最佳方法是什么?
    猜你喜欢
    • 1970-01-01
    • 2011-05-11
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多