Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions config/localizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions src/Detectors/OmittedLocaleDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace CodeZero\Localizer\Detectors;

use Illuminate\Support\Facades\Config;

class OmittedLocaleDetector implements Detector
{
/**
* Detect the locale.
*
* @return string|array|null
*/
public function detect()
{
return Config::get('localizer.omitted-locale') ?: null;
}
}
35 changes: 35 additions & 0 deletions tests/Feature/SetLocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ public function you_can_configure_which_segment_to_use_as_locale()
$this->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()
{
Expand Down Expand Up @@ -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.
*
Expand Down