diff --git a/src/Macros/RouterMacros.php b/src/Macros/RouterMacros.php index 96f0a47..a09796a 100644 --- a/src/Macros/RouterMacros.php +++ b/src/Macros/RouterMacros.php @@ -29,4 +29,22 @@ public function multilingual(): Closure ); }; } + + /** + * Check if a route with the given name exists for the current locale. + * + * @param string|array $name + * @return \Closure + */ + public function hasLocalized(): Closure + { + return function ($name) { + $names = array_map( + static fn ($pattern) => locale().".{$pattern}", + is_array($name) ? $name : func_get_args(), + ); + + return $this->has($names); + }; + } } diff --git a/tests/RouteTest.php b/tests/RouteTest.php index 318f73c..7b44ede 100644 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -522,6 +522,17 @@ public function a_route_can_be_registered_with_a_middleware(): void } } + /** @test **/ + public function a_named_route_can_be_checked_if_it_exists(): void + { + $this + ->registerTestRoute() + ->name('foo'); + + $this->assertTrue(Route::hasLocalized('foo')); + $this->assertFalse(Route::hasLocalized('bar')); + } + protected function registerTestRoute(): MultilingualRoutePendingRegistration { $this->registerTestTranslations();