Skip to content

Commit 2167d33

Browse files
committed
Add localisation to MaxMindWebService
1 parent eff2eb7 commit 2167d33

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Services/MaxMindDatabase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,10 @@ protected function downloadFileByUrl(string $filename, string $url): void
198198
throw new \RuntimeException('Cannot download the file. Please enable allow_url_fopen or install curl extension.');
199199
}
200200
}
201+
201202
/**
202203
* Get localized country name, state name and city name based on config languages
203-
* @return array<string, string>
204+
* @return array<string, string|null>
204205
*/
205206
private function getLocalizations(City $record): array
206207
{

src/Services/MaxMindWebService.php

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

55
namespace InteractionDesignFoundation\GeoIP\Services;
66

7+
use GeoIp2\Model\City;
78
use GeoIp2\WebService\Client;
9+
use Illuminate\Support\Arr;
810

911
class MaxMindWebService extends AbstractService
1012
{
@@ -46,6 +48,23 @@ 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
}
54+
/**
55+
* Get localized country name, state name and city name based on config languages
56+
* @return array<string, string|null>
57+
*/
58+
private function getLocalizations(City $record): array
59+
{
60+
$localizations = [];
61+
62+
foreach ($this->config('locales', ['en']) as $lang) {
63+
$localizations[$lang]['country'] = Arr::get($record->country->names, $lang);
64+
$localizations[$lang]['state_name'] = Arr::get($record->mostSpecificSubdivision->names, $lang);
65+
$localizations[$lang]['city'] = Arr::get($record->city->names, $lang);
66+
}
67+
68+
return $localizations;
69+
}
5170
}

0 commit comments

Comments
 (0)