How to encode the current locale in the url

With a multilingual application you often want to encode the current locale in you URL. This is something that is not easily done with Rails’ routing system though.

Solutions

There are currently three main approaches to this problem.

The translate_routes plugin by Raul Murciano lets you translate your URLs and use routes like this:


/es/contacto {:lang=>"es", :controller=>"contact", :action=>"index"}
/contact     {:lang=>"en", :controller=>"contact", :action=>"index"}    

The routing_filter plugin by Sven Fuchs provides the same kind of URL design but takes a different approach. It wraps around the Rails routing system and let’s you define filters in a generic way (which is useful in other situations, too).

Also, it is possible to use the default_url_options feature of Rails to preserve the locale in URLs. This approach is “native” to Rails but somewhat limited, too. (See the mailinglist discussion linked below.)

Discussions:

Pre Rails 2.2 resources

revision: 1 last updated: June 02, 2009 15:33 by: Marko Seppä