diff --git a/README.md b/README.md index 42ad504..88242fc 100644 --- a/README.md +++ b/README.md @@ -88,11 +88,15 @@ Add any locales you wish to support to your published `config/localizer.php` fil By default, the middleware will use the following detectors to check for a supported locale in: 1. The URL slug -2. The authenticated user model -3. The session -4. A cookie -5. The browser -6. The app's default locale +2. A main omitted locale +3. The authenticated user model +4. The session +5. A cookie +6. The browser +7. The app's default locale + +If you set an omitted locale, no additional detectors will run after the `OmittedLocaleDetector`. +This makes sense, because the locale will always be determined by the URL in this scenario. You can configure the session key, cookie name and the attribute on the user model that holds the locale. By default this is all set to `locale`. If the user model does not have this attribute, it will skip this check. diff --git a/config/localizer.php b/config/localizer.php index 46a7634..025cc68 100644 --- a/config/localizer.php +++ b/config/localizer.php @@ -7,12 +7,21 @@ */ 'supported-locales' => [], + /** + * If your main locale is omitted from the URL, set it here. + * It will always be used if no supported locale is found in the URL. + * Note that no other detectors will run after the OmittedLocaleDetector! + * Setting this option to `null` will disable this detector. + */ + 'omitted-locale' => null, + /** * The detectors to use to find a matching locale. * These will be executed in the order that they are added to the array! */ 'detectors' => [ CodeZero\Localizer\Detectors\UrlDetector::class, + CodeZero\Localizer\Detectors\OmittedLocaleDetector::class, CodeZero\Localizer\Detectors\UserDetector::class, CodeZero\Localizer\Detectors\SessionDetector::class, CodeZero\Localizer\Detectors\CookieDetector::class, diff --git a/src/Detectors/OmittedLocaleDetector.php b/src/Detectors/OmittedLocaleDetector.php new file mode 100644 index 0000000..5f9f8d8 --- /dev/null +++ b/src/Detectors/OmittedLocaleDetector.php @@ -0,0 +1,18 @@ +assertEquals('nl', $response->original); } + /** @test */ + public function it_checks_for_a_configured_omitted_locale() + { + $this->setSupportedLocales(['en', 'nl', 'fr', 'de', 'es', 'it']); + $this->setOmittedLocale('nl'); + $this->setSessionLocale('fr'); + $this->setBrowserLocales('it'); + $this->setAppLocale('en'); + $cookie = 'de'; + + Route::get('some/route', function () { + return App::getLocale(); + })->middleware(['web', SetLocale::class]); + + $response = $this->getWithCookie('some/route', $cookie); + + $response->assertSessionHas($this->sessionKey, 'nl'); + $response->assertCookie($this->cookieName, 'nl'); + $this->assertEquals('nl', $response->original); + } + /** @test */ public function it_looks_for_a_locale_on_the_authenticated_user_if_not_found_in_the_url() { @@ -250,6 +271,20 @@ protected function setSupportedLocales(array $locales) return $this; } + /** + * Set the omitted locale. + * + * @param string $locale + * + * @return $this + */ + protected function setOmittedLocale($locale) + { + Config::set('localizer.omitted-locale', $locale); + + return $this; + } + /** * Set the locale in the session. *