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]
end
langs.sort_by { |lang, q| q }.map { |lang, q| lang }.reverse
end

deoxxa commented May 9, 2013

U2FsdGVkX1/e5p64uZ0k/b+/tWcqFy3wH8FPMpgVJW4JMxBnx4TCrA==

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