mjackson / rack-accept

HTTP Accept* for Ruby/Rack
http://mjackson.github.com/rack-accept
47 stars 16 forks source link

Add support for first-level language matching #9

Closed remi closed 12 years ago

remi commented 12 years ago

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
remi commented 12 years ago

I implemented this feature because my current code to handle all these languages looks kind of messy :smile:

accept = env['rack-accept.request']
redirect_lang = accept.best_language(%w{fr fr-ca fr-CA fr-fr fr-FR en en-us en-US en-ca en-CA})
mttkay commented 12 years ago

+1 for having this merged. What's the hold-up/objection?

remi commented 12 years ago

Thanks for merging it!

mjackson commented 12 years ago

No problem! Your patch is included in the 0.4.5 release that I pushed this morning. Sorry it took so long. I promise it won't be so long in the future!