Tounge

Tounge allows for easy locale switching in Ruby on Rails through url parameters, session, user settings or http accept language string.

Installation

Add this to your Gemfile and run the bundle command.

gem "tounge"

Getting started

Make sure you have atleast two available locales for your application (otherwise, whats the point in switching?).

Then include the Tounge::LocaleControl module in your application controller and your ready to go.

class ApplicationController < ActionController::Base
	include Tounge::LocaleControl
end

Now fire up your application and add locale=xx as a url parameter to any page, where xx is any of your available locales.

Lookup chain

Tounge uses a default lookup chain that looks like this, highest priority first: [:params, :user, :session, :http, :default]

:params, Url parameters

Add locale=xx (where xx is any supported locale in your application) to a url and that will be used to set the locale. This will also get passed on to any url helpers so it doesn’t get lost between requests.

:user, User settings

Tounge expects a current_user method on the controller and will call locale on that to set the locale.

:session, Session/Cookie setting

Uses session[:locale] to set the locale.

:http, HTTP Accept Language

Parses ENV['HTTP_ACCEPT_LANGUAGE] and sets the locale to the first match against the available locales.

:default, Default locale

If everything else fails, use the default locale.

Configuration

You can change the lookup chain by creating an initializer

#config/initializers/tounge_setup.rb
Tounge::LocaleSetter.configure |config|
  config.lookup_chain = [:user, :http, :session, :default]
end

This would give user settings the highest priority and disallow setting locale through url parameters.

Contribute

  1. Fork the repository

  2. Make your changes in a topic branch

  3. Hit me with a pull request.

Please add tests to any contributions (even though the current tests pretty much suck), and don’t update the version.

Thanks

Inspiration (and initial code) was taken from a great talk by Jeff Casimir at Steel City Ruby Conf 2012.

License

Copyright © 2012 Jonas Lundqvist

This project rocks and uses MIT-LICENSE.