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

Web DevProductivity

Keeping on top of website updates

Learn what website updates are, what they entail, why they are important, and how we can help

Freelancing

Getting the most out of your agency

Here are some tips based on years of working with, for, and as an agency on how to get the most out of any work you do.