Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add experimental I18n Locale middleware

  • Loading branch information...
commit 01c36960e3201ba2a99296f6831d0c79636ac3ae 1 parent 5373195
@josh josh authored
Showing with 33 additions and 1 deletion.
  1. +2 −1  lib/rack/contrib.rb
  2. +31 −0 lib/rack/locale.rb
View
3  lib/rack/contrib.rb
@@ -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"
View
31 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
Please sign in to comment.
Something went wrong with that request. Please try again.