A slightly better locale middleware for Rack.
What’s different from the locale middleware in rack/contrib?
-
Adds a locale method to Rack::Request.
-
You can set a “locale” GET variable to change the locale.
-
Each request sets a cookie with the current locale to preserve it across requests.
-
Resolves languages specified in HTTP_ACCEPT_LANGUAGE with your available locales.
require 'rubygems' require 'sinatra' require 'rack_locale' use RubySouth::Rack::Locale I18n.backend = I18n::Backend::Simple.new I18n.backend.load_translations("en.yml", "es.yml") before do I18n.locale = request.locale end helpers do def t(*args) I18n.translate(*args) end end get "/" do haml :index end # index.haml %p = t(:hello_world) %p %a{:href => '?locale=en'} English %a{:href => '?locale=es'} Spanish # es.yml es: hello_world: ¡Hola mundo! # en.yml en: hello_world: Hello world!
Installation
Download and build it, or install the gem from Github. Note that it currently depends on Sven Fuchs’s fork of i18n.