A Rack module for setting a user's locale via a cookie, browser default language or I18n default locale.
Switch branches/tags
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Gem Version Build Status

A Rack module for getting and setting a user's locale via a cookie or browser default language.

Credit to these gems for pointing me in the direction I wanted to go in...


gem install rack-user-locale

In a Gemfile:

gem 'rack-user-locale'


Add the following line to your Rack application

use Rack::UserLocale

and thats it!!

Your application will now attempt to set the I18n.locale value based on the following:

  • Whether a user has a "user-locale" cookie set
  • The "HTTP_ACCEPT_LANGUAGE" value. If there are multiple values it will attempt to set the one ranked highest
  • The I18n.default_locale value (basically a fallback)

There is the option to pass in an accepted array of locales, like so:

use Rack::UserLocale, :accepted_locales => [:de, :en, :es, :fr, :ja, :pt, :zh, (whatever codes you support)]

If this option is supplied the the users locale will either be set to one of the accepted locales in the array, otherwise it will be set to the I18n.default_locale value.

Should you wish to overwrite a users locale value at any point in your application, like for changing language prefs, then simple rewrite the "user-locale" cookie with a new value.


The user locale detection is done on every Rack::Request, which would include all of the non Ruby/Rack actions like .css or .js files.

If you don't want this happen then you can use Rack::Static in your application and filter out which files Rack should serve up statically.

Something like this:

use Rack::Static, :urls => ["/css", "/js"], :root => "public"

would serve anything in your public folder statically and wouldn't create a Rack::Request.

See the Rack::Static docs for more info.

Contributing to rack-user-locale

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
  • Fork the project.
  • Start a feature/bugfix branch.
  • Commit and push until you are happy with your contribution.
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.


Copyright (c) 2013 Stuart Chinery and Dave Hrycyszyn - headlondon.com.

See LICENSE.txt for further details.