Skip to content

Instantly share code, notes, and snippets.

@jsqu99
Created June 2, 2015 20:29
Show Gist options
  • Save jsqu99/4d6b1dd6fade822afee5 to your computer and use it in GitHub Desktop.
Save jsqu99/4d6b1dd6fade822afee5 to your computer and use it in GitHub Desktop.

Revisions

  1. jsqu99 created this gist Jun 2, 2015.
    36 changes: 36 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    def get_country
    if cookies[:country].present?
    set_locale(cookies[:country])
    return cookies[:country]
    else
    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

    code = country.try(:country_code2) || 'US'
    set_locale(code)
    code
    end

    end

    def set_locale(country_code)
    if GlobalConstants::LOCALES.include?(country_code)
    locale = GlobalConstants::LOCALES[country_code]

    cookies[:locale] = {
    value: locale,
    expires: 1.year.from_now,
    path: '/'
    }
    end

    I18n.locale = locale || I18n.default_locale
    end