Skip to content

Commit

Permalink
Add experimental I18n Locale middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Dec 7, 2008
1 parent 5373195 commit 01c3696
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/rack/contrib.rb
Expand Up @@ -5,11 +5,12 @@ def self.release
"0.4"
end
end

autoload :ContentLength, "rack/content_length"
autoload :ETag, "rack/etag"
autoload :JSONP, "rack/jsonp"
autoload :LighttpdScriptNameFix, "rack/lighttpd_script_name_fix"
autoload :Locale, "rack/locale"
autoload :MailExceptions, "rack/mailexceptions"
autoload :PostBodyContentTypeParser, "rack/post_body_content_type_parser"
autoload :Sendfile, "rack/sendfile"
Expand Down
31 changes: 31 additions & 0 deletions lib/rack/locale.rb
@@ -0,0 +1,31 @@
require 'i18n'

module Rack
class Locale
def initialize(app)
@app = app
end

def call(env)
old_locale = I18n.locale
locale = nil

# http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
if lang = env["HTTP_ACCEPT_LANGUAGE"]
lang = lang.split(",").map { |l|
l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
l.split(';q=')
}.first
locale = lang.first.split("-").first
else
locale = I18n.default_locale
end

locale = env['rack.locale'] = I18n.locale = locale.to_s
status, headers, body = @app.call(env)
headers['Content-Language'] = locale
I18n.locale = old_locale
[status, headers, body]
end
end
end

0 comments on commit 01c3696

Please sign in to comment.