Permalink
Browse files

Make compatible_language_from follow the RFC 2616 selection algorithm

The change in commit 92b276c is wrong
because it reverses the previous change to obey the RFC 2616 algorithm.

Specifically RFC 2616 says that a request for en-GB will only match
with en-GB or something that starts with en-GB and it will not match
against en with no suffix.

The test failures were because the test was wrong, not because the
code was wrong. This commit restores the previous code and fixes the
tests.
  • Loading branch information...
1 parent e298593 commit d7843567b725d9fd64d9497129180618279552cc @tomhughes committed Mar 17, 2011
Showing with 6 additions and 7 deletions.
  1. +3 −4 lib/http_accept_language/parser.rb
  2. +3 −3 spec/parser_spec.rb
@@ -55,10 +55,9 @@ def preferred_language_from(array)
# request.compatible_language_from I18n.available_locales
#
def compatible_language_from(available_languages)
- user_preferred_languages.map do |x| #en-US
- available_languages.find do |y| # en
- y = y.to_s
- x == y || x.split('-', 2).first == y.split('-', 2).first
+ user_preferred_languages.map do |preferred|
+ available_languages.find do |available|
+ available.to_s =~ /^#{Regexp.escape(preferred.to_s)}(-|$)/
end
end.compact.first
end
View
@@ -35,12 +35,12 @@ def parser
end
it "should find first compatible from user preferred" do
- parser.header = 'en-us,de-de'
- parser.compatible_language_from(%w{de en}).should eq 'en'
+ parser.header = 'en-US,en,de'
+ parser.compatible_language_from(%w{de-DE en-GB}).should eq 'en-GB'
end
it "should accept symbols as available languages" do
- parser.header = 'en-us'
+ parser.header = 'en'
parser.compatible_language_from([:"en-HK"]).should eq :"en-HK"
end

0 comments on commit d784356

Please sign in to comment.