Rails I18n:插补链接的更好方法? [英] Rails I18n: Better way of interpolating links?

查看:78
本文介绍了Rails I18n:插补链接的更好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种更清洁的content_tag-ish方法? (我不想在我的YML中使用HTML)

Is there a cleaner, content_tag-ish way of doing this? (I don't want HTML in my YML)

en.yml:

expert_security_advice: "Go <a href='{{url}}'>here</a> for expert security advice."

layout.html.erb:

layout.html.erb:

<%= t(:expert_security_advice, :url => "http://www.getsafeonline.org") %>


推荐答案

我能想到的最好的方法:

The best I could come up with:

en.yml:

expert_security_advice: "Go *[here] for expert security advice."

layout.html.erb:

layout.html.erb:

<%= translate_with_link(:expert_security_advice, "http://www.getsafeonline.org") %>

application_helper.rb:

application_helper.rb:

include ActionView::Helpers::TagHelper

def translate_with_link(key, *urls)
  urls.inject(I18n.t(key)) { |s, url|
    s.sub(/\*\[(.+?)\]/, content_tag(:a, $1, :href => url))
  }
end

这篇关于Rails I18n:插补链接的更好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆