Using the 'it' Gem to add links to translations

How to can use the 'it' Gem to add links to your translations

We have an ongoing project which is a site that is available both in English and Chinese. We used the Rails baked in i18n functionality to implement the translations and the languages can be switched between to display either.

We found that adding links into our translations files was a bit of an arse and left it rather untidy. It was also going to be difficult to explain to our client how to do the Chinese translations from the English YAML file that we provided. After some searching we stumbled upon the it gem (it was and remains hard to find because the name is so ambiguous) which was a more elegant solution to our problem.

First of all you will want to add the gem to your Gemfile.

gem 'it'

You want this to sit out in your ‘main’ Gemfile as you’ll want it available in all environments. Then run bundle to grab your dependencies.

So onto using it. in your YAML file you’ll want to have something like this:

en:
  contact_us: "Why don't you %{contact: contact us?}"

You want to call the ‘it’ Gem with It. Within your view file you want something like this:

<%= it 'contact_us', contact: It.link(contact_us_path) %>

So this will give us something that looks like this:

A line of text which reads "Why don't you contact us?" Contact us is a hyperlink

The easiest way for me to explain the relationship between what is in the yaml file and view is with this diagram:

A diagram showing the relationship between the view and yaml elements. The contact_us from the yaml file is used in the view file by calling it "contact_us" and the contact from the yaml file is used to call the link with It.link(contact_us_path)


Recent posts View all

Ruby

Forcing a Rails database column to be not null

How you can force a table column to always have something in it with Rails

Writing Marketing

We've deleted an article's worth of unhelpful words

We've improved several pages across our site by removing words that add no value, and often detract from the article.