Permalink
Browse files

Updated regex to allow for more language codes including numeric regions

Even though it is not RFC 2616 compliant, some browsers (e.g. Google Chrome) 
send HTTP_ACCEPT_LANGUAGE headers that include numeric regions like
those in BCP 47.
  • Loading branch information...
1 parent 0b57ca1 commit 98036d967b98d0654671cce09b59e71e2a944af6 @postpostmodern committed Apr 21, 2011
Showing with 6 additions and 1 deletion.
  1. +1 −1 lib/http_accept_language.rb
  2. +5 −0 test/http_accept_language_test.rb
@@ -13,7 +13,7 @@ def user_preferred_languages
l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
l.split(';q=')
end.sort do |x,y|
- raise "Not correctly formatted" unless x.first =~ /^[a-z\-]+$/i
+ raise "Not correctly formatted" unless x.first =~ /^[a-z\*]{1,8}(-[a-z0-9\*]{1,8})*$/i
y.last.to_f <=> x.last.to_f
end.collect do |l|
l.first.downcase.gsub(/-[a-z]+$/i) { |x| x.upcase }
@@ -23,6 +23,11 @@ def test_should_ignore_jambled_header
request.env['HTTP_ACCEPT_LANGUAGE'] = 'odkhjf89fioma098jq .,.,'
assert_equal [], request.user_preferred_languages
end
+
+ def test_should_accept_numeric_regions
+ request.env['HTTP_ACCEPT_LANGUAGE'] = 'es-419,es;q=0.8,en-US;q=0.6,en;q=0.4'
+ assert_equal %w{es-419 es en-US en}, request.user_preferred_languages
+ end
def test_should_find_first_available_language
assert_equal 'en-GB', request.preferred_language_from(%w{en en-GB})

0 comments on commit 98036d9

Please sign in to comment.