Internationalization 在我的haml文件中,我希望包含一个yml文件中的字符串,该文件具有多个链接(在同一个字符串中)

Internationalization 在我的haml文件中,我希望包含一个yml文件中的字符串,该文件具有多个链接(在同一个字符串中),internationalization,yaml,haml,Internationalization,Yaml,Haml,这是呈现的html的外观: <p>The <a href="http://example.com/one.html">first link</a> and the <a href="http://example.com/two.html">second link</a> are both in this string.</p> %p = t(:example_text_html, link1:"https://www.

这是呈现的html的外观:

<p>The <a href="http://example.com/one.html">first link</a> and the <a href="http://example.com/two.html">second link</a> are both in this string.</p>
%p
  = t(:example_text_html, link1:"https://www.example.com/one.html", link2:"http://example.com/two.html")

我在尝试时遇到语法错误。

我建议在YAML语言环境文件中只保留翻译本身的内容(即“第一个链接”等),并在视图中保留链接信息。此外,由于“第一个链接”和“第二个链接”的内容可能会在区域设置中发生变化,因此您可能需要为它们设置单独的区域设置条目

综上所述,您可以执行以下操作:

config/locales/en.yml

en:
  first_link: first link
  second_link: second link
  example_text_html: The %{first_link} and the %{second_link} are both in this string that could get translated to have very different grammar.
%p
  = t('example_text_html',
      first_link: link_to(t('first_link'), 'http://example.com/one.html', target: :blank),
      second_link: link_to(t('second_link'), 'http://example.com/two.html', target: :blank))
%p
  = t('example_text_html', first_link: first_link, second_link: second_link)
app/views/your_view.html.haml

en:
  first_link: first link
  second_link: second link
  example_text_html: The %{first_link} and the %{second_link} are both in this string that could get translated to have very different grammar.
%p
  = t('example_text_html',
      first_link: link_to(t('first_link'), 'http://example.com/one.html', target: :blank),
      second_link: link_to(t('second_link'), 'http://example.com/two.html', target: :blank))
%p
  = t('example_text_html', first_link: first_link, second_link: second_link)
如果这看起来有点长,您可以创建一些助手来清理它。也许是这样的:

app/helpers/your_helper.rb

def first_link
  link_to(t('first_link'), 'http://example.com/one.html', target: :blank)
end

def second_link
  link_to(t('second_link'), 'http://example.com/two.html', target: :blank)
end
因此,您可以重构视图,使其看起来像:

app/views/your_view.html.haml

en:
  first_link: first link
  second_link: second link
  example_text_html: The %{first_link} and the %{second_link} are both in this string that could get translated to have very different grammar.
%p
  = t('example_text_html',
      first_link: link_to(t('first_link'), 'http://example.com/one.html', target: :blank),
      second_link: link_to(t('second_link'), 'http://example.com/two.html', target: :blank))
%p
  = t('example_text_html', first_link: first_link, second_link: second_link)

我试试这个!谢谢我必须将_html部分添加到变量中吗?我假设您的意思是将
_html
添加到YAML键中?对于这个解决方案,您不需要这样做,因为这些键的值中不包含HTML。有关在Rails中使用安全HTML翻译的更多信息,请参阅……我确实需要将HTML添加到主变量名中,以使其呈现HTML。请编辑您的答案,以便我将其标记为正确答案?而且,我没有使用助手。。。在这一点上,这有点让人困惑。也许你可以在答案的末尾加上它?只是一个建议。我想我需要从头开始创建该文件,对吗?我们同时评论:)