Skip to content
No description or website provided.
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Rails 3.x routes translations based on Raul's translate_routes (

It's currently a stripped down version of the forked gem, adding some bugfixes for rails 3.1 and features I needed for my project. See doc below to see what it can do.

translate_routes & i18n_routes seems to be unmaintained projects so I decided to start a fork of the first one for my own projects, I can't promise high dedication but I'll to maintain it for my own use and take care all patches & bugs submitted, help is welcome!


Add it to your Gemfile:

gem 'rails-translate-routes'

Basic usage

Let's imagine you have a SampleApp with products and a contact page. A typical 'routes.rb' file should look like:

SampleApp::Application.routes.draw do
  resources :products
  match 'contact', :to => 'pages#contact'

Running 'rake routes' we have:

    products GET    /products(.:format)          {:action=>"index", :controller=>"products"}
             POST   /products(.:format)          {:action=>"create", :controller=>"products"}
 new_product GET    /products/new(.:format)      {:action=>"new", :controller=>"products"}
edit_product GET    /products/:id/edit(.:format) {:action=>"edit", :controller=>"products"}
     product GET    /products/:id(.:format)      {:action=>"show", :controller=>"products"}
             PUT    /products/:id(.:format)      {:action=>"update", :controller=>"products"}
             DELETE /products/:id(.:format)      {:action=>"destroy", :controller=>"products"}
     contact        /contact(.:format)           {:action=>"contact", :controller=>"pages"}

We want to have them in two languages english and spanish, to accomplish this with rails-translate-routes:

1) We have to activate the translations appending this line to the end of 'routes.rb'


2) Now we can write translations on a standard YAML file (e.g: in config/locales/routes.yml), including all the locales and their translations:

  # you can leave empty locales, for example the default one
  products: productos
  contact: contacto
  new: crear

3) Include this filter in your ApplicationController:

before_filter :set_locale_from_url

Also remember to include language detection to your app, a simple example of an ApplicationController

class ApplicationController < ActionController::Base

  before_filter :set_locale
  before_filter :set_locale_from_url


  def set_locale
    I18n.locale = params[:locale] || ((lang = request.env['HTTP_ACCEPT_LANGUAGE']) && lang[/^[a-z]{2}/])

And that's it! Now if we execute 'rake routes':

   products_en GET    /products(.:format)              {:action=>"index", :controller=>"products"}
   products_es GET    /es/productos(.:format)          {:action=>"index", :controller=>"products"}
               POST   /products(.:format)              {:action=>"create", :controller=>"products"}
               POST   /es/productos(.:format)          {:action=>"create", :controller=>"products"}
new_product_en GET    /products/new(.:format)          {:action=>"new", :controller=>"products"}
new_product_es GET    /es/productos/new(.:format)      {:action=>"new", :controller=>"products"}

edit_product_en GET /products/:id/edit(.:format) {:action=>“edit”, :controller=>“products”} edit_product_es GET /es/productos/:id/edit(.:format) {:action=>“edit”, :controller=>“products”}

product_en GET    /products/:id(.:format)          {:action=>"show", :controller=>"products"}
product_es GET    /es/productos/:id(.:format)      {:action=>"show", :controller=>"products"}
           PUT    /products/:id(.:format)          {:action=>"update", :controller=>"products"}
           PUT    /es/productos/:id(.:format)      {:action=>"update", :controller=>"products"}
           DELETE /products/:id(.:format)          {:action=>"destroy", :controller=>"products"}
           DELETE /es/productos/:id(.:format)      {:action=>"destroy", :controller=>"products"}
contact_en        /contact(.:format)               {:action=>"contact", :controller=>"pages"}
contact_es        /es/contacto(.:format)           {:action=>"contact", :controller=>"pages"}

The application recognizes the different routes and sets the 'I18n.locale' value in controllers, but what about the routes generation? As you can see on the previous rake routes execution, the 'contact_es_path' and 'contact_en_path' routing helpers have been generated and are available in your controllers and views. Additionally, a 'contact_path' helper has been generated, which generates the routes according to the current request's locale. This means that if you use named routes you don't need to modify your application links because the routing helpers are automatically adapted to the current locale.

URL structure options

As you can see by default it generates the following url structure:


In case you want the default languages to be scoped resulting in the following structure:


You can specify the following option:

ActionDispatch::Routing::Translator.translate_from_file('config/locales/routes.yml', { :prefix_on_default_locale => true })

If you use the 'prefix_on_default_locale' you will have to make the proper redirect on your root controller from to or rails-translate-routes adds an extra unstranslated root path:

root_en        /en                              {:controller=>"pages", :action=>"index"}
root_es        /es                              {:controller=>"pages", :action=>"index"}
   root        /                                {:controller=>"pages", :action=>"index"}

A simple example of a redirection to user locale in index method:

def index
  unless params[ :locale]
    # it takes I18n.locale from the previous example set_locale as before_filter in application controller
    redirect_to eval("root_#{I18n.locale}_path

  # rest of the controller logic ...


Help is welcome ;)

  • make basic testing

  • support for rails 3.2


Thanks to:

Similar projects

There are other two projects for translating routes in Rails (which I know of), both of them are unfortunately unmaintained but you may want to check them out if you use Rails 2 or have different needs.

Something went wrong with that request. Please try again.