轨。 Retriving country用户正在访问

时间:2015-09-27 12:37:45

标签: ruby-on-rails ruby-on-rails-4 geocoding rails-geocoder

我想知道是否有人知道该用户正在访问的国家?到目前为止,我一直在使用'geocoder' - 结果太慢了。发送和检索的请求会增加服务器响应时间(600-800ms之间)的大量时间。或许我想念某种配置?

如果有人有更好的方法,请告诉我!

谢谢!

application.rb中

def extract_locale_from_accept_language_header

  if cookies[:windhager_locale] && I18n.available_locales.include?(cookies[:windhager_locale].to_sym)
    l = cookies[:windhager_locale].to_sym
  else
    begin
      country_code = country_code = request.location.country_code
      if country_code
        country_code = country_code.downcase.to_sym
        case country_code
          when :en
            l = :int_en
          when :us
            l = :int_en
          when :ca
            # CHECK BROWSER LANGUAGE
            l = check_browser_language_ca
          when :nz
            l = :int_en
          when :au
            l = :int_en
          when :de
            l = :de
          when :fr
            l = :fr
          when :ch
            # CHECK BROWSER LANGUAGE
            l = check_browser_language_ch
          when :at
            l = :at
          when :li
            l = :ch_de
          when :rd
            l = :at
          else
            l = check_browser_language
        end
      else
        l = check_browser_language
      end
    rescue
      l = I18n.default_locale
    ensure
      cookies.permanent[:windhager_locale] = l
    end
  end

  return l
end

1 个答案:

答案 0 :(得分:0)

假设您现在正在进行远程API调用,您可以从MaxMind下载IP数据库并执行本地查找而不是API调用。这种方法对我的用例非常有用。 Checkout the documentation from the geocoder github page了解更多详情,但这是一个示例:

Geocoder.configure(ip_lookup: :maxmind_local, maxmind_local: {file: File.join('folder', 'GeoLiteCity.dat')})
相关问题