You have a great Rails application. It’s so popular that folks are asking you publish it in additional languages. Oh that we could all be so lucky! It’s a problem faced by many successful projects. At some point you’re going to need to translate your application into other languages. Luckfully in Rails it is relatively painless to handle.
I18n Class in Rails
Rails has a special class that you can use to both translate strings and localize date / time objects. I18n is the main workhorse that provides internationalization support out-of-box. You can also use different a backend like Globalize2 (either as a complete replacement or hybrid solution). You can learn more bout the I18n API in the official Rails documentation.
Steps to Consider
Setting up your application to support additional languages you’ll need to think about the following:
- What is the default locale going to be?
- Where to keep and how to organize your locale dictionaries?
- How do you set the locale?
- How do you preserve the locale for the user’s session?
- How do you switch locale?
Default Locale and Dictionary Locations
Setting the default locale is easy. In the Rails config/environment.rb file you will find an option commented out for setting the default locale. Simply uncomment this line and set the locale to whatever default you desire.
config.i18n.default_locale = :en
While you’re in this file you’ll want to also uncomment the line that defines where the locale dictionaries are kept. The locale dictionaries are just YAML files that define string replacement. You’ll use these dictionaries to define ActiveRecord date and time formats, counts, and standard validation messages. It also can be used to define custom strings, used for string replacement. Below is a small sample of a dictionary file:
en: hello: "Hello world"
You can find more complete starter dictionaries by Svenfuchs on Github.com. I use these on my own projects to jump-start the translation process and highly recommend them.
<%= t('hello') %>
You can then use string replacement to facilitate translation, the code above would produce “Hello world” if the site was set to the ‘en’ locale.
Setting the Locale
The easiest way to set the locale is to have locale passed as params in the request. You could take more sophisticated approaches using URL prefixes, browser preference settings, etc. Below the code demonstrates a simple implementation using a locale param:
before_filter :set_locale def set_locale # if params[:locale] is nil then I18n.default_locale will be used I18n.locale = params[:locale] end
With this code in your ApplicationController visits to http://www.mysite.com:3000?locale=fr would load the French dictionary file.
Preserving Locale
Another problem you’ll need to contend with is preserving the locale with each subsequent request. The most usual way of handling this is to modify the default_url_options to always include the locale parameter:
# app/controllers/application_controller.rb
def default_url_options(options={})
{ :locale => I18n.locale }
end
Switching Locale
You’ll want to give users ways to switch their locale when they visit. I’m not going to go into details on how to do this because your implementation will be custom to you. The most popular is using a simple drop down that will redirect the user to the new url with the new locale parameter set. Another possibility, if you have users log in, is to provide a control panel or setting they can configure and redirect them upon login to the correct language.
