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?