Skip to content

Commit

Permalink
Make compatible_language_from follow the RFC 2616 selection algorithm
Browse files Browse the repository at this point in the history
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
tomhughes committed Oct 4, 2013
1 parent e298593 commit d784356
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
7 changes: 3 additions & 4 deletions lib/http_accept_language/parser.rb
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/parser_spec.rb
Expand Up @@ -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

Expand Down

0 comments on commit d784356

Please sign in to comment.