Skip to content

Commit 2d65b35

Browse files
committed
Use a fallback locale when generating a url for an unregistered localized route
1 parent 6f9e9cc commit 2d65b35

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

src/Illuminate/Routing/UrlGenerator.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,29 @@ protected function resolveLocalizedRouteName($name, $locale, $currentLocale)
9393
return $name;
9494
}
9595

96+
// Normalize the route name by removing any locale prefix.
97+
// We will prepend the applicable locale manually.
98+
$baseName = $this->stripLocaleFromRouteName($name);
99+
100+
if ($baseName === '') {
101+
return '';
102+
}
103+
96104
// Use the specified or current locale
97105
// as a prefix for the route name.
98106
$locale = $locale ?: $currentLocale;
107+
$newName = "{$locale}.{$baseName}";
108+
$fallbackLocale = LocaleConfig::getFallbackLocale();
99109

100-
// If the locale is not supported, use a fallback
101-
// locale if one is configured.
102-
if ( ! LocaleConfig::isSupportedLocale($locale)) {
103-
$locale = LocaleConfig::getFallbackLocale() ?: $locale;
110+
// If the localized route name doesn't exist,
111+
// use a fallback locale if one is configured.
112+
if ( ! Route::has($newName) && $fallbackLocale) {
113+
$newName = "{$fallbackLocale}.{$baseName}";
104114
}
105115

106-
// Normalize the route name by removing any locale prefix.
107-
// We will prepend the applicable locale manually.
108-
$baseName = $this->stripLocaleFromRouteName($name);
109-
110-
// If the route has a name (not just the locale prefix)
111-
// add the requested locale prefix.
112-
$newName = $baseName ? "{$locale}.{$baseName}" : '';
113-
114-
// If the new localized route name does not exist, but the unprefixed route name does,
115-
// someone is calling "route($name, [], true, $locale)" on a non localized route.
116+
// If the unprefixed route name exists, but the new localized route name doesn't,
117+
// someone may be trying to resolve a localized name in an unsupported locale,
118+
// e.g. "route('en.route', [], true, 'fr')" (where 'fr.route' doesn't exist and 'route' does)
116119
// In that case, resolve the unprefixed route name.
117120
if (Route::has($baseName) && ! Route::has($newName)) {
118121
$newName = $baseName;

tests/Unit/Illuminate/Routing/UrlGeneratorTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,14 @@ function it_uses_a_fallback_locale_when_the_requested_locale_is_unsupported()
184184
/** @test */
185185
public function it_uses_a_fallback_locale_when_the_requested_locale_is_not_registered()
186186
{
187-
// $this->setSupportedLocales(['en', 'nl']);
188-
// $this->setAppLocale('en');
189-
// $this->setFallbackLocale('en');
190-
//
191-
// Route::get('en/route')->name('en.route');
192-
//
193-
// $this->assertEquals(URL::to('en/route'), URL::route('route', [], true, 'en'));
194-
// $this->assertEquals(URL::to('en/route'), URL::route('route', [], true, 'nl'));
187+
$this->setSupportedLocales(['en', 'nl']);
188+
$this->setAppLocale('en');
189+
$this->setFallbackLocale('en');
190+
191+
Route::get('en/route')->name('en.route');
192+
193+
$this->assertEquals(URL::to('en/route'), URL::route('route', [], true, 'en'));
194+
$this->assertEquals(URL::to('en/route'), URL::route('route', [], true, 'nl'));
195195
}
196196

197197
/** @test */

0 commit comments

Comments
 (0)