Skip to content

Commit e28501d

Browse files
committed
Fix setting locale for fallback routes when omitting main locale
1 parent c720a81 commit e28501d

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

src/Middleware/SetLocale.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use Closure;
66
use CodeZero\LocalizedRoutes\LocaleHandler;
7+
use CodeZero\Localizer\Detectors\UrlDetector;
78
use Illuminate\Support\Facades\App;
9+
use Illuminate\Support\Facades\Config;
810

911
class SetLocale
1012
{
@@ -20,10 +22,52 @@ public function handle($request, Closure $next)
2022
{
2123
// This package sets a custom route attribute for the locale.
2224
// If it is present, use this as the locale.
23-
$locale = $request->route()->getAction('localized-routes-locale');
25+
$locale = $request->route()->getAction('localized-routes-locale')
26+
?: $this->getLocaleFromFallbackRoute($request);
2427

2528
App::make(LocaleHandler::class)->handleLocale($locale);
2629

2730
return $next($request);
2831
}
32+
33+
/**
34+
* Get locale from fallback route.
35+
*
36+
* @param \Illuminate\Http\Request $request
37+
*
38+
* @return string|null
39+
*/
40+
protected function getLocaleFromFallbackRoute(\Illuminate\Http\Request $request)
41+
{
42+
if ( ! $request->route()->isFallback) {
43+
return null;
44+
}
45+
46+
$locale = App::make(UrlDetector::class)->detect();
47+
$localeIsSupported = in_array($locale, $this->getSupportedLocales());
48+
$omittedLocale = Config::get('localized-routes.omit_url_prefix_for_locale');
49+
50+
if ( ! $localeIsSupported && $omittedLocale) {
51+
return $omittedLocale;
52+
}
53+
54+
return $locale;
55+
}
56+
57+
/**
58+
* Get the supported locales and not the custom domains.
59+
*
60+
* @return array
61+
*/
62+
protected function getSupportedLocales()
63+
{
64+
$locales = Config::get('localized-routes.supported-locales', []);
65+
$keys = array_keys($locales);
66+
67+
if ( ! empty($locales) && is_numeric($keys[0])) {
68+
return $locales;
69+
}
70+
71+
return $keys;
72+
}
2973
}

tests/Unit/Macros/LocalizedUrlMacroTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CodeZero\LocalizedRoutes\Tests\Unit\Macros;
44

5+
use CodeZero\LocalizedRoutes\Middleware\SetLocale;
56
use CodeZero\LocalizedRoutes\Tests\Stubs\Model;
67
use CodeZero\LocalizedRoutes\Tests\Stubs\ModelWithCustomRouteParameters;
78
use CodeZero\LocalizedRoutes\Tests\TestCase;
@@ -247,15 +248,15 @@ public function it_returns_a_localized_url_for_a_localized_fallback_route()
247248
public function it_returns_a_localized_url_for_a_non_localized_fallback_route_if_the_url_contains_a_supported_locale()
248249
{
249250
$this->setSupportedLocales(['en', 'nl']);
250-
$this->setAppLocale('nl');
251+
$this->setAppLocale('en');
251252

252253
Route::fallback(function () {
253254
return response([
254255
'current' => Route::localizedUrl(),
255256
'en' => Route::localizedUrl('en'),
256257
'nl' => Route::localizedUrl('nl'),
257258
], 404);
258-
})->name('404');
259+
})->middleware(SetLocale::class)->name('404');
259260

260261
$response = $this->call('GET', '/nl/non/existing/route');
261262
$response->assertNotFound();
@@ -294,15 +295,15 @@ public function it_returns_a_localized_url_for_a_non_localized_fallback_route_wh
294295
{
295296
$this->setSupportedLocales(['en', 'nl']);
296297
$this->setOmitUrlPrefixForLocale('nl');
297-
$this->setAppLocale('nl');
298+
$this->setAppLocale('en');
298299

299300
Route::fallback(function () {
300301
return response([
301302
'current' => Route::localizedUrl(),
302303
'en' => Route::localizedUrl('en'),
303304
'nl' => Route::localizedUrl('nl'),
304305
], 404);
305-
})->name('404');
306+
})->middleware(SetLocale::class)->name('404');
306307

307308
$response = $this->call('GET', '/non/existing/route');
308309
$response->assertNotFound();

tests/Unit/Middleware/SetLocaleTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ public function it_sets_the_right_locale_when_accessing_localized_routes_with_om
5555
$this->assertEquals('en', $response->original);
5656
}
5757

58+
/** @test */
59+
public function it_sets_the_right_locale_when_accessing_non_localized_fallback_routes_with_omitted_prefix()
60+
{
61+
$this->setSupportedLocales(['en', 'nl']);
62+
$this->setOmitUrlPrefixForLocale('nl');
63+
$this->setAppLocale('en');
64+
65+
Route::fallback(function () {
66+
return App::getLocale();
67+
})->middleware(SetLocale::class);
68+
69+
$response = $this->call('GET', '/non/existing/route');
70+
$response->assertOk();
71+
$this->assertEquals('nl', $response->original);
72+
73+
$response = $this->call('GET', '/en/non/existing/route');
74+
$response->assertOk();
75+
$this->assertEquals('en', $response->original);
76+
}
77+
5878
/** @test */
5979
public function it_sets_the_locale_for_localized_routes_within_route_groups()
6080
{

0 commit comments

Comments
 (0)