在我的 haml 文件中,我想包含一个来自具有多个链接的 yml 文件的字符串(在同一个字符串中)
In my haml file, I want include a string from a yml file that has multiple links (in the same string)
这是呈现的 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>
yml 和 haml 应该是什么样的?
注意:我已经弄清楚如何使用单个 link 的字符串,但我对如何设置多个 link 感到困惑。
我认为 yaml 可能如下所示:
example_text_html: "The <a href='%{link1}' target='_blank'>first link</a> and the <a href='%{link2}' target='_blank'>second link</a> are both in this string."
这就是我认为 haml 的样子:
%p
= t(:example_text_html, link1:"https://www.example.com/one.html", link2:"http://example.com/two.html")
我尝试这样做时出现语法错误。
我建议只在 YAML 语言环境文件中保留翻译本身的内容(即 "first link" 等),并在您的视图中保留 link 信息。此外,由于 "first link" 和 "second link" 的内容在语言环境中可能会发生变化,因此您可能需要为它们单独设置语言环境条目。
把这些放在一起,你可以做类似的事情:
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.
app/views/your_view.html.haml
%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))
如果看起来有点长,您可以创建一些助手来清理它。也许是这样的:
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
%p
= t('example_text_html', first_link: first_link, second_link: second_link)
这是呈现的 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>
yml 和 haml 应该是什么样的?
注意:我已经弄清楚如何使用单个 link 的字符串,但我对如何设置多个 link 感到困惑。
我认为 yaml 可能如下所示:
example_text_html: "The <a href='%{link1}' target='_blank'>first link</a> and the <a href='%{link2}' target='_blank'>second link</a> are both in this string."
这就是我认为 haml 的样子:
%p
= t(:example_text_html, link1:"https://www.example.com/one.html", link2:"http://example.com/two.html")
我尝试这样做时出现语法错误。
我建议只在 YAML 语言环境文件中保留翻译本身的内容(即 "first link" 等),并在您的视图中保留 link 信息。此外,由于 "first link" 和 "second link" 的内容在语言环境中可能会发生变化,因此您可能需要为它们单独设置语言环境条目。
把这些放在一起,你可以做类似的事情:
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.
app/views/your_view.html.haml
%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))
如果看起来有点长,您可以创建一些助手来清理它。也许是这样的:
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
%p
= t('example_text_html', first_link: first_link, second_link: second_link)