Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[patch] Rack::Locale : fall into decault_locale if browser's primary locale is not available #14

Open
hs opened this Issue · 0 comments

2 participants

@hs
hs commented

Hi,
Thanks a lot for publication of useful tools.

Present Rack::Locale seems to check only primary locale of a browser. I write a patch as follows:
1. Check a browser's accept languages(locale) in order of priority.
2. Set the locale of an application to first available one.
3. If no locale are supported, set it to default_locale.

patch:

--- locale.rb.old       2010-09-26 18:54:43.000000000 +0900
+++ locale.rb   2010-09-26 19:40:04.000000000 +0900
@@ -12,14 +12,14 @@

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

       locale = env['rack.locale'] = I18n.locale = locale.to_s
       status, headers, body = @app.call(env)
@mpalmer mpalmer added the needs-tests label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.