Skip to content

Restore RFC 2616 compliance #6

Closed
wants to merge 3 commits into from
View
10 lib/http_accept_language/parser.rb
@@ -55,12 +55,10 @@ 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
- end
- end.compact.first
+ user_preferred_languages.find do |preferred|
+ available_languages.find { |available| available.to_s == preferred.to_s } ||
+ available_languages.find { |available| available.to_s =~ /^#{Regexp.escape(preferred.to_s)}-/ }
+ end
end
# Returns a supplied list of available locals without any extra application info
View
6 spec/parser_spec.rb
@@ -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
Something went wrong with that request. Please try again.