Skip to content

Commit 599c02d

Browse files
committed
Add localizations to MaxMind
1 parent a1df8ca commit 599c02d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/GeoIP.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public function getService()
208208

209209
// Sanity check
210210
if ($class === null) {
211-
throw new \Exception('The GeoIP service is not valid.');
211+
throw new \Exception('No GeoIP service is configured.');
212212
}
213213

214214
// Create service instance

src/Services/MaxMindDatabase.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace InteractionDesignFoundation\GeoIP\Services;
66

77
use GeoIp2\Database\Reader;
8+
use GeoIp2\Model\City;
9+
use Illuminate\Support\Arr;
810

911
class MaxMindDatabase extends AbstractService
1012
{
@@ -59,6 +61,7 @@ public function locate($ip)
5961
'lon' => $record->location->longitude,
6062
'timezone' => $record->location->timeZone,
6163
'continent' => $record->continent->code,
64+
'localizations' => $this->getLocalizations($record),
6265
]);
6366
}
6467

@@ -195,4 +198,20 @@ protected function downloadFileByUrl(string $filename, string $url): void
195198
throw new \RuntimeException('Cannot download the file. Please enable allow_url_fopen or install curl extension.');
196199
}
197200
}
201+
/**
202+
* Get localized country name, state name and city name based on config languages
203+
* @return array<string, string>
204+
*/
205+
private function getLocalizations(City $record): array
206+
{
207+
$localizations = [];
208+
209+
foreach ($this->config('locales', ['en']) as $lang) {
210+
$localizations[$lang]['country'] = Arr::get($record->country->names, $lang);
211+
$localizations[$lang]['state_name'] = Arr::get($record->mostSpecificSubdivision->names, $lang);
212+
$localizations[$lang]['city'] = Arr::get($record->city->names, $lang);
213+
}
214+
215+
return $localizations;
216+
}
198217
}

0 commit comments

Comments
 (0)