Dirk Bergmann

Developer, panographer. Does consultancy work from own company. Lives in Chiang Mai, Thailand.

Twitter

Chiang Mai

Hua Hin

Thailand Hotels

ListVote - List. Vote. Learn.

Partnerseek - Find Partners

Rails i18n with Routed Locales

Localizing and globalizing Rails apps has gotten much easier with version 2.2. Add model translations with Globalize2 and you’re on your way. But where to store the locale? The session is a big no no, so it would be more elegant to just prepend them to the path and adjust the routes accordingly:

/widgets/

/en/widgets/

/de/widgets/

Rails routing is arcane and difficult, so hacking away at it is like stumbling into  a mine field. Especially difficult is the preservation of url_helpers, so Sven Fuchs decided to wrap the whole blob with a routing filter whose sample implementation is in: 

#{RAILS_ROOT}/vendor/routing-filter/lib/routing-filter/locale.rb

and now works out of the box, so there’s no need for you to hack your own filter module as it might appear reading the docs.

An alternative approach is translate_routes by Raul Murciano which might be more feature-rich. Also check out Karel Minarik’s sample app.