Add English to the UI and a profile menu for language and theme
Every UI string now exists in lang/fi and lang/en; a unit test checks that both languages have the same keys and placeholders. A user's language is stored on the account (users.locale) and applied by the SetLocale web middleware, falling back to the session's choice on signed-out pages and then APP_LOCALE. Emails use the user's language through HasLocalePreference, and the login page keeps the language after logout. The header's name, theme toggle and logout button become one profile menu (a disclosure) with the language buttons, light/dark/system theme radios kept on the device, and logging out. Signed-out pages get a Suomi/English switch next to the theme toggle. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Modules\Users\Presentation\Http\Controllers;
|
||||
|
||||
use App\Modules\Users\Presentation\Http\Middleware\SetLocale;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
@@ -10,10 +11,15 @@ class LogoutController
|
||||
{
|
||||
public function __invoke(Request $request): RedirectResponse
|
||||
{
|
||||
$locale = app()->getLocale();
|
||||
|
||||
Auth::guard('web')->logout();
|
||||
$request->session()->invalidate();
|
||||
$request->session()->regenerateToken();
|
||||
|
||||
// The login page stays in the language the user had chosen.
|
||||
$request->session()->put(SetLocale::SESSION_KEY, $locale);
|
||||
|
||||
return redirect()->route('login')->with('status', __('auth.logged_out'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
$request->session()->put(SetLocale::SESSION_KEY, $locale->value);
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?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';
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user