rails i18n - 翻译带有链接的文本 [英] rails i18n - translating text with links inside
问题描述
我想输入如下所示的文本:
I'd like to i18n a text that looks like this:
已经注册了?登录!
请注意,文本上有一个链接.在此示例中,它指向 google - 实际上它将指向我的应用的 log_in_path
.
Note that there is a link on the text. On this example it points to google - in reality it will point to my app's log_in_path
.
我找到了两种方法,但没有一种看起来正确".
I've found two ways of doing this, but none of them looks "right".
我知道的第一种方法是让我的 en.yml
:
The first way I know involves having this my en.yml
:
log_in_message: "Already signed up? <a href='{{url}}'>Log in!</a>"
在我看来:
<p> <%= t('log_in_message', :url => login_path) %> </p>
这工作,但在 en.yml
上有 <a href=...</a>
部分在我看来不是很干净.
This works, but having the <a href=...</a>
part on the en.yml
doesn't look very clean to me.
我知道的另一个选项是使用 本地化视图 - login.en.html.erb
和 login.es.html.erb
.
The other option I know is using localized views - login.en.html.erb
, and login.es.html.erb
.
这也感觉不对,因为唯一不同的就是前面提到的那一行;视图的其余部分(约 30 行)将对所有视图重复.它不会很干燥.
This also doesn't feel right since the only different line would be the aforementioned one; the rest of the view (~30 lines) would be repeated for all views. It would not be very DRY.
我想我可以使用本地化部分",但这似乎太麻烦了;我想我更喜欢第一个选项,而不是拥有这么多微小的视图文件.
I guess I could use "localized partials" but that seems too cumberstone; I think I prefer the first option to having so many tiny view files.
所以我的问题是:是否有正确"的方式来实现这一点?
So my question is: is there a "proper" way to implement this?
推荐答案
en.yml
log_in_message_html: "This is a text, with a %{href} inside."
log_in_href: "link"
login.html.erb
<p> <%= t("log_in_message_html", href: link_to(t("log_in_href"), login_path)) %> </p>
这篇关于rails i18n - 翻译带有链接的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!