Skip to content

Instantly share code, notes, and snippets.

@jsqu99
Last active August 29, 2015 14:22
Show Gist options
  • Save jsqu99/49edd84518462c3bab40 to your computer and use it in GitHub Desktop.
Save jsqu99/49edd84518462c3bab40 to your computer and use it in GitHub Desktop.
def get_country
code = cookies[:country]
unless code.present?
set_country_cookie
code = cookies[:country] || 'US'
end
set_locale(code)
code
end
def set_country_cookie
country = GEOIP_DB.country(request.remote_ip)
if country
# the default value should never be used.
cookies[:country] = {
value: country.country_code2,
expires: 1.year.from_now,
path: '/'
}
end
end
def set_locale(country_code)
locale = if GlobalConstants::LOCALES.include?(country_code)
GlobalConstants::LOCALES[country_code]
end
I18n.locale = locale || I18n.default_locale
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment