Skip to content

Instantly share code, notes, and snippets.

@madrobby
Created October 20, 2012 12:20
Show Gist options
  • Save madrobby/3923148 to your computer and use it in GitHub Desktop.
Save madrobby/3923148 to your computer and use it in GitHub Desktop.
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