Skip to content

Instantly share code, notes, and snippets.

@oliyoung
Created February 17, 2009 11:52
Show Gist options
  • Save oliyoung/65707 to your computer and use it in GitHub Desktop.
Save oliyoung/65707 to your computer and use it in GitHub Desktop.
grabbing the locale from the user's browser
before_filter :set_localization
def set_localization
# Set the language
I18n.locale = if session[:locale]
session[:locale]
elsif logged_in? && current_member.locale
current_member.locale.code
elsif !accepted_languages.empty?
accepted_languages.first.first
end
end
def accepted_languages
return [] if request.env["HTTP_ACCEPT_LANGUAGE"].nil?
accepted = request.env["HTTP_ACCEPT_LANGUAGE"].split(",")
accepted.map! { |locale| locale.strip.split(";") }
accepted.map! do |locale|
if (locale.size == 2)
[ locale[0], locale[1].sub(/^q=/, "").to_f ]
else
[ locale[0], 1.0 ]
end
end
accepted.sort { |a, b| b[1] <=> a[1] }
accepted = accepted.map do |locale|
l = locale[0].split("-")
locale[0] = l.size > 1 ? "#{l[0]}-#{l[1].upcase}" : l[0]
locale
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment