Home arrow Ruby-on-Rails arrow Page 3 - Going Global with Multiple Language Support

Going Global with Multiple Language Support

In this second part of a four-part article series on adding support for multiple languages to a Ruby-on-Rails ecomerce application, you will learn how to use Globalize to add this important capability. This article is excerpted from chapter 10 of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
June 30, 2010
  1. · Going Global with Multiple Language Support
  2. · Setting Up Globalize
  3. · Implementing the Change Locale User Story
  4. · Implementing the Translation User Stories

print this article

Going Global with Multiple Language Support - Implementing the Change Locale User Story
(Page 3 of 4 )

The base language, or default locale, for Emporium is US English. The user should be able to change to another language by clicking a link. For example, by accessing the following URL, the user can change the locale to British English:


Accessing the following URL changes the locale to Swedish:


Note that the link contains the locale parameter, which tells your system which locale the user wants to use. The code that changes the locale is easily implemented as a before_filter . As explained in Chapter 5, filters allow you to run code before, around, and after the controllerís action is called. In our case, the set_locale filter (Listing 10-1) is called before anything else is called.

Next, open app/controllers/application.rb in your editor and add the code for the set_locale method shown in Listing 10-1.

Listing 10-1. The set_locale Filter

class ApplicationController < ActionController::Base
before_filter :set_locale


  def set_locale
    accept_lang = request.env['HTTP_ACCEPT_LANGUAGE']
    accept_lang = accept_lang.blank? ? nil : accept_lang[/[^,;]+/]

    locale = params[:locale] || session[:locale] || accept_lang || DEFAULT_LOCALE

      Locale.set locale
      session[:locale] = locale
      Locale.set DEFAULT_LOCALE


The filter looks for a user-specified locale from the request parameters. If the locale is not found in the request parameters, it looks for a locale stored in the session. If the locale is not found in either the request or session, it tries to use the HTTP_ACCEPT_LANGUAGE header, which is sent out by the browser. Note that the algorithm falls back on the default locale, which we defined in the environment.rb configuration file, if all other methods of discovering the userís locale fail.

Tip  To set the HTTP_ACCEPT_LANGUAGE HTTP header in Firefox, select Tools ? Options and click the Advanced icon at the top of the Options dialog box. Then click the Edit Languages button on the General tab, as shown in Figure 10-2.

Lastly, the filter stores the locale in the session, so users donít need to change the locale every time they navigate to a new page. The filter falls back on the default locale if the user specifies an invalid locale.

Figure 10-2.   Setting the preferred language with the HTTP_ACCEPT_LANGUAGE header

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials