Skip to content

A better i18n middleware for Rack. Uses the same i18n backend as Rails.

License

Notifications You must be signed in to change notification settings

rubysouth/rack_locale

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rack Locale

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.

Example Sinatra app:

  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.

About

A better i18n middleware for Rack. Uses the same i18n backend as Rails.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages