mjackson / rack-accept

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

best_language fails to match language code against locale codes #14

Closed mttkay closed 11 years ago

mttkay commented 11 years ago

Not sure if this behavior is defined in the RFC, but the current implementation of language matching seems broken to me:

env = {'HTTP_ACCEPT_LANGUAGE' => 'en'}
req = Rack::Accept::Request.new(env)
req.best_language(%w(fr_fr en_gb))
=> nil

why does en_GB not match en? A client requesting just a language code, not a specific locale, is obviously only interested in the language part, not the country, so this should match all locale codes starting with en_*?

mjackson commented 11 years ago

The problem is in your example. The RFC for language tags specifies that locales should be separated from languages by a hyphen, not an underscore. If you change your example you'll get the proper result.

req.best_language(%w(fr-fr en-gb))
=> "en-gb"
mttkay commented 11 years ago

Oh dear, thanks! You're right of course. Fixed this in our backend. Sorry for the noise.

mjackson commented 11 years ago

No problem. Thank you for reporting what you thought was an issue. More eyes makes for better code!