Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 2 contributors
Commits on Mar 16, 2012
@remiprev remiprev Add support for first-level language matching
This allows us to only declare first-level languages we want to match
against and still get matchs from languge pairs (such as `fr-ca` or
`en-US`). This is better summarized using an example:

    get "/" do
      accept = env['rack-accept.request'] # => "en;q=0.5, fr-ca"
      accept.best_language(%w{fr en}) # => "en"
    end

    get "/foo" do
      accept = env['rack-accept.request'] # => "en;q=0.5, fr-ca"
      accept.first_level_match = true
      accept.best_language(%w{fr en}) # => "fr"
    end
6cc33a0
Commits on Jun 15, 2012
@mjackson Merge remote-tracking branch 'remiprev/feature-first-level-language-m…
…atch'

Conflicts:
	test/language_test.rb
bb964f4
Showing with 8 additions and 0 deletions.
  1. +2 −0 lib/rack/accept/language.rb
  2. +6 −0 test/language_test.rb
View
2 lib/rack/accept/language.rb
@@ -6,6 +6,7 @@ module Rack::Accept
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
class Language
include Header
+ attr_writer :first_level_match
# The name of this header.
def name
@@ -24,6 +25,7 @@ def qvalue(language)
# +language+, ordered by precedence.
def matches(language)
values.select {|v|
+ v = v.match(/^(.+?)-/) ? $1 : v if @first_level_match
v == language || v == '*' || (language.match(/^(.+?)-/) && v == $1)
}.sort {|a, b|
# "*" gets least precedence, any others are compared based on length.
View
6 test/language_test.rb
@@ -37,5 +37,11 @@ def test_best_of
l = L.new('en-gb;q=0.5, EN, fr-CA')
assert_equal('en', l.best_of(%w< en en-gb >))
+
+ l = L.new('en;q=0.5, fr-ca')
+ assert_equal('en', l.best_of(%w< en fr >))
+
+ l.first_level_match = true
+ assert_equal('fr', l.best_of(%w< en fr >))
end
end

No commit comments for this range

Something went wrong with that request. Please try again.