Multilingual Websites with Ruby on Rails

Part 1 – Overview

Writing software for several languages is an ever recurrent challenge. Ruby on Rails provides solutions to most of the standard problems in web development. But it needed time until the version 2.2. for the “Internationalization API ” to become an official part of Ruby on Rails. .

The Ruby-On-Rails Internationalization module

Structure of the framework

The framework consists of a “public” and a “private” part. The I18N module forms the public part with the methods that we call in our code. The private backend is interchangeable. The standard implementation reads the texts from files in Yaml format or from normal ruby files.

Calling

In our code, we replace all the text to be translated with the I18N.translate() method. Thanks to an alias definition, we can simply write t(). We give a key as parameter, and the backend uses this key to look up the correct translation.

The key is a string or a symbol. Normally, look up by symbol is slightly more efficient. But since the key gets decomposed into its components during the processing, I expect that this advantage disappears.

In a typical web application, many of the texts to be translated are found in the views, ie. Erb templates. As an abbreviation for pure HTML text, there is the following tag:

<%=t 'key' %>

Organization of keys

The keys have a hierarchical structure. The suggested organization is:

“Controllername.viewname.key”, for example ‘companies.show.header’

The language files

The language files can either be Ruby code files or text files in Yaml format. The person who translates is often a person without Ruby knowledge. That is why the Yaml file format is recommended.

The format looks like this:

de:
  users:
    show:
      header: "Company Data"
      intro: "For this company, we have stored the following data:"

The first key part the language code. This has the disadvantage that it separates the text entries of different languages. To help the translators, you can insert comments by marking a line with a beginning ‘#’ character . And of course, you should name the keys as clear as possible.

But even then, when developer and translator are not the same person, I think the Yaml file format is not optimal.

Part 2 of this article will soon be translated to English.

Join the Conversation

1 Comment

  1. Hello Meier! If you need to localize your Rail app into multiple languages, there are some alternative tools, but I suggest to go for POEditor software localization management platform; the work interface is neat and simple to handle. The tool doesn’t support .yaml files now, but , you can convert them to po formats using this free converter tool http://yml2po.com/. It worked for me, maybe it can help you too.

Leave a comment