diff --git a/src/Services/MaxMindDatabase.php b/src/Services/MaxMindDatabase.php index 8d912f2..5ba6899 100644 --- a/src/Services/MaxMindDatabase.php +++ b/src/Services/MaxMindDatabase.php @@ -198,9 +198,10 @@ protected function downloadFileByUrl(string $filename, string $url): void throw new \RuntimeException('Cannot download the file. Please enable allow_url_fopen or install curl extension.'); } } + /** * Get localized country name, state name and city name based on config languages - * @return array + * @return array */ private function getLocalizations(City $record): array { diff --git a/src/Services/MaxMindWebService.php b/src/Services/MaxMindWebService.php index 5ed3a22..ee10410 100644 --- a/src/Services/MaxMindWebService.php +++ b/src/Services/MaxMindWebService.php @@ -4,7 +4,9 @@ namespace InteractionDesignFoundation\GeoIP\Services; +use GeoIp2\Model\City; use GeoIp2\WebService\Client; +use Illuminate\Support\Arr; class MaxMindWebService extends AbstractService { @@ -46,6 +48,23 @@ public function locate($ip) 'lon' => $record->location->longitude, 'timezone' => $record->location->timeZone, 'continent' => $record->continent->code, + 'localizations' => $this->getLocalizations($record), ]); } + /** + * Get localized country name, state name and city name based on config languages + * @return array + */ + private function getLocalizations(City $record): array + { + $localizations = []; + + foreach ($this->config('locales', ['en']) as $lang) { + $localizations[$lang]['country'] = Arr::get($record->country->names, $lang); + $localizations[$lang]['state_name'] = Arr::get($record->mostSpecificSubdivision->names, $lang); + $localizations[$lang]['city'] = Arr::get($record->city->names, $lang); + } + + return $localizations; + } }