# Automatic localized routes using routing filter and user session

**URL:** <https://www.ruby-forum.com/t/automatic-localized-routes-using-routing-filter-and-user-session/184643>\
**Category:** Rails I18n\
**Created:** [March 5, 2010, 5:18pm UTC](https://www.ruby-forum.com/t/automatic-localized-routes-using-routing-filter-and-user-session/184643 "2010-03-05T17:18:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kristianm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@kristianm](https://www.ruby-forum.com/u/kristianm)\
**Post date:** [March 5, 2010, 5:18pm UTC](https://www.ruby-forum.com/t/automatic-localized-routes-using-routing-filter-and-user-session/184643/1 "2010-03-05T17:18:54Z")

</div>

I would like to add localization to my rails apps in the prefix form,  
fx /:locale/my/other/path/segments

As I understand it, you currently have to do sth like the following

map.resources :books, :path\_prefix =\> ‘/:locale’

and then something like:

book\_url(book, :locale =\> :da)

But I don’t want to have to rewrite all my generated urls!!!  
I would like to have the locale stored in the user session (as  
retrieved from the route, perhaps using Sven’s routing filter) and  
then auto insert the :locale options if it is present, so I don’t have  
to rewrite the urls.

filter

- always store route :locale entry in session[:locale]

book\_url(book)

# calls ActionDispatch:Routing.url\_for

# which dispatches to

ActionController::PolymorphicRoutes.polymorphic\_url

But I can’t seem to find the :locale option from here either in rails  
core or I18n.  
Should I simply monkeypatch polymorphic\_url and force-add the locale  
options to my liking or is there a better way? I thought Rails 3 was  
all about avoiding monkeypatches 😉

Proposed hack of a solution

def polymorphic\_url  
…

# add default locale from session (retrieved from prefix on route

path)  
args \<\< {:locale =\> session[:locale]} if session[:locale]  
**send** (named\_route, \*args)  
end

Any better ideas or solutions?

---

<div class="post-metadata">

**Author:** ![kristianm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@kristianm](https://www.ruby-forum.com/u/kristianm)\
**Post date:** [March 5, 2010, 6:12pm UTC](https://www.ruby-forum.com/t/automatic-localized-routes-using-routing-filter-and-user-session/184643/2 "2010-03-05T18:12:13Z")

</div>

As described on [Rails Internationalization (I18n) API — Ruby on Rails Guides](http://guides.rubyonrails.org/i18n.html), you can use  
default\_url\_options for that:

def default\_url\_options(options={})  
{ :locale =\> I18n.locale }  
end

2010/3/5 Kristian M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![kristianm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@kristianm](https://www.ruby-forum.com/u/kristianm)\
**Post date:** [March 5, 2010, 10:06pm UTC](https://www.ruby-forum.com/t/automatic-localized-routes-using-routing-filter-and-user-session/184643/3 "2010-03-05T22:06:21Z")

</div>

I just found this:

> <https://stackoverflow.com/questions/212425/creating-routes-with-an-optional-path-prefix>

Which was very old, but at the end had a recent link to this:

[http://www.artweb-design.de/2007/5/13/concise-localized-rails-url-helpers-solved-twice](http://www.artweb-design.de/2007/5/13/concise-localized-rails-url-helpers-solved-twice)

Which at the end proposed this simple solution 🙂

map.with\_options(:path\_prefix =\> “:locale”) do |m|  
m.resources :posts  
m.resources :stories  
end

And you have to add before filter to application controller to define  
locale, such as

before\_filter :define\_locale

def define\_locale  
if params[:locale] == nil  
I18n.locale = ‘en’  
else  
I18n.locale = params[:locale]  
end  
end

Yeah, I think that could work !!!

This should be a standard feature of Rails 3 as I see it and  
documented in the Rails 3 User Guide 😉  
This is how I imagine most people would use localization.

BTW:  
I have tried to port the translate\_routes to Rails 3. See  
[http://github.com/kristianmandrup/translate\_routes](http://github.com/kristianmandrup/translate_routes)  
I still need some help in understanding how to port the old route  
segments to the new style, which also supports optional segments.  
Please fork my branch and go from there 🙂
