A helper for links in your translations

Why?

I18n is baked right into Rails, and it’s great. But if you want to place links inside your translated copies, things get a little messy. You need to specify the label of your links separately from the rest of the copy. Writing HTML in your translations is even worse.

en:
  copy: "Did you read the %{guide_link}?"
  copy_guide_link: "Rails I18n guide"
<%=raw t("copy", guide_link: link_to(t("copy_guide_link"), "http://guides.rubyonrails.org/i18n.html")) %>

Wouldn’t it be much nicer and easier to understand for your translator to have the whole copy in single label? I18n_link lets you do that:

en:
  copy: "Did you read the %{guide_link:Rails I18n guide}?"
<%=t_link "copy", guide_link: "http://guides.rubyonrails.org/i18n.html" %>

You may have noticed in the example above, that t_link doesn’t require raw anymore. Of course, all HTML in the translation gets properly escaped, so you don’t have to worry about XSS.

Installation

Just add the following line to your Gemfile & run bundle install:

gem 'i18n_link'

Usage

You may have as many links inside your translations as you like, and normal interpolations are possible as well:

en:
  copy: "Did you read the %{guide:Rails I18n guide}? It has more than %{advises} useful advises. You may fork the repo at {repo:github}."
<%=t_link "copy", guide: "http://guides.rubyonrails.org/i18n.html", advices: 100, repo: "https://github.com/lifo/docrails/tree/master/railties/guides" %>

You may also specify options for link_to. Just give a second option named like the link + _options

<%=t_link "copy", guide_link: "http://guides.rubyonrails.org/i18n.html", guide_link_options: {target: '_blank', class: "important"} %>

You may pass any kind of object accepted by link_to as the link target, so your loved named routes like article_path(:id => article.id) or hashes like {controller: "articles", action: "index"} will all work fine, too.

Even nested interpolations are possible:

en:
  copy: "Did you read this %{link:nice %{guide}}?"
<%=t_link "copy", link: "http://guides.rubyonrails.org/i18n.html", guide: 'article' %>

isit18.com?

Sorry, I’m using Ruby 1.9.2 at the time of writing and didn’t took the time to port i18n_link back to Ruby 1.8. You either might update to Ruby 1.9 as well or make the port yourself. It’s probably a mater of minutes to do so, since the lib is small. Send me a pull request, if you have a port!

Abandoned & outdated?

Everybody hates old and outdated gems, which don’t work on with the latest Rails or Ruby version or haven’t been updated for ages. Sadly, rubygems is full of such gems. If you improved i18n_link, send me a pull request. If I have not time to support the lib anymore, I will happily hand the project over to a new maintainer.

Broken English?

I’m not a native speaker, so you’ll probably find some spelling errors or clumsy sentences above. If you do, please send me a message.