Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions server/src/Support/Geocoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,28 @@ public static function canGoogleGeocode(): bool
return Utils::notEmpty(config('services.google_maps.api_key'));
}

/**
* The language geocoding results are returned in — Google's `language`
* parameter, e.g. `en`, `ru`, `es`.
*/
public static function getLocale(): string
{
// `?:` rather than a config() default: the settings table stores this
// key, so it exists-but-empty on installs that never filled it in, and
// config()'s default only applies when the key is absent entirely.
return config('services.google_maps.locale') ?: env('GOOGLE_MAPS_LOCALE', 'en');
}

/**
* The region requests are biased towards — Google's `region` parameter.
* This is a ccTLD (`us`, `ru`, `sg`), not a language code, and is a
* separate concern from the locale above.
*/
public static function getRegion(): ?string
{
return config('services.google_maps.region') ?: env('GOOGLE_MAPS_REGION', 'us');
}

protected static function makeGeocoder(): object
{
// Allow an alternative geocoder (or test double) to be injected
Expand All @@ -219,10 +241,13 @@ protected static function makeGeocoder(): object
return app('fleetops.geocoder');
}

// Region biases which results rank highest; locale controls the
// language they come back in. Applying both here covers every caller
// rather than each construction site repeating them.
$httpClient = new Client();
$provider = new GoogleMaps($httpClient, null, config('services.google_maps.api_key', env('GOOGLE_MAPS_API_KEY')));
$provider = new GoogleMaps($httpClient, static::getRegion(), config('services.google_maps.api_key', env('GOOGLE_MAPS_API_KEY')));

return new StatefulGeocoder($provider, 'en');
return new StatefulGeocoder($provider, static::getLocale());
}

protected static function makePlaceFromGoogleAddress($googleAddress): Place
Expand Down
23 changes: 23 additions & 0 deletions server/tests/Unit/Support/GeocodingInjectedClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
eval('namespace Fleetbase\Support; function session($key = null, $default = null) { if ($key === null) { return new class { public function has($k) { return \session($k) !== null; } public function get($k, $d = null) { return \session($k, $d); } }; } return \session($key, $default); }');
}

if (!function_exists('Fleetbase\\FleetOps\\Support\\env')) {
eval('namespace Fleetbase\\FleetOps\\Support; function env($key = null, $default = null) { return $default; }');
}

if (!function_exists('Fleetbase\\Observers\\event')) {
eval('namespace Fleetbase\\Observers; function event($event = null, $payload = []) { return []; }');
}
Expand Down Expand Up @@ -243,3 +247,22 @@ public function __call($method, $arguments)
Geocoder\Laravel\Facades\Geocoder::clearResolvedInstance('geocoder');
expect(PlaceSearch::geocode('anywhere'))->toHaveCount(0);
});

test('locale and region resolve independently from configuration', function () {
// Google treats these as two different parameters: `language` decides what
// language results come back in, `region` (a ccTLD) only biases ranking.
// They are read from separate config keys so setting one cannot silently
// change the other.
config()->set('services.google_maps.locale', 'ru');
config()->set('services.google_maps.region', 'sg');

expect(Geocoding::getLocale())->toBe('ru')
->and(Geocoding::getRegion())->toBe('sg');

// Unset, each falls back to its own documented default
config()->set('services.google_maps.locale', null);
config()->set('services.google_maps.region', null);

expect(Geocoding::getLocale())->toBe('en')
->and(Geocoding::getRegion())->toBe('us');
});