locale routing/url generation filter

234 views
Skip to first unread message

Sven Fuchs

unread,
Sep 20, 2008, 12:51:10 PM9/20/08
to rails-i18n
Passing locales as the leftmost path segment has always been a bit of
a hassle with Rails Routing.

Although there are a couple of partial solutions [1] all of them have
some kind of annoying shortcoming, be it that one can not use non-
named, old-school routes with it or even has to pass lots of
parameters to url_for in certain cases and none of them gave me the
flexibility i wanted.

So I came up with a wild hack that wraps around the whole Rails
routing system instead of trying to solve things from the inside [2]
(and can actually be used for a variety of other interesting
purposes). I've now updated this plugin to work with the I18n API and
added it to a branch in our Globlize2 demo application [3]

The gist: when you have a default locale :en defined it transparently
changes route recognition and generation so that you get:

http://host.com/foos/1
http://host.com/de/foos/1

The former is generated when the current locale is the default locale.
The latter is generated when either the current locale is :de or one
passes the locale as an option to the url_helper like
foo_path(foo, :locale => :de)

Both urls are recognized without the need to modify the routes. The
filter just strips the locale from the beginning of the recognized
path and puts it to the returned params hash, so one can use it in,
e.g. the ApplicationController.

This is still experimental, but I'd appreciate some feedback,
especially if there are cases for url generation that do not work
(Rails routes are probably one of the most arcane and weird parts of
Rails, so it's pretty likely that I've overlooked something)

[1] http://www.artweb-design.de/2007/5/13/concise-localized-rails-url-helpers-solved-twice
[2] http://github.com/svenfuchs/routing-filter
[3] http://github.com/joshmh/globalize2-demo/tree/routing-filter

Raul Murciano

unread,
Sep 20, 2008, 1:14:45 PM9/20/08
to rails...@googlegroups.com
2008/9/20 Sven Fuchs <sven...@artweb-design.de>:

> Although there are a couple of partial solutions [1] all of them have
> some kind of annoying shortcoming, be it that one can not use non-
> named, old-school routes with it or even has to pass lots of
> parameters to url_for in certain cases and none of them gave me the
> flexibility i wanted.

Hi Sven,

Please excuse my insistence - I wrote a message about this just
yesterday - but there is another solution which, as you propose, wraps
around the routing system and manages helpers on a very confortable
way:

http://github.com/raul/translate_routes/

During the last months I've received some feedback from european and
latin-american developers and they are using it succesfully on
production environments, so it's a bit more mature than your
experimental suggestion. Maybe it could be a more solid base to build
on, what do you think about it?

--
Raul Murciano - Freelance Web Developer
http://raul.murciano.net

Sven Fuchs

unread,
Sep 20, 2008, 1:46:26 PM9/20/08
to rails...@googlegroups.com
Hey Raul,

On 20.09.2008, at 19:14, Raul Murciano wrote:
> 2008/9/20 Sven Fuchs <sven...@artweb-design.de>:
>> Although there are a couple of partial solutions [1] all of them have
>> some kind of annoying shortcoming, be it that one can not use non-
>> named, old-school routes with it or even has to pass lots of
>> parameters to url_for in certain cases and none of them gave me the
>> flexibility i wanted.
>
> Hi Sven,
>
> Please excuse my insistence - I wrote a message about this just
> yesterday -

Apparently I've failed to read it thoroughly. I thought it's about
translating path segments. Sorry about that.

> but there is another solution which, as you propose, wraps
> around the routing system and manages helpers on a very confortable
> way:
>
> http://github.com/raul/translate_routes/
>
> During the last months I've received some feedback from european and
> latin-american developers and they are using it succesfully on
> production environments, so it's a bit more mature than your
> experimental suggestion. Maybe it could be a more solid base to build
> on, what do you think about it?

Very cool! :)

I'll have to have a closer look at it.

From what I grasp from the README one would use named route
url_helpers like home_es_path to generate urls?

Does this also work with the url_for helper?

I've added this to the wiki (see http://rails-i18n.org/wiki)

Thanks!

Raul Murciano

unread,
Sep 20, 2008, 2:10:32 PM9/20/08
to rails...@googlegroups.com
2008/9/20 Sven Fuchs <sven...@artweb-design.de>:

> Apparently I've failed to read it thoroughly. I thought it's about
> translating path segments. Sorry about that.

In fact translating path segments is part of the job, but it is done
after routes generation so it doesn't need to mess with that exotic
areas of code :D

And of course you have nothing to apologize for: I arrived quite late
to the i18n party so it's my fault :D

> From what I grasp from the README one would use named route
> url_helpers like home_es_path to generate urls?

Yes. At translation time some helpers like home_es_path are generated.
At execution time the home_path look for the currently selected
language - let's say 'es' - and calls home_es_path. This way you
developers can stop worrying about localized routing helpers - but
they are available though.

> Does this also work with the url_for helper?

Yes it does, using a :lang parameter - maybe 'lang' is not the right
name but it's configurable.

> I've added this to the wiki (see http://rails-i18n.org/wiki)

Thanks a lot! I would have done it myself but I'm just learning about
how you're organized.

> Thanks!

My pleasure! You guys are doing a tremendous work and I'd like to
contribute somehow :)

Reply all
Reply to author
Forward
0 new messages