Skip to content

Commit

Permalink
Fix registering duplicate locale when using domains (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvermeyen committed Apr 17, 2021
1 parent 4daffcb commit 6695309
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Macros/LocalizedRoutesMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public static function register()
$setMiddleware = $options['use_locale_middleware']
?? Config::get('localized-routes.use_locale_middleware', false);

if ($omitPrefix) {
$notUsingDomains = is_numeric(array_key_first($locales));

if ($omitPrefix && $notUsingDomains) {
// Move the omitted locale to the end of the array
// to avoid root placeholders catching existing slugs.
// https://github.com/codezero-be/laravel-localized-routes/issues/28
Expand All @@ -42,7 +44,7 @@ public static function register()
// Allow supported locales to be a
// simple array of locales or an
// array of ['locale' => 'domain']
if (is_numeric($locale)) {
if ($notUsingDomains) {
$locale = $domain;
$domain = null;
}
Expand Down
39 changes: 39 additions & 0 deletions tests/Unit/Macros/LocalizedRoutesMacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,45 @@ public function it_maps_a_custom_domain_to_each_locale()
$this->assertEquals('/', $route->uri);
}

/** @test */
public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale_with_domains()
{
$this->setSupportedLocales([
'en' => 'english-domain.com',
'nl' => 'dutch-domain.com',
]);
$this->setOmitUrlPrefixForLocale('en');

Route::localized(function () {
Route::get('/', function () { return 'Home '.App::getLocale(); })->name('home');
Route::get('{slug}', function () { return 'Dynamic '.App::getLocale(); })->name('catch-all');
});

$routes = $this->getRoutes();

$this->assertCount(4, $routes);

$route = $routes[0];
$this->assertEquals('english-domain.com', $route->action['domain']);
$this->assertEquals('en.home', $route->action['as']);
$this->assertEquals('/', $route->uri);

$route = $routes[1];
$this->assertEquals('english-domain.com', $route->action['domain']);
$this->assertEquals('en.catch-all', $route->action['as']);
$this->assertEquals('{slug}', $route->uri);

$route = $routes[2];
$this->assertEquals('dutch-domain.com', $route->action['domain']);
$this->assertEquals('nl.home', $route->action['as']);
$this->assertEquals('/', $route->uri);

$route = $routes[3];
$this->assertEquals('dutch-domain.com', $route->action['domain']);
$this->assertEquals('nl.catch-all', $route->action['as']);
$this->assertEquals('{slug}', $route->uri);
}

/** @test */
public function it_temporarily_changes_the_app_locale_when_registering_the_routes()
{
Expand Down

0 comments on commit 6695309

Please sign in to comment.