Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails optional /:locale route

I'm trying to setup a routing system for my rails app that allows for an optional route (/:locale) to be allowed to the base of the website.

So more or less:

/en/home/ would goto the same page as /home/ /en/people/ -> /people/

The only issue I'm having is setting this up in the routes config.

like image 637
matsko Avatar asked Jul 09 '11 16:07

matsko


2 Answers

Use scope '(:locale)' do...end. You can see an example from Agile Web Development with Rails here:

http://intertwingly.net/projects/AWDwR4/checkdepot-30/section-15.1.html

like image 76
Sam Ruby Avatar answered Oct 26 '22 15:10

Sam Ruby


What I usually do is, in config/routes.rb:

MyApp::Application.routes.draw do

  scope "(:locale)", :locale => /en|fr/ do
    #here only two languages are accepted: english and french

  end
end

And in my ApplicationController:

before_filter :set_locale

def set_locale
  I18n.locale = params[:locale] || "en"
end
like image 34
apneadiving Avatar answered Oct 26 '22 16:10

apneadiving