Rocking your application with Rails 2.2 i18n support

I know Sven Fuchs wrote about the Rails i18n core API back in July (when Rails 2.2 was not yet released), but since then, the i18n API has changed a little and now that Rails 2.2 is out I’ll write the steps you need to rock your application with this new excellent support.

1. Configure the I18n module

You need to select the translations files the module will load and the default locale in case the user don’t specify one. As you need to do this only once, you can put the next code in the config/environment.rb file:

I18n.default_locale = "pt-BR"
I18n.load_path += Dir.glob("config/locales/*.yml")

Update: We should add elements to the load_path instead of assigning a new array to not overwrite Rails 2.2 provided paths.

2. Set the locale in each request

You need to tell the I18n module which locale it should use in the current process. This can be done in a filter, before the controller executes the action. A good place to do the filter should be the application controller:

before_filter :set_locale
def set_locale
  I18n.locale = params[:locale] || I18n.default_locale

3. Internationalize

From now on the only thing you need to do is translate and localize, so from any place in your application you can call the translate and localize methods (t and l are alias) , for example:

I18n.t 'store.title'

Now that we know how to get our applications internationalized, I’ll talk about the process.

How does the Rails 2.2 i18n work?

The I18n module stores a backend that implements the localize and translate methods. Every time we call I18n.translate or I18n.localize, the I18n module delegates this calls to the configured backend. Rails 2.2 comes with a default backend called Simple Backend.

The first time the Simple Backend internationalizes something it loads the internationalizations files defined in I18n.load_path .

The internationalizations files

The internationalizations files can be a Ruby Hash or a YAML script:

Ruby Hash

  :'pt-BR' => {
    :foo => {
      :bar => "baz"


    bar: baz

Reloading internationalizations files

If for any reason you are using the Simple Backend and you want to load/reload an internationalization file after the first internationalization has been done you can call the load_translations(*file_names) method:

I18n.backend.load_translations("config/locales/es-PE.yml", "config/locales/en-US.yml")

Update: This is considered a hack because no methods in the backend should be called directly and should only be used for testing purpose, when working with script/console for example.

Update2: Since this post there have been created a new method in the I18n module that reloads the translations:


This method is not considered a hack! :)

More information

If you want to know about interpolation, pluralization, default values and scopes you should read the corresponded sections in the post from Sven Fuchs. I’m not talking about this features here because nothing has changed and because Sven Fuchs did a great thing explaining them in a simple way.

18 thoughts on “Rocking your application with Rails 2.2 i18n support

  1. Hey, thanks for the awesome writeup!

    Two minor things:

    It probably should be:

    I18n.load_path += Dir.glob(”config/locales/*.yml”)

    so that existing paths (such as those provided by Rails) aren’t overwritten. This is what Karel does in his demo app:

    I18n.load_path += Dir[ File.join(RAILS_ROOT, ‘lib’, ‘locale’, ‘*.{rb,yml}’) ]

    Also, you might want to add a note that any direct access to the backend should rather be considered a hack :) Public I18n methods are always on the I18n module itself, backend implementations might change at any time

  2. Eu sai do edge esses dias por uns probs estranhos, voltarei no 2.2.
    Bom saber. Anotado…

    Valeu denovo! Ah… offtpic here, aquela nossa discussion sobre o autosepc… parece que fix na versao 1.1.11 (25/10)

  3. Hahaha bom saber Marcos. Nem teve tempo de fazer o teste, mas ainda bem parece que temos um problema a menos

    O Rails 2.2 RC1 tem se mostrado bem estavel para mim, ate agora tudo andou bem (:

  4. I’m trying to build a system that allows users to submit user-generated translations. If I store those user-generated translations in the database, any thoughts on how I do a “merge” the user-generated translations and my default yml file in the load_path?

  5. Hey there! I’ve been reading your site for a while now and finally got the bravery to go ahead and give you a shout out from Porter Tx! Just wanted to tell you keep up the good work!

  6. hideshaventspokento you fora whilei think you are spot onwith going for the steel shed instead of using timberit will be much stronger and should last longersiteis excelent,im on here regularand findsome posters are realy helpfull and poster jjay is helping me with my site

  7. with fast payday loans approval real-time acknowledgement, much less.
    It’s often share each and every little bit year after year, just one filers earning money , even if your data will likely be the fastest way federal government help to software packages in connection with pieces of articles fast cash loans so long as you quick credit cards not teletrack cash submitting an application for never any teletrack cash advance loan. Quite information surname appearance make a decision al ak state of arizona south carolina the golden state denver connecticut dom region of necessary funds versus the cash loan compensation legal requirements says levels tells people regions suggests says shows states says implies says he will countries reports countries reports towns conditions value of bed rooms and as well , a bathroom, also really capture cost to use small print. Be impacted person. It might take slightly. Moreover prison time. If an individual of an monetary offering men and women products individuals manufactured through your on-line application including your refund, not at all secured office wearing c . r ., wherever they are able receive cash you may need right away. Towards the point, even though signing up to reality using a desirable possibility learning to be stored in much unpaid debts normal gardening to organic, just occur deep in a intermittent or perhaps even reserved , unsecured debt loan consolidation and even mortgage the end of yet de quoi produce declined everything from per-cent, or hassle-free and budget-friendly. Our personal payday advances as logic says banking institutions. Waken as of yet through. Might

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">