Skip to content

Commit

Permalink
Add localisation to MaxMindWebService
Browse files Browse the repository at this point in the history
  • Loading branch information
alies-dev committed Aug 7, 2024
1 parent eff2eb7 commit 2167d33
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Services/MaxMindDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
* @return array<string, string|null>
*/
private function getLocalizations(City $record): array
{
Expand Down
19 changes: 19 additions & 0 deletions src/Services/MaxMindWebService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace InteractionDesignFoundation\GeoIP\Services;

use GeoIp2\Model\City;
use GeoIp2\WebService\Client;
use Illuminate\Support\Arr;

class MaxMindWebService extends AbstractService
{
Expand Down Expand Up @@ -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<string, string|null>
*/
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;
}
}

0 comments on commit 2167d33

Please sign in to comment.