stalno u pokretu

< IT & tech

17. 09. 2014. back end * i18n&l10n * ruby

i18n gem advanced features – Ruby on Rails internationalization

I originally published this article on Lingohub blog in 2013, while working for the company. Lingohub specializes in providing app localization for developers and translators.</a>

Previous article on internationalization for Ruby has covered the basic use of the i18n gem – currently one of the most popular internationalization solutions in the Ruby world. It contains all the information on setup and usage that most Ruby, Ruby on Rails, Sinatra or Padrino developers will need. However, there is much more to this gem. This article will highlight some i18n gem advanced features in Ruby on Rails internationalization.

Using Different Backends for Ruby i18n gem

I18n gem comes with the Simple backend by default. It can load resource files in YAML or Ruby hashes, but can store new translations only in the memory – it is not possible to save them. This can be a problem if the translation support needs to be built into the application. Also, sometimes for one reason or another, the use of YAML or Ruby hash resource file formats is not an option.

With the i18n gem, other backends can easily be used instead of Simple backend, for example Key-Value, ActiveRecord, or Gettext.

Backends can also be combined in the Chain backend, which can be useful when the standard translations are used with the Simple backend but custom application translations are stored in a database or other backends.

Next up, I will describe some of the optional backends that come with the gem, but if there is no backend that suits some special need, one could be made from scratch without too much effort.

Key-Value backend

The store that the backend receives on initialization responds to three methods:

  • store# – Used to get a value
  • store#[ ]=(key, value) – Used to set a value
  • store#keys – Used to get all keys

Since these stores only support strings, all values are converted to JSON before being stored, allowing it to also store booleans, hashes and arrays. However, this store does not support Procs. Symbols are just supported when loading translations from the filesystem or through explicit store translations.

To initialize the backend, a new file should be created in the /config/initializers directory:

translations = {}
I18n.backend =

An empty store is available now. New translations can be added by calling the store_translations method on the I18n.backend:

I18n.backend.store_translations(locale, { key => value }, :escape => false)

Translations hash can be accessed through the store:

<pre>translations =

Key-Value backend is all set now, but since the values are being stored in a Ruby Hash, they will be lost after the web server is restarted. To avoid that, a persistent key-value store should be used, for example Redis.

Installing Redis from repository and starting the server:

$ apt-get install redis
$ redis-server

The redis gem should be added to the Gemfile ( gem ‘redis’ ) and bundle command run. Now, instead of a blank hash, a new Redis object should be passed to the backend initializer:

# /config/initializers/i18n_backend.rb
I18n.backend =

Detailed instruction on setting the Key-Value backend with Redis can be seen in this railcast.

ActiveRecord i18n backend

Support for the ActiveRecord i18n backend has been extracted to a gem of its own: i18n-active_record. To install the gem, this should be put in the Gemfile prior to bundling:

gem 'i18n-active_record',
    :git => 'git://',
    :require => 'i18n/active_record'

Next, an active record model named Translation should be created. The purpose of the model is for managing translations and languages. The migration should look like this:

class CreateTranslations < ActiveRecord::Migration
  def self.up
    create_table :translations do |t|
      t.string :locale
      t.string :key
      t.text   :value
      t.text   :interpolations
      t.boolean :is_proc, :default => false


  def self.down
    drop_table :translations

To load the backend, a new file should be added to config/initializers named locale.rb :

require 'i18n/backend/active_record'
I18n.backend =

After the backend is loaded, the translations can be looked up or stored as previously described.

An optional module Missing can be included. It creates empty records in the database for all the missing translations:

I18n::Backend::Chain.send(:include, I18n::Backend::ActiveRecord::Missing)
I18n.backend =,

Stub records for pluralizations will also be created for each key defined in i18n.plural.keys.

A Gettext backend for i18n

I18n library provides an experimental support for using Gettext po files for storing translations. To enable the support, I18n::Backend::Gettext module should be included in the Simple backend or other backend that is currently used:


After the module is included, it is possible to load the po files with the I18n.load_path:

I18n.load_path += Dir["path/to/locales/*.po"]

Following the Gettext convention this implementation expects that the translation files are named by their locales. E.g. the file en.po would contain the translations for the English locale.

The translations can be looked up the same way as for the Simple backend:

t('some msgid')
# => 'msgstr for some msgid'

Chaining backends

Using multiple backends at the same time is also possible. This can be achieved via the Chain backend. On initialization, it can receive any number of other backends as arguments. For example ActiveRecord backend can be used with a fall back to the default Simple backend:

first =
first.store_translations(:en, :foo => 'foo')

second = I18n.backend
second.store_translations(:en, :bar => 'bar')

I18n.backend =, second)

I18n.t(:foo)  # => "foo"
I18n.t(:bar)  # => "bar"

The cost of replacing the Simple backend and implementing a built-in support for the translation process in each application can be the loss of simplicity and possibly performance. Additional time is also needed to implement and maintain an interface through which the translation would be conducted. On the other hand a specialized professional service can be used instead. You can let Lingohub assume this burden, which lets you focus on developing the application, and our platform takes care of the internationalization details.

Caching is an i18n gem advanced feature

To enable caching, the Cache module should be included in the Simple backend, or whatever other backend is being used:

I18n::Backend::Simple.send(:include, I18n::Backend::Cache)

Also a cache store implementation should be set, for example ActiveSupport::Cache:

I18n.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)

Any cache implementation that provides the same API as ActiveSupport::Cache can be used (only the methods #fetch and #write are being used).


I18n locale fallbacks allow use of translations from other locales when translations for the current locale are missing. This can be useful in several cases:

  • when the application is thoroughly localized to include even the regional variants of a language; in this case regional locale resource files would contain only the phrases that differ, but would fall back to the common base locale (:en-US, :en-CA… with fallback to :en)
  • when it is acceptable to use a temporary fallback for missing translations to some other language that the users are expected to know, in order to provide them with some content until it is localized; the fallback language can be either some mutually intelligible language with the current language, one of the world languages or one of the languages widely spoken in the target community.
  • for testing purposes in the development or during the localization process.

Locale fallbacks can be enabled by including the Fallbacks module to the backend that is being used in the application:


This in fact overwrites the Base backend translate method in order to force it to try each locale given by the I18n.fallbacks until it finds a result. Otherwise it will throw the MissingTranslation.

If the :default is passed to the translation lookup method, it takes precedence over the fallback only if it is a Symbol. When the default contains a String, Proc or Hash it is evaluated last after all the fallback locales have been tried.

Fallbacks = I18n.Fallback :'en-CA' => :'en-US', :'en-US' => :en, :de => :en )
# => [:en]

Translation Metadata

I18n translation metadata is useful when accessing information about the way a translation was looked up, pluralized or interpolated.

msg = I18n.t(:message, :default => 'Hi!', :scope => :foo)
# => { :key => :message, :scope => :foo, :default => 'Hi!' }

If :count option was passed to #translate it will be set to the metadata. Likewise, if any interpolation variables were passed they will also be set.

Translation metadata can be enabled by simply including the Metadata module into the Simple backend class – or whatever other backend is being used:


Cascading lookups

Cascading lookups can be done in backends that are compatible with the Simple backend.

This means that for any key that can not be found, the Cascade module strips one segment off the scope part of the key and then tries to look up the key in that scope.

For example when a lookup for the key :”” does not yield a result then the segment :bar will be stripped off the scope part :”” and the new scope :foo will be used to look up the key :baz. If that does not succeed then the remaining scope segment :foo will be omitted, too, and again the key :baz will be looked up (now with no scope).

Cascading lookup can be enabled by passing the :cascade option:

I18n.t(:'', :cascade => true)

This will return the first translation found for :””, :”foo.baz” or :baz in this order.

The cascading lookup takes precedence over resolving any given defaults. This means that the defaults will kick in after the cascading lookups haven’t succeeded.

This behaviour is useful for libraries like ActiveRecord validations where the library wants to give users a bunch of more or less fine-grained options of scopes for a particular key.

It is possible to set key of one key-value pair as a value of another pair. This way both pairs would have the same value, but it would be set only at one place.

  something: some phrase
  something_else: :something
I18n.t :something_else
 # => "some phrase"

Valid use for this would be situation where the key-value pairs that share values are already structured (for example localized libraries or gems). Or if two unrelated pairs, for example one for model and the other for the view, should share the value, perhaps it is better that they both point to a third shared pair, then to use the localization of the model for the localization of the view or vice versa.

Using custom exception handlers

The I18n API defines the following exceptions that will be raised by backends when the corresponding unexpected conditions occur:

MissingTranslationData       # no translation was found for the requested key
InvalidLocale                # the locale set to I18n.locale is invalid (e.g. nil)
InvalidPluralizationData     # a count option was passed but the translation data is not suitable for pluralization
MissingInterpolationArgument # the translation expects an interpolation argument that has not been passed
ReservedInterpolationKey     # the translation contains a reserved interpolation variable name (i.e. one of: scope, default)
UnknownFileType              # the backend does not know how to handle a file type that was added to I18n.load_path

The i18n API will catch all of these exceptions when they are thrown in the backend and pass them to the default_exception_handler method. This method will re-raise all exceptions except for MissingTranslationData exceptions. When a MissingTranslationData exception has been caught, it will return the exception’s error message string containing the missing key/scope.

The reason for this is that during development it is probably preferred to render the views even though a translation is missing.

In other contexts this behavior can be changed, e.g. the default exception handling does not allow to catch missing translations during automated tests easily. For this purpose a different exception handler can be specified. The specified exception handler must be a method on the I18n module or a class with #call method:

module I18n
  class JustRaiseExceptionHandler < ExceptionHandler
    def call(exception, locale, key, options)
      if exception.is_a?(MissingTranslation)
        raise exception.to_exception

I18n.exception_handler =

This would re-raise only the MissingTranslationData exception, passing all other input to the default exception handler.

However, if you are using I18n::Backend::Pluralization this handler will also raise I18n::MissingTranslationData: translation missing: en.i18n.plural.rule exception that should normally be ignored to fall back to the default pluralization rule for English locale. To avoid this translation key could be additionally checked:

if exception.is_a?(MissingTranslation) &amp;&amp; key.to_s != 'i18n.plural.rule'
  raise exception.to_exception

Another example where the default behaviour is less desirable is the Rails TranslationHelper which provides the method #t (as well as #translate). When a MissingTranslationData exception occurs in this context, the helper wraps the message into a span with the CSS class translation_missing.

To do so, the helper forces I18n#translate to raise exceptions no matter what exception handler is defined by setting the :raise option:

I18n.t :foo, raise: true # always re-raises exceptions from the backend

These were just some of the** i18n gem advanced** features that seemed most relevant. You can easily find some more if you browse through the source code of the i18n gem. Extending the existing functionality by adding custom features is also not very hard.

Even though i18n is probably the internationalization solution for Ruby that you were looking for, the final two articles on internationalization for Ruby that we have prepared will introduce some alternatives to it and will give you more complete picture. Stay tuned for more, follow us on the social media channels and please feel free to leave some feedback in the comments, I would love to hear your thoughts about the tutorial.

Further reading

Related posts

17. 09. 2014. back end * i18n&l10n * padrino * ruby * sinatra
Internationalization for Ruby – i18n gem

i18n for plain Ruby & the i18n gem, i18n for Ruby on Rails, i18n for Sinatra, i18n for Padrino
[ reading time: ~ 20 min. ]

17. 09. 2014. back end * i18n&l10n * ruby
Internationalization for Ruby with the r18n gem

Installation and setup, translation lookup, filters for translations processing, pluralization, translating activeRecord models and plain Ruby objects, locale settings, r18n wrappers for rails and sinatra, r18n vs i18n
[ reading time: ~ 9 min. ]

17. 09. 2014. back end * gettext * i18n&l10n * ruby
Ruby gettext internationalization tutorial on using the fast_gettext gem

Setup, translation lookup, managing translations (MO/PO, db, yaml), rails plugin, pluralization, defaults, multiple repositories, logging...
[ reading time: ~ 9 min. ]

15. 08. 2014. api * back end * ruby
API - design, implementation, testing

notes and resources
[ reading time: ~ 3 min. ]

01. 09. 2014. api * back end * bdd / tdd * cucumber * ruby * ruby gem
Developing rails REST API with Cucumber

json_spec, writing the definitions, checking the http headers, testing authentication...
[ reading time: ~ 3 min. ]

03. 09. 2014. api * back end * ruby * ruby gem * security
Devise as authentication solution for rails API

Authentication token, the use of sessions, intsallation and setup, configuring the model, the routes, application controller, custom devise controllers, testing the controllers
[ reading time: ~ 9 min. ]

07. 09. 2014. api * back end * ruby
Building a JSON API in rails

[ reading time: ~ 1 min. ]

12. 09. 2014. back end * ruby
ActiveRecord notes

Uniqueness on a combination of fields, inclusion in array, getting all model associations, overriding model getters and setters, serialization...
[ reading time: ~ 2 min. ]

17. 09. 2014. back end * cakephp * codeigniter * i18n&l10n * php * symfony * yii * zend
Internationalization How To for the 5 most popular PHP frameworks

In this how-To you will read about using CodeIgniter, CakePHP, Zend, Yii and Symfony
[ reading time: ~ 8 min. ]

17. 09. 2014. back end * fuelphp * i18n&l10n * laravel * php
PHP internationalization frameworks: Laravel and FuelPHP

The article covers two more PHP frameworks that are frequently used, Laravel and FuelPHP
[ reading time: ~ 6 min. ]

17. 09. 2014. back end * i18n&l10n * php
PHP internationalization – i18n mechanisms tutorial

Static web and internationalization, dynamic web applications, localizing strings directly in the code, storing the strings in a relational database, message catalogues (string arrays), JSON, use of resource files
[ reading time: ~ 10 min. ]

17. 09. 2014. back end * gettext * i18n&l10n * php
PHP internationalization with gettext tutorial

Installation, portable object template files, plurals, an example of a PO file, directory structure, machine object files, gettext caching problems, setting up PHP for internationalization with gettext...
[ reading time: ~ 18 min. ]

17. 09. 2014. back end * i18n&l10n
Resource file formats

ini files, properties files, iOS .strings files, YAML files, RESX and RESW files, RESJSON files
[ reading time: ~ 4 min. ]

22. 11. 2014. back end * deployment * ruby
rails deployment

Setting up Debian 7 server on VPS, nginx, php-fpm, ruby, unicorn/phusion passanger, essentials, ssh keys, capistrano...
[ reading time: ~ 2 min. ]

10. 08. 2014. api * back end
Representational state transfer (REST)

[ reading time: ~ 3 min. ]

21. 08. 2014. database * ruby
Adding UUID to rails

[ reading time: ~ 1 min. ]

04. 09. 2014. api * back end
Design principles of the REST-ful JSON API (HATEOAS)

REST, JSON, API, HATEOAS, media types, top level resource representations, url templates, compound documents, urls, responses, errors, JSend response types, HTTP Status codes
[ reading time: ~ 12 min. ]

24. 11. 2014. back end * dev_tool
nginx notes

[ reading time: ~ 1 min. ]

10. 12. 2014. back end * ruby gem

[ reading time: ~ 1 min. ]

Ovaj sajt ne sadrži first party kukije i druge mehanizme za aktivno praćenje poseta/ponašanja (facebook, google analytics, itd...). Na nekim stranicama se nalaze embedovani youtube video klipovi i google mape koji učitavaju svoje kukije.