Skip to content

Instantly share code, notes, and snippets.

@chrisb
Created October 19, 2011 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisb/1299837 to your computer and use it in GitHub Desktop.
Save chrisb/1299837 to your computer and use it in GitHub Desktop.
def current_language
# if a "lang" URL parameter is specified, force it
return params[:lang] if params[:lang].present?
# if the user is logged in, use their language preference stored in the DB
return current_user.language if logged_in?
# if the user has manually selected a language, return that
return cookies[:language_code] if cookies[:language_code]
# if the user is not logged in or has not selected a language
# attempt to determine a language based on their IP
LANGUAGE_TO_COUNTRY_MAPPINGS.keys.each { |lang| return lang if LANGUAGE_TO_COUNTRY_MAPPINGS[lang].include?(current_country) }
# if all else fails, return english
return 'eng'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment