Skip to content
Snippets Groups Projects
Commit 829c516d authored by Alex Reisner's avatar Alex Reisner
Browse files

Merge pull request #909 from davydovanton/geoip2-locale-support

Add locale support to GeoIP2 result display class
parents 3a3cc236 26ba1809
No related branches found
No related tags found
No related merge requests found
......@@ -125,6 +125,5 @@ module Geocoder
end
EOS
end.join("\n\n"))
end
end
......@@ -13,35 +13,35 @@ module Geocoder
end
def latitude
data.fetch('location',{}).fetch('latitude',0.0)
data.fetch('location', {}).fetch('latitude', 0.0)
end
def longitude
data.fetch('location',{}).fetch('longitude',0.0)
data.fetch('location', {}).fetch('longitude', 0.0)
end
def city
data.fetch('city', {}).fetch('names', {}).fetch('en', '')
data.fetch('city', {}).fetch('names', {}).fetch(locale, '')
end
def state
data.fetch('subdivisions',[]).fetch(0,{}).fetch('names',{}).fetch('en','')
data.fetch('subdivisions', []).fetch(0, {}).fetch('names', {}).fetch(locale, '')
end
def state_code
data.fetch('subdivisions',[]).fetch(0,{}).fetch('iso_code','')
data.fetch('subdivisions', []).fetch(0, {}).fetch('iso_code', '')
end
def country
data.fetch('country', {}).fetch('names',{}).fetch('en','')
data.fetch('country', {}).fetch('names', {}).fetch(locale, '')
end
def country_code
data.fetch('country',{}).fetch('iso_code','')
data.fetch('country', {}).fetch('iso_code', '')
end
def postal_code
data.fetch('postal',{}).fetch('code','')
data.fetch('postal', {}).fetch('code', '')
end
def self.response_attributes
......@@ -59,6 +59,10 @@ module Geocoder
def data
@data.to_hash
end
def locale
@locale ||= Geocoder.config[:language].to_s
end
end
end
end
# encoding: utf-8
require 'rubygems'
require 'test/unit'
......@@ -189,7 +190,7 @@ module Geocoder
def results(query)
return [] if query.to_s == 'no results'
return [] if query.to_s == '127.0.0.1'
[{'city'=>{'names'=>{'en'=>'Mountain View'}},'country'=>{'iso_code'=>'US','names'=>
[{'city'=>{'names'=>{'en'=>'Mountain View', 'ru'=>'Маунтин-Вью'}},'country'=>{'iso_code'=>'US','names'=>
{'en'=>'United States'}},'location'=>{'latitude'=>37.41919999999999,
'longitude'=>-122.0574},'postal'=>{'code'=>'94043'},'subdivisions'=>[{
'iso_code'=>'CA','names'=>{'en'=>'California'}}]}]
......
......@@ -6,6 +6,10 @@ class Geoip2Test < GeocoderTestCase
Geocoder.configure(ip_lookup: :geoip2, file: 'test_file')
end
def teardown
Geocoder::Configuration.language = :en
end
def test_result_attributes
result = Geocoder.search('8.8.8.8').first
assert_equal 'Mountain View, CA 94043, United States', result.address
......@@ -24,4 +28,11 @@ class Geoip2Test < GeocoderTestCase
results = Geocoder.search('127.0.0.1')
assert_equal [], results
end
def test_localization
result = Geocoder.search('8.8.8.8').first
Geocoder::Configuration.language = :ru
assert_equal 'Маунтин-Вью', result.city
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment