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.)
- translate_routes translate your URLs and routing helpers (Raul Murciano)
- routing_filter transparently filter in/output of routes
- Globalize2 demo app including instructions for routing_filter, see the README (Sven Fuchs)
Discussions:
- Using default_url_options for preserving locale in URLs
- Locale routing/url generation filter
- URL and routing consideration