Skip to content

Commit d65f639

Browse files
authored
Add OmittedLocaleDetector (#10)
1 parent 8941000 commit d65f639

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,15 @@ Add any locales you wish to support to your published `config/localizer.php` fil
8888
By default, the middleware will use the following detectors to check for a supported locale in:
8989

9090
1. The URL slug
91-
2. The authenticated user model
92-
3. The session
93-
4. A cookie
94-
5. The browser
95-
6. The app's default locale
91+
2. A main omitted locale
92+
3. The authenticated user model
93+
4. The session
94+
5. A cookie
95+
6. The browser
96+
7. The app's default locale
97+
98+
If you set an omitted locale, no additional detectors will run after the `OmittedLocaleDetector`.
99+
This makes sense, because the locale will always be determined by the URL in this scenario.
96100

97101
You can configure the session key, cookie name and the attribute on the user model that holds the locale.
98102
By default this is all set to `locale`. If the user model does not have this attribute, it will skip this check.

config/localizer.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@
77
*/
88
'supported-locales' => [],
99

10+
/**
11+
* If your main locale is omitted from the URL, set it here.
12+
* It will always be used if no supported locale is found in the URL.
13+
* Note that no other detectors will run after the OmittedLocaleDetector!
14+
* Setting this option to `null` will disable this detector.
15+
*/
16+
'omitted-locale' => null,
17+
1018
/**
1119
* The detectors to use to find a matching locale.
1220
* These will be executed in the order that they are added to the array!
1321
*/
1422
'detectors' => [
1523
CodeZero\Localizer\Detectors\UrlDetector::class,
24+
CodeZero\Localizer\Detectors\OmittedLocaleDetector::class,
1625
CodeZero\Localizer\Detectors\UserDetector::class,
1726
CodeZero\Localizer\Detectors\SessionDetector::class,
1827
CodeZero\Localizer\Detectors\CookieDetector::class,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace CodeZero\Localizer\Detectors;
4+
5+
use Illuminate\Support\Facades\Config;
6+
7+
class OmittedLocaleDetector implements Detector
8+
{
9+
/**
10+
* Detect the locale.
11+
*
12+
* @return string|array|null
13+
*/
14+
public function detect()
15+
{
16+
return Config::get('localizer.omitted-locale') ?: null;
17+
}
18+
}

tests/Feature/SetLocaleTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ public function you_can_configure_which_segment_to_use_as_locale()
7373
$this->assertEquals('nl', $response->original);
7474
}
7575

76+
/** @test */
77+
public function it_checks_for_a_configured_omitted_locale()
78+
{
79+
$this->setSupportedLocales(['en', 'nl', 'fr', 'de', 'es', 'it']);
80+
$this->setOmittedLocale('nl');
81+
$this->setSessionLocale('fr');
82+
$this->setBrowserLocales('it');
83+
$this->setAppLocale('en');
84+
$cookie = 'de';
85+
86+
Route::get('some/route', function () {
87+
return App::getLocale();
88+
})->middleware(['web', SetLocale::class]);
89+
90+
$response = $this->getWithCookie('some/route', $cookie);
91+
92+
$response->assertSessionHas($this->sessionKey, 'nl');
93+
$response->assertCookie($this->cookieName, 'nl');
94+
$this->assertEquals('nl', $response->original);
95+
}
96+
7697
/** @test */
7798
public function it_looks_for_a_locale_on_the_authenticated_user_if_not_found_in_the_url()
7899
{
@@ -250,6 +271,20 @@ protected function setSupportedLocales(array $locales)
250271
return $this;
251272
}
252273

274+
/**
275+
* Set the omitted locale.
276+
*
277+
* @param string $locale
278+
*
279+
* @return $this
280+
*/
281+
protected function setOmittedLocale($locale)
282+
{
283+
Config::set('localizer.omitted-locale', $locale);
284+
285+
return $this;
286+
}
287+
253288
/**
254289
* Set the locale in the session.
255290
*

0 commit comments

Comments
 (0)