Skip to content

Commit 6695309

Browse files
committed
Fix registering duplicate locale when using domains (#32)
1 parent 4daffcb commit 6695309

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/Macros/LocalizedRoutesMacro.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public static function register()
2828
$setMiddleware = $options['use_locale_middleware']
2929
?? Config::get('localized-routes.use_locale_middleware', false);
3030

31-
if ($omitPrefix) {
31+
$notUsingDomains = is_numeric(array_key_first($locales));
32+
33+
if ($omitPrefix && $notUsingDomains) {
3234
// Move the omitted locale to the end of the array
3335
// to avoid root placeholders catching existing slugs.
3436
// https://github.com/codezero-be/laravel-localized-routes/issues/28
@@ -42,7 +44,7 @@ public static function register()
4244
// Allow supported locales to be a
4345
// simple array of locales or an
4446
// array of ['locale' => 'domain']
45-
if (is_numeric($locale)) {
47+
if ($notUsingDomains) {
4648
$locale = $domain;
4749
$domain = null;
4850
}

tests/Unit/Macros/LocalizedRoutesMacroTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,45 @@ public function it_maps_a_custom_domain_to_each_locale()
142142
$this->assertEquals('/', $route->uri);
143143
}
144144

145+
/** @test */
146+
public function it_registers_routes_in_the_correct_order_without_prefix_for_a_configured_main_locale_with_domains()
147+
{
148+
$this->setSupportedLocales([
149+
'en' => 'english-domain.com',
150+
'nl' => 'dutch-domain.com',
151+
]);
152+
$this->setOmitUrlPrefixForLocale('en');
153+
154+
Route::localized(function () {
155+
Route::get('/', function () { return 'Home '.App::getLocale(); })->name('home');
156+
Route::get('{slug}', function () { return 'Dynamic '.App::getLocale(); })->name('catch-all');
157+
});
158+
159+
$routes = $this->getRoutes();
160+
161+
$this->assertCount(4, $routes);
162+
163+
$route = $routes[0];
164+
$this->assertEquals('english-domain.com', $route->action['domain']);
165+
$this->assertEquals('en.home', $route->action['as']);
166+
$this->assertEquals('/', $route->uri);
167+
168+
$route = $routes[1];
169+
$this->assertEquals('english-domain.com', $route->action['domain']);
170+
$this->assertEquals('en.catch-all', $route->action['as']);
171+
$this->assertEquals('{slug}', $route->uri);
172+
173+
$route = $routes[2];
174+
$this->assertEquals('dutch-domain.com', $route->action['domain']);
175+
$this->assertEquals('nl.home', $route->action['as']);
176+
$this->assertEquals('/', $route->uri);
177+
178+
$route = $routes[3];
179+
$this->assertEquals('dutch-domain.com', $route->action['domain']);
180+
$this->assertEquals('nl.catch-all', $route->action['as']);
181+
$this->assertEquals('{slug}', $route->uri);
182+
}
183+
145184
/** @test */
146185
public function it_temporarily_changes_the_app_locale_when_registering_the_routes()
147186
{

0 commit comments

Comments
 (0)