Skip to content
kwi edited this page Sep 14, 2010 · 21 revisions

I18n_routing

I18n_routing is a plugin for Ruby on Rails that lets you easily translate your routes trough the I18n api included in Rails since version 2.2

Why translating my routes ?

  • A must have if your Rails website is localized.
  • More friendly for your visitors.
  • Url are a key point for SEO, place keywords in them !

I18n_routing keypoints

  • No translations are made during runtime, all is precompiled when building routes at startup.
  • Works with Rails 2.x series (> 2.2) and with Rails 3
  • Builded on top of I18n api => translate your routes as never been as simple as now

Examples (Rails 3)

First, declare a localized resources in your routes.rb :

localized do
  resources :users
end

Then, translate your resources in your locales files :

fr:
  resources:
    users: 'utilisateurs'

After that, when you will use any users route helpers in your app, it will transparently use the correct route path depending on your current locale.

$ rails console
ruby-1.8.7-p249 >   I18n.locale = :en
 => :en 
ruby-1.8.7-p249 > app.users_path
 => "/users" 
ruby-1.8.7-p249 > I18n.locale = :fr
 => :fr 
ruby-1.8.7-p249 > app.users_path
 => "/utilisateurs" 
ruby-1.8.7-p249 > 

Rails 2 warning :

The Rails 2.x code is kind of dirty yet, it need clean up and intensive refactoring. If you can, use the Rails 3 code wich is really cleaner !

Clone this wiki locally