Files
Toiminnanohjaus/app/Modules/Users/Presentation/Http/Controllers/SwitchLocaleController.php
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

42 lines
1.3 KiB
PHP

<?php
namespace App\Modules\Users\Presentation\Http\Controllers;
use App\Models\User;
use App\Modules\Users\Application\UseCases\ChangePreferredLocale;
use App\Modules\Users\Domain\Enums\Locale;
use App\Modules\Users\Presentation\Http\Middleware\SetLocale;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
/**
* Changes the language of the user interface: on the account of a signed-in
* user (from the profile menu), in the session on the signed-out pages.
*/
class SwitchLocaleController
{
public function __invoke(Request $request, ChangePreferredLocale $changeLocale): RedirectResponse
{
$validated = $request->validate([
'locale' => ['required', 'string', Rule::enum(Locale::class)],
]);
$locale = Locale::from($validated['locale']);
$user = $request->user();
if ($user instanceof User) {
$changeLocale->execute($user->id, $locale);
// The signed-in user object may outlive this request (e.g. Octane, in-process browser tests).
$user->locale = $locale;
} else {
$request->session()->put(SetLocale::CHOSEN_SESSION_KEY, true);
}
$request->session()->put(SetLocale::SESSION_KEY, $locale->value);
return redirect()->back();
}
}