Rack::LocaleRootRedirect

Build Status Coverage Status Ruby Gem

Rack::LocaleRootRedirect redirects requests to "/" based on the Accept-Language HTTP header.

Installation

Add this line to your application’s Gemfile:

gem 'rack-locale-root-redirect'

And then execute:

$ bundle

Usage

With Sinatra:

# Gemfile
gem 'sinatra'
gem 'rack-locale-root-redirect'

# config.ru
require 'bundler'
Bundler.require

class MyApp < Sinatra::Base
  use Rack::Accept
  use Rack::LocaleRootRedirect, fr: "/fr", en: "/en"

  get('/fr') { 'Français!' }
  get('/en') { 'English!' }
end

run MyApp

Then, test it:

$ rackup &

$ curl -sI "http://0.0.0.0:9292" -H "Accept-Language: fr;q=1, en;q=0.8" | grep "302\|Location"
HTTP/1.1 302 Moved Permanently
Location: /fr

$ curl -sI "http://0.0.0.0:9292" -H "Accept-Language: fr;q=0.4, en;q=0.8" | grep "302\|Location"
HTTP/1.1 302 Moved Permanently
Location: /en

License

Rack::LocaleRootRedirect is © 2013 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.

About Mirego

Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We proudly build mobile applications for iPhone, iPad, Android, Blackberry, Windows Phone and Windows 8 in beautiful Quebec City.

We also love open-source software and we try to extract as much code as possible from our projects to give back to the community.