Every repository with this icon (
Every repository with this icon (
Notes on root route + prefix on default locale
The root route (“/” route) is handled differently depending on the prefix_on_default_locale configuration option. If prefix_on_default_locale is set to true, the root route remains untouched and all localized versions of this root route are generated.
This is better explained with an example. Given the following single route definition on config/routes.rb:
map.root :controller => “home”, :action => “index”Without translate_routes the generated routes are:
root / {:controller=>"home", :action=>"index"}With translate_routes and prefix_on_default_locale set to false the routes are:
root_es / {:action=>"index", :controller=>"home", :locale=>"es"} root_en /en {:action=>"index", :controller=>"home", :locale=>"en"}And finally with translate_routes and prefix_on_default_locale set to true the routes are:
root / {:action=>"index", :controller=>"home"} root_es /es {:action=>"index", :controller=>"home", :locale=>"es"} root_en /en {:action=>"index", :controller=>"home", :locale=>"en"}It’s important to note that in this last case the original root route does not set the locale param. This allows controller code to distinguish the “unlocalized root” path (“/”) from the “localized root” paths (“/es”, “/en”) and to redirect the user to a suitable default locale from the unlocalized root. The following example shows how to do it with the excellent [http_accept_language plugin](http://github.com/iain/http_accept_language):
def index available_locales = [ “es”, “en”] unless params[ :locale]- if the locale is not specified in the URL we are in the unlocalized root path
- we find the first available locale from the user agent’s Accept-Language header
request.user_preferred_languages.each do |locale|
if available_locales.include?( locale)
redirect_to :locale => locale # we found one, redirect and finish
return
end
end - no suitable locale found, redirect to the default one
redirect_to :locale => I18n.default_locale
return
end - locale is set, handle the action as usual
…
end







