Sha256: ee25bc6e2680ab00120226b91fa99445f11b1387f1c039dba481ff38c7e55931
Contents?: true
Size: 1.41 KB
Versions: 1
Compression:
Stored size: 1.41 KB
Contents
require 'geoip' module Rack class GeoLocale def initialize(app) @app = app end def call(env) env["locale.language"], env["locale.country"] = parse_locale(env) if country = parse_country(env) env["locale.country"] = country end @app.call(env) end private def parse_country(env) remote_addr = env["REMOTE_ADDR"] return nil unless remote_addr result = geoip.country(remote_addr).country_code2 if result != "--" result else nil end end def parse_locale(env) env["HTTP_ACCEPT_LANGUAGE"] ||= "" language_ranges = env["HTTP_ACCEPT_LANGUAGE"].split(",") language_ranges.map do |language_range| language_range += ';q=1.0' unless language_range =~ /;q=\d+\.\d+$/ locale, q = language_range.split(";q=") language, country = locale.strip.split("-") {:language => language, :country => country, :q => q} end.sort {|x, y| y[:q] <=> x[:q]}.map{|o| [o[:language], o[:country]]}.first end def database? if ENV["GEOIP_DATABASE"] ::File.exist? ENV["GEOIP_DATABASE"] else false end end def database ENV["GEOIP_DATABASE"] end def geoip if database? GeoIP.new(database) else nil end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rack-geo-locale-0.0.2 | lib/rack/geo_locale.rb |