Internationalization in Rails 2.2

Tuesday, October 13, 2009

Filed under: Ruby, Ruby on Rails, web-development — Tags: , , , — Dmitri @ 08:41

This tutorials is about Internationalization in Ruby on Rails 2.2 and later. You don’t need to invent a wheel from scratch all stuff for internationalization and localization of your application is available in standard Rails package since version 2.2.

Configuration

So let’s start with empty Rails application.
$rails test_locale

In directory APP_ROOT/config/locales/ you will find file en.yml this is file which contains all texts for English version of your application. Here in Estonia we usually make applications for 3 or more languages. So I copy en.yml and name files as locale.

en.yml – for English
en:
  hello: "Hello world"

ru.yml – for Russian
ru:
  hello: "Привет мир!"

ee.yml – for Estonian
ee:
  hello: "Tere maailm!"

Now lets do a controller for test.
test_locale$ ruby script/generate controller SayHello index

Now open view file in APP_ROOT/app/views/say_hello/index.html.erb and edit like:
<%=t :hello %>
t – function is a helper that translates the text for current locale. If locale text is missing, it will generate message that “translation is missing” it doesn’t halt your application.

Locale will pass the parameter as query_string ?locale=ru and you need to add this manually for every URL in you application, of course you don’t like to do this manually. If you want that our app looks like http://www.example.com/ru/say_hello go to APP_ROOT/config/routes.rb end add following line
map.connect ':locale/:controller/:action/:id', :controller => 'say_hello', :action => 'index', :requirements => {:locale => /ee|ru|en/}

This will set for ROOT controller too. In requirement we set all available locales to avoid people enter in URL shitty things. After you set this route, Rails will generate pretty nice URLs with link_to, link_to_remote, etc…

Now we need to a function to set locale in every controller of you application. Open APP_ROOT/app/controllers/application_controller.rb and edit:
class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  protect_from_forgery
  before_filter :set_locale
  def set_locale
    locale = params[:locale]
    I18n.locale = locale
    I18n.load_path += Dir[ File.join(RAILS_ROOT, 'config', 'locales', '*.{rb,yml}') ]
  end

end

If you want to setup default locale different than English(en). Open file APP_ROOT/config/environment.rb and commentate following lines (usually at the end of file) and set up default locale. For example it will be Russian:
config.i18n.load_path += Dir[Rails.root.join('config','locales', '*.{rb,yml}')]
config.i18n.default_locale = :ru

Result

Lets see what we got:
$ruby script/server

Open the URL in your browser http://localhost:3000/say_hello

Default (we set by default Russian[ru] locale)

English

Estonian

Thats all! Really isn’t hard?

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

© 2008 Dmitri Smirnov