>

December 29, 2015

Managing Rails Translations

Translations in your Rails app

The tech behind managing the localised versions of your rails app is very simple. Instead of using a normal string in your html, such as

<div>
  Welcome to this great website
</div>

You would use the translate method made availble to you via the i18n gem in Ruby, and it would look something like this

<div>
  <%= t("welcome_to_site") %>
</div>

Now, assuming that the version of the site you are looking at is English, which is generally the default that is set by rails, it will go to a file in your config/locales directory of your application, in this case en.yml, and to serve this content it would look like this

en:
  welcome_to_site: Welcome to this great website

Easy enough, and nice and simple to get started, however over time your application will grow, and you need to manage the contents of these files, and that will mean as you build new features, which parts of your en.yml file are translated into other languages, and which parts are not. For a small file this could be easy enough, but when it becomes thousands, maybe tens of thousands of words long, this becomes difficult to manage.

Enter i18ndiff

When I ran up against this, it was certainly frustrating, so I rolled up my sleeves and wrote some code, and the i18ndiff ruby gem was the result. It takes two versions of your translations yaml file, an older version, one that has already been translated in full and put into production, and a new one, that contains that old file and all the new stuff that you want to get translated for your users. Using it is pretty simple, first up of course you need to install, and this is a standard ruby gem install:

gem install i18ndiff

Using it is also quite simple:

i18ndiff /path/to/older/file.yml /path/to/newer/file.yml

In order to test the gem I have two small en.yml files that I used and they looked like this:

Old file:

en:
  activerecord:
    errors:
      not_found: "is not found!"
      cant_be_blank: "can't be blank!"
  welcome: "Welcome to this site"
  login: "Please login"

New File:

en:
  activerecord:
    messages:
      thing: "this is a made up thing"
    errors:
      not_found: "is not found!"
      cant_be_blank: "can't be blank!"
  welcome: "Welcome to this site"
  login: "Please login"
  register: "Please register"
  logout: "Logout"

In this simple example, running these files through the gem would give you the new parts of your file that has only the new words that need to be translated, in the existing structure of you file, as follows.

en:
  activerecord:
    messages:
      thing: this is a made up thing
  register: Please register
  logout: Logout

A nice easy way to have this packaged up into a file ready to used for translating is to run it as follows:

i18ndiff /path/to/older/file.yml /path/to/newer/file.yml > /path/to/diff.yml

Now you have an easier to work with version of your translations file to get translated, which I am sure will be appreciated by the poor soul who has to perform the translations.