Skip to content

Commit 2cdf606

Browse files
committed
Add localizations to MaxMind
1 parent 7b2a0b9 commit 2cdf606

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/Services/MaxMindDatabase.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use PharData;
66
use Exception;
7+
use GeoIp2\Model\City;
78
use GeoIp2\Database\Reader;
9+
use Illuminate\Support\Arr;
810

911
class MaxMindDatabase extends AbstractService
1012
{
@@ -55,6 +57,7 @@ public function locate($ip)
5557
'lon' => $record->location->longitude,
5658
'timezone' => $record->location->timeZone,
5759
'continent' => $record->continent->code,
60+
'localizations' => $this->getLocalizations($record),
5861
]);
5962
}
6063

@@ -166,4 +169,23 @@ protected function deleteDirectory(string $directory)
166169

167170
return rmdir($directory);
168171
}
172+
173+
/**
174+
* Get localized country name, state name and city name based on config languages
175+
*
176+
* @param City $record
177+
* @return array
178+
*/
179+
private function getLocalizations(City $record): array
180+
{
181+
$localizations = [];
182+
183+
foreach ($this->config('locales', ['en']) as $lang) {
184+
$localizations[$lang]['country'] = Arr::get($record->country->names, $lang);
185+
$localizations[$lang]['state_name'] = Arr::get($record->mostSpecificSubdivision->names, $lang);
186+
$localizations[$lang]['city'] = Arr::get($record->city->names, $lang);
187+
}
188+
189+
return $localizations;
190+
}
169191
}

src/Services/MaxMindWebService.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Torann\GeoIP\Services;
44

5+
use GeoIp2\Model\City;
6+
use Illuminate\Support\Arr;
57
use GeoIp2\WebService\Client;
68

79
class MaxMindWebService extends AbstractService
@@ -46,6 +48,27 @@ public function locate($ip)
4648
'lon' => $record->location->longitude,
4749
'timezone' => $record->location->timeZone,
4850
'continent' => $record->continent->code,
51+
'localizations' => $this->getLocalizations($record),
4952
]);
5053
}
51-
}
54+
55+
/**
56+
* Get localized country name, state name and city name based on config languages
57+
*
58+
* @param City $record
59+
*
60+
* @return array
61+
*/
62+
private function getLocalizations(City $record): array
63+
{
64+
$localizations = [];
65+
66+
foreach ($this->config('locales', ['en']) as $lang) {
67+
$localizations[$lang]['country'] = Arr::get($record->country->names, $lang);
68+
$localizations[$lang]['state_name'] = Arr::get($record->mostSpecificSubdivision->names, $lang);
69+
$localizations[$lang]['city'] = Arr::get($record->city->names, $lang);
70+
}
71+
72+
return $localizations;
73+
}
74+
}

0 commit comments

Comments
 (0)