Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
because plugins and gems are dependencies and dependencies are bad
# returns an descending ranked array of language preferences in IS0-639-1
# parse('en-US,en-GB;q=0.9,es-MX;q=0.8,de-DE;q=0.7')
# => [:en, :en, :es, :de]
def accepted_locales http_accept_language
return [] if http_accept_language.blank?
langs = http_accept_language.scan(/([a-zA-Z]{2,4})(?:-[a-zA-Z]{2})?(?:;q=(1|0?\.[0-9]{1,3}))?/).map do |pair|
lang, q = pair
[lang.to_sym, (q || '1').to_f]
langs.sort_by { |lang, q| q }.map { |lang, q| lang }.reverse

deoxxa commented May 9, 2013


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment