Skip to content

Commit 8226b57

Browse files
committed
feat: 🌍 Add fluent locale() method for localization with
caching.
1 parent c2f6631 commit 8226b57

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

src/ProviderAndDumperAggregator.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ProviderAndDumperAggregator
3434
protected $limit;
3535
protected $results;
3636
protected $isCaching = true;
37+
protected ?string $locale = null;
3738

3839
public function __construct()
3940
{
@@ -98,12 +99,23 @@ public function dump(string $dumper) : Collection
9899

99100
public function geocode(string $value) : self
100101
{
102+
if ($this->locale !== null) {
103+
return $this->geocodeQuery(GeocodeQuery::create($value)->withLocale($this->locale));
104+
}
105+
101106
$cacheKey = (new Str)->slug(strtolower(urlencode($value)));
102107
$this->results = $this->cacheRequest($cacheKey, [$value], "geocode");
103108

104109
return $this;
105110
}
106111

112+
public function locale(?string $locale) : self
113+
{
114+
$this->locale = $locale;
115+
116+
return $this;
117+
}
118+
107119
public function geocodeQuery(GeocodeQuery $query) : self
108120
{
109121
$cacheKey = serialize($query);
@@ -169,6 +181,12 @@ public function registerProvidersFromConfig(Collection $providers) : self
169181

170182
public function reverse(float $latitude, float $longitude) : self
171183
{
184+
if ($this->locale !== null) {
185+
return $this->reverseQuery(
186+
ReverseQuery::fromCoordinates($latitude, $longitude)->withLocale($this->locale)
187+
);
188+
}
189+
172190
$cacheKey = strtolower(urlencode("{$latitude}-{$longitude}"));
173191
$this->results = $this->cacheRequest($cacheKey, [$latitude, $longitude], "reverse");
174192

tests/Feature/Providers/GeocoderServiceTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,50 @@
292292
Http::assertSent(fn ($request) => $request->header('User-Agent')[0] === 'ContainerBoundAgent/1.0');
293293
});
294294

295+
it('propagates locale via the fluent locale() method on geocode()', function () {
296+
app('geocoder')
297+
->locale('it')
298+
->geocode('1600 Pennsylvania Ave NW, Washington, DC 20500, USA')
299+
->get();
300+
301+
Http::assertSent(fn ($request) => str_contains($request->url(), 'accept-language=it'));
302+
});
303+
304+
it('propagates locale via the fluent locale() method on reverse()', function () {
305+
app('geocoder')
306+
->locale('fr')
307+
->reverse(38.8791981, -76.9818437)
308+
->get();
309+
310+
Http::assertSent(fn ($request) =>
311+
str_contains($request->url(), '/reverse')
312+
&& str_contains($request->url(), 'accept-language=fr')
313+
);
314+
});
315+
316+
it('caches locale-scoped convenience-method results separately per locale', function () {
317+
app('geocoder')->locale('it')->geocode('Washington')->get();
318+
app('geocoder')->locale('fr')->geocode('Washington')->get();
319+
app('geocoder')->locale('it')->geocode('Washington')->get();
320+
321+
Http::assertSentCount(2);
322+
});
323+
324+
it('propagates locale from geocodeQuery to the provider and caches per-locale', function () {
325+
$italianQuery = GeocodeQuery::create('1600 Pennsylvania Ave NW, Washington, DC 20500, USA')
326+
->withLocale('it');
327+
$frenchQuery = GeocodeQuery::create('1600 Pennsylvania Ave NW, Washington, DC 20500, USA')
328+
->withLocale('fr');
329+
330+
app('geocoder')->geocodeQuery($italianQuery)->get();
331+
app('geocoder')->geocodeQuery($frenchQuery)->get();
332+
app('geocoder')->geocodeQuery($italianQuery)->get();
333+
334+
Http::assertSent(fn ($request) => str_contains($request->url(), 'accept-language=it'));
335+
Http::assertSent(fn ($request) => str_contains($request->url(), 'accept-language=fr'));
336+
Http::assertSentCount(2);
337+
});
338+
295339
it('does not collide reverse cache keys across coordinate signs', function () {
296340
$providerName = app('geocoder')->getProvider()->getName();
297341
$negativeKey = sha1("{$providerName}-" . strtolower(urlencode('-45.473282--73.834721')));

0 commit comments

Comments
 (0)