Created
October 20, 2012 12:20
-
-
Save madrobby/3923148 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def set_locale | |
locale = params[:locale].to_s | |
if !locale.blank? | |
cookies[:preferred_lang] = { :value => locale, :expires => 10.years.from_now } | |
session[:update_lang] = locale # remember preferred setting for this session | |
else | |
locale = cookies[:preferred_lang] | |
end | |
preferred = (locale.blank? ? request.user_preferred_languages : [locale]).map{|l| l.downcase.strip } | |
best_match = preferred.find do |lang| | |
AVAILABLE_LOCALES.include? lang # de-AT etc. | |
end or preferred.map{|l| l[0..1] }.uniq.find do |lang| | |
AVAILABLE_LOCALES.include? lang # en, de, etc. | |
end or "en" # fall back to en if unknown | |
I18n.locale = best_match | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment