Whenever we hear about Internationalization of web application, the first word that comes to our mind is "Translation". There are three ways to translate software:

  • Do it Yourself
  • Pay Someone for doing Translation
  • Ask your Users
translation

No matter what, making the software 'Internationalization ready' is the developer's job. No static strings, dates in the code and so forth; every text must be put in resource files and the code decides which text to use. Luckily Ruby on Rails provides a simple ruby gem called "I18n" (internationalization) that does exactly that.

Definition of Internationalization & Localization :

First of all let's check the definition of Internationalization & Localization. According to Wikipedia,

i18n - "Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes." This means, the "Internationalization" functionality translates all string and other locale specific information ( like date and currency formats) from your application in desire language to end user.

l10n - "Localization is the process of adapting software for a specific region or language by adding locale-specific components and translating text." This means, the "Localization" functionality gives translations in local-specific language and other localized formats for local specific information.

How i18n mechanism works in Rails :

All the languages are different from each other in many ways (For example pluralization rules). It is very difficult for any tools/software to provide a general solution for language translation. To solve this problem I18n API provides two features in Ruby on Rails.

  1. Give support for English and other similar languages out of the box
  2. Provide a user-friendly and adjustable functions for supporting other languages

This means in Ruby on Rails every static string has been internationalized and you can "over-ride" these defaults with "localization" of the application.

There are two parts of Ruby I18n gem which are:

  1. The public I18n API - provides a Ruby module with public methods to characterize the work of the library
  2. A default backend - which executes these public methods

The Public I18n API comes with two most important methods which are:

  1. translate # This public method do translations of the text
  2. localize # This public method localize Date and Time objects to local formats as per settings

These method have aliases #t and #l respectively, so you can use them like this:

[gist id=772983]

Setup/Configuration about the i18n mechanism

You can easily setup ruby I18n gem with few simple steps and use in your application for I18n support. Ruby on Rails application set up reasonable defaults in all .rb and .yml files in config folder. If you want to change the settings, you can easily change them according to your need.

To check the defaults for I18n module you have to check config/locales directory, which is default path for translation.

You will get one file named en.yml which default locale and contains these translation strings:

[gist id=773002]

English is default locale in I18n library. So, if your application is not set in different locale, :en will translate all the static text changes.

So, when you check this on console the output shows like following :

[gist id=773005]

Some other available locales which are supported by rails 3 are:

ar, bg, bn-IN, bs, ca, cy, cz, da, de, de-AT, de-CH, dsb, el, en-GB, en-US, eo, es, es-AR, es-CO, es-MX, es-PE, et, eu, fa, fi, fr, fr-CA, fr-CH, fur, gl-ES, gsw-CH, he, hi-IN, hr, hsb, hu, id, is, it, ja, ko, lo, lt, lv, mk, mn, nb, nl, nn, pl, pt-BR, pt-PT, rm, ro, ru, sk, sl, sr, sr-Latn, sv-SE, sw, th, tr, uk, vi, zh-CN, zh-TW

The easiest method to set your own locales to override the default locale is to change config/application.rb file in Rails 3. (You can apply same settings to config/environment.rb in Rails 2). Add this line to get German language as default locale:

[gist id=773012]

The first line of code tells the location of translation file to the I18n library. The second line of code will set the default locale to :de other than :en.

Using the I18n module

Put these lines in config/locale/de.yml.

[gist id=773014]

Let's check now whether this settings translate "hello" into "German" locale or not.

[gist id=773017]

Using the I18n with interpolation

Most of the time you want to use various variables with your translations. That's why I18n API also provide interpolation feature.

[gist id=773020]

Using the I18n with pluralization In English there are only one singular and one plural form for a given string, e.g. “1 friend” and “2 friends”. The best thing is I18n API also provides a flexible pluralization feature in Rails applications.

We must note that an interpolation variable named :count plays key role in translation and pluralization.

[gist id=773024]

The algorithm for pluralization is: [gist id=773027]

I.e. here 1 denotes singular and 0 denotes plural. So as specified using conditional operator, if count == 1, it will consider it as singular form otherwise its plural.

That's it for Part I. In Part II we will check other features of I18n like

  • Localization of Times/Dates
  • Using views for translation
  • Translating ActiveModel errors
  • Organization of resource files
  • Setting the locale (from domain, parameter or client) and of course storing it in the database when the user is logged in

For more details you can check http://ruby-i18n.org/ .

Try lingohub 14 days for free. No credit card. No catch. Cancel anytime