Files
petriandClaude Opus 5.5 45dbb04344 Keep the chosen language across sign-in and forced sign-out
A language picked on a signed-out page is now stored on the account when
the user signs in (StoreLocaleChosenBeforeLogin), so the app does not
switch back to the account default. EnsureAccountIsUsable keeps the user's
language for the login page when it ends a disabled account's session.

ChangePreferredLocale runs in a transaction like the other use cases, the
language names come from layout.languages instead of the Locale enum, and
the translation test also compares fi.json with en.json and rejects
English files without a Finnish counterpart.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-25 07:09:22 +02:00

44 lines
1.2 KiB
PHP

<?php
namespace App\Modules\Users\Presentation\Http\Middleware;
use App\Models\User;
use App\Modules\Users\Domain\Enums\Locale;
use Carbon\Carbon;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Symfony\Component\HttpFoundation\Response;
/**
* Sets the language of the user interface: the signed-in user's own choice,
* otherwise the one chosen on the signed-out pages (kept in the session),
* otherwise the application default.
*/
class SetLocale
{
public const SESSION_KEY = 'locale';
/**
* Set when the user picked the language on a signed-out page: signing in
* then stores it on the account (StoreLocaleChosenBeforeLogin).
*/
public const CHOSEN_SESSION_KEY = 'locale_chosen';
public function handle(Request $request, Closure $next): Response
{
$user = $request->user();
$locale = $user instanceof User
? $user->locale
: Locale::tryFrom((string) ($request->hasSession() ? $request->session()->get(self::SESSION_KEY) : ''));
if ($locale !== null) {
App::setLocale($locale->value);
Carbon::setLocale($locale->value);
}
return $next($request);
}
}