(Tests Ruby 1.9.2, 1.9.3, 2.0.0, Rubinius and Jruby)

What is it?

I18n is baked right into Rails, and it’s great. But if you want to place markup or 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: "If you are already registered, %{login_link}!"
  copy_login_link: "please sign in"
<%=raw t("copy", login: link_to(t("copy_login_link"), login_path)) %>

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

en:
  copy: "If you are already registered, %{login_link:please sign in}!"
<%=it "copy", login_link: login_path %>

You may have noticed in the example above, that it 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 'it'

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}."
<%=it "copy", guide: It.link("http://guides.rubyonrails.org/i18n.html"), advices: 100, repo: It.link("https://github.com/lifo/docrails/tree/master/railties/guides") %>

As you see above, unless the interpolation name is link or starts with _link or ends with _link, you need to call It.link to create a link. The advantage of It.link: You may specify options like you would with link_to:

<%=it "copy", guide: It.link("http://guides.rubyonrails.org/i18n.html", 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) will all work fine.

Want to introduce some markup into your sentences? it will help you:

en:
  advantages: There are %{b:many advantages} for registered users!
<%=it "advantages", b: It.tag(:b, class: "red") %>

Even nested interpolations are possible:

en:
  copy: "Want to contact %{user}%? %{link:send %{b:%{user} a message}}!"
<%=it "copy", link: "mailto:igel@igels.net", user: 'iGEL', b: It.tag(:b) %>

If you would like to use the same translations in your html and plain text mails, you will like the It.plain method:

en:
  mail_copy: "Do you like %{link:Rails}?"

HTML mail:
<%= it "mail_copy", link: It.link("http://www.rubyonrails.org/") %>

Plain mail:
<%= it "mail_copy", link: It.plain("%s[http://www.rubyonrails.org/]") %>

The %s will be replaced with the label, in the example with Rails. You could provide any other string containing %s. The default is just %s, so it will return only the label itself.

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 it 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.