Detectors
Resolution order
SetLocale walks these sources, first match wins:
- URL -
{locale}segment of awith_locale.*route (/de/about->de). - Route action - the
localeaction attribute set byRoute::translate(). - Session - locale from a previous request.
- Cookie - persisted client-side.
- Detectors - see below.
fallback_localefromconfig/app.php.
Only URL and route action override an existing session/cookie. Without a URL signal (request came in as /about, not /de/about), SetLocale keeps the session/cookie value. That way a user who picked German isn't reset to English on every unprefixed link, and RedirectLocale can send them to the prefixed variant.
This is also why a switcher uses Route::localizedSwitcherUrl() (always prefixed): the URL itself flips the active locale on click. See Language Switcher.
Built-in detectors
Detectors run only when steps 1-3 produce nothing (typically a first visit). Two are registered by default:
UserDetector- reads from the authenticated user model.BrowserDetector- readsAccept-Language.
Custom detectors
Add to localizer.detectors:
php
// config/localizer.php
'detectors' => [
\App\Locale\CustomDetector::class,
\NielsNumbers\LaravelLocalizer\Detectors\UserDetector::class,
\NielsNumbers\LaravelLocalizer\Detectors\BrowserDetector::class,
],Implement DetectorInterface:
php
namespace App\Locale;
use Illuminate\Http\Request;
use NielsNumbers\LaravelLocalizer\Contracts\DetectorInterface;
class CustomDetector implements DetectorInterface
{
public function detect(Request $request): ?string
{
return $request->header('X-App-Locale');
}
}Return a locale string, or null to defer to the next detector.