Skip to content

Commit 1b441c3

Browse files
committed
Make Localizer optional
1 parent 83cac56 commit 1b441c3

File tree

4 files changed

+124
-4
lines changed

4 files changed

+124
-4
lines changed

config/localized-routes.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,29 @@
2121
*/
2222
'use_locale_middleware' => false,
2323

24+
/**
25+
* If true, this package will use 'codezero/laravel-localizer'
26+
* to detect and set the preferred supported locale.
27+
*
28+
* For non-localized routes, it will look for a locale in the URL,
29+
* in the session, in a cookie, in the browser or in the app config.
30+
* This can be very useful if you have a generic home page.
31+
*
32+
* If a locale is detected, it will be stored in the session,
33+
* in a cookie and as the app locale.
34+
*
35+
* If you disable this option, only localized routes will have a locale
36+
* and only the app locale will be set (so not in the session or cookie).
37+
*
38+
* You can publish its config file and tweak it for your needs.
39+
* This package will only override its 'supported-locales' option
40+
* with the 'supported-locales' option in this file.
41+
*
42+
* For more info, visit:
43+
* https://github.com/codezero-be/laravel-localizer
44+
*
45+
* This option has no effect if 'use_locale_middleware' is false.
46+
*/
47+
'use_localizer' => true,
48+
2449
];

src/Middleware/SetLocale.php

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

55
use Closure;
66
use CodeZero\Localizer\Localizer;
7+
use Illuminate\Support\Facades\App;
78
use Illuminate\Support\Facades\Config;
89

910
class SetLocale
@@ -36,14 +37,21 @@ public function __construct(Localizer $localizer)
3637
public function handle($request, Closure $next)
3738
{
3839
// This package sets a custom route attribute for the locale.
39-
// If it is present, set it as the locale with 'codezero/laravel-localizer'.
40-
// If not, auto-detect the locale with 'codezero/laravel-localizer'
41-
$locale = $request->route()->getAction('localized-routes-locale') ?: $this->detectLocales();
40+
// If it is present, use this as the locale.
41+
$locale = $request->route()->getAction('localized-routes-locale');
4242

43-
if ($locale) {
43+
if ( ! $locale && $this->shouldUseLocalizer()) {
44+
$locale = $this->detectLocales();
45+
}
46+
47+
if ($locale && $this->shouldUseLocalizer()) {
4448
$this->localizer->store($locale);
4549
}
4650

51+
if ($locale && ! $this->shouldUseLocalizer()) {
52+
App::setLocale($locale);
53+
}
54+
4755
return $next($request);
4856
}
4957

@@ -60,4 +68,14 @@ public function detectLocales()
6068

6169
return $this->localizer->detect();
6270
}
71+
72+
/**
73+
* Check if the 'use_localizer' option is enabled.
74+
*
75+
* @return bool
76+
*/
77+
protected function shouldUseLocalizer()
78+
{
79+
return Config::get('localized-routes.use_localizer', false);
80+
}
6381
}

tests/TestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ protected function setUseLocaleMiddleware($value)
4747
Config::set('localized-routes.use_locale_middleware', $value);
4848
}
4949

50+
/**
51+
* Set the use_localizer config option
52+
*
53+
* @param boolean $value
54+
*
55+
* @return void
56+
*/
57+
protected function setUseLocalizer($value)
58+
{
59+
Config::set('localized-routes.use_localizer', $value);
60+
}
61+
5062
/**
5163
* Get the currently registered routes.
5264
*

tests/Unit/Middleware/SetLocaleTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public function it_allows_for_localized_route_model_binding()
101101
public function it_does_not_detect_the_preferred_locale_with_localizer_for_localized_routes()
102102
{
103103
$this->setSupportedLocales(['en', 'nl']);
104+
$this->setUseLocalizer(true);
104105

105106
$localizer = Mockery::mock(Localizer::class);
106107
$localizer->shouldReceive('setSupportedLocales')->with(['en', 'nl']);
@@ -122,6 +123,7 @@ public function it_does_not_detect_the_preferred_locale_with_localizer_for_local
122123
public function it_detects_the_preferred_locale_with_localizer_for_non_localized_routes()
123124
{
124125
$this->setSupportedLocales(['en', 'nl']);
126+
$this->setUseLocalizer(true);
125127

126128
$localizer = Mockery::mock(Localizer::class);
127129
$localizer->shouldReceive('setSupportedLocales')->with(['en', 'nl']);
@@ -136,4 +138,67 @@ public function it_detects_the_preferred_locale_with_localizer_for_non_localized
136138

137139
$this->call('GET', '/non-localized-route')->assertOk();
138140
}
141+
142+
/** @test */
143+
public function it_does_not_use_localizer_when_disabled()
144+
{
145+
$this->setSupportedLocales(['en', 'nl']);
146+
$this->setUseLocalizer(false);
147+
148+
$localizer = Mockery::mock(Localizer::class);
149+
$localizer->shouldNotReceive('setSupportedLocales');
150+
$localizer->shouldNotReceive('detect');
151+
$localizer->shouldNotReceive('store');
152+
153+
App::instance(Localizer::class, $localizer);
154+
155+
Route::localized(function () {
156+
Route::get('localized-route', function () {})
157+
->name('localized.route')
158+
->middleware(['web', SetLocale::class]);
159+
});
160+
161+
Route::get('/non-localized-route', function () {})
162+
->name('non-localized.route')
163+
->middleware(['web', SetLocale::class]);
164+
165+
$this->call('GET', '/non-localized-route')->assertOk();
166+
$this->call('GET', '/en/localized-route')->assertOk();
167+
}
168+
169+
/** @test */
170+
public function it_still_sets_the_app_locale_for_localized_routes_if_localizer_is_disabled()
171+
{
172+
$this->setSupportedLocales(['en']);
173+
$this->setUseLocalizer(false);
174+
175+
App::setLocale('fr');
176+
177+
Route::localized(function () {
178+
Route::get('localized-route', function () {
179+
return App::getLocale();
180+
})->name('localized.route')->middleware(['web', SetLocale::class]);
181+
});
182+
183+
$response = $this->call('GET', '/en/localized-route');
184+
185+
$this->assertEquals('en', $response->original);
186+
}
187+
188+
/** @test */
189+
public function it_does_not_set_the_app_locale_for_non_localized_routes_if_localizer_is_disabled()
190+
{
191+
$this->setSupportedLocales(['en', 'nl']);
192+
$this->setUseLocalizer(false);
193+
194+
App::setLocale('fr');
195+
196+
Route::get('/non-localized-route', function () {
197+
return App::getLocale();
198+
})->name('non-localized.route')->middleware(['web', SetLocale::class]);
199+
200+
$response = $this->call('GET', '/non-localized-route');
201+
202+
$this->assertEquals('fr', $response->original);
203+
}
139204
}

0 commit comments

Comments
 (0)