Skip to content
Snippets Groups Projects
Commit 2ba20514 authored by Michal Musialik's avatar Michal Musialik Committed by Thu Trang Pham
Browse files

Allow geoip2 result language to be changed for each result (#1157)

Maxmind geoip2 returns a collection of names in all available languages
for each field.
This commit allows us to change the language of a single result without
having to reconfigure Geocoder.
parent f905e455
No related branches found
No related tags found
No related merge requests found
...@@ -15,11 +15,15 @@ module Geocoder ...@@ -15,11 +15,15 @@ module Geocoder
end end
def city def city
data.fetch('city', {}).fetch('names', {}).fetch(locale, '') fetch_name(
data.fetch('city', {}).fetch('names', {})
)
end end
def state def state
data.fetch('subdivisions', []).fetch(0, {}).fetch('names', {}).fetch(locale, '') fetch_name(
data.fetch('subdivisions', []).fetch(0, {}).fetch('names', {})
)
end end
def state_code def state_code
...@@ -27,7 +31,9 @@ module Geocoder ...@@ -27,7 +31,9 @@ module Geocoder
end end
def country def country
data.fetch('country', {}).fetch('names', {}).fetch(locale, '') fetch_name(
data.fetch('country', {}).fetch('names', {})
)
end end
def country_code def country_code
...@@ -48,14 +54,26 @@ module Geocoder ...@@ -48,14 +54,26 @@ module Geocoder
end end
end end
def language=(l)
@language = l.to_s
end
def language
@language ||= default_language
end
private private
def data def data
@data.to_hash @data.to_hash
end end
def locale def default_language
@locale ||= Geocoder.config[:language].to_s @default_language = Geocoder.config[:language].to_s
end
def fetch_name(names)
names[language] || names[default_language] || ''
end end
end end
end end
......
...@@ -35,4 +35,22 @@ class Geoip2Test < GeocoderTestCase ...@@ -35,4 +35,22 @@ class Geoip2Test < GeocoderTestCase
Geocoder::Configuration.language = :ru Geocoder::Configuration.language = :ru
assert_equal 'Маунтин-Вью', result.city assert_equal 'Маунтин-Вью', result.city
end end
def test_dynamic_localization
result = Geocoder.search('8.8.8.8').first
result.language = :ru
assert_equal 'Маунтин-Вью', result.city
end
def test_dynamic_localization_fallback
result = Geocoder.search('8.8.8.8').first
result.language = :unsupported_language
assert_equal 'Mountain View', result.city
assert_equal 'California', result.state
assert_equal 'United States', result.country
end
end end
...@@ -29,4 +29,24 @@ class MaxmindGeoip2Test < GeocoderTestCase ...@@ -29,4 +29,24 @@ class MaxmindGeoip2Test < GeocoderTestCase
results = Geocoder.search("no results") results = Geocoder.search("no results")
assert_equal 0, results.length assert_equal 0, results.length
end end
def test_dynamic_localization
result = Geocoder.search('1.2.3.4').first
result.language = :ru
assert_equal 'Лос-Анджелес', result.city
assert_equal 'Калифорния', result.state
assert_equal 'США', result.country
end
def test_dynamic_localization_fallback
result = Geocoder.search('1.2.3.4').first
result.language = :unsupported_language
assert_equal 'Los Angeles', result.city
assert_equal 'California', result.state
assert_equal 'United States', result.country
end
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