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:
2026-11-25 06:28:39 +02:00
co-authored by Claude Opus 5.5
parent c2265256c3
commit 8aead06972
40 changed files with 2197 additions and 44 deletions
+23 -2
View File
@@ -3,10 +3,12 @@
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Modules\Users\Domain\Enums\Locale;
use App\Modules\Users\Domain\Enums\MoneyPermission;
use App\Modules\Users\Domain\Enums\Role;
use App\Modules\Users\Infrastructure\Persistence\Eloquent\UserMoneyPermissionsModel;
use Database\Factories\UserFactory;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -27,15 +29,25 @@ use Illuminate\Support\Carbon;
* @property bool $is_active
* @property Carbon|null $employment_ends_on
* @property int|null $supervisor_id
* @property Locale $locale
* @property-read UserMoneyPermissionsModel|null $moneyPermissions
*/
#[Fillable(['name', 'email', 'password', 'role', 'is_active', 'employment_ends_on', 'supervisor_id'])]
#[Fillable(['name', 'email', 'password', 'role', 'is_active', 'employment_ends_on', 'supervisor_id', 'locale'])]
#[Hidden(['password', 'remember_token'])]
class User extends Authenticatable
class User extends Authenticatable implements HasLocalePreference
{
/** @use HasFactory<UserFactory> */
use HasFactory, Notifiable;
/**
* The column defaults, so a user made in this request has them too.
*
* @var array<string, mixed>
*/
protected $attributes = [
'locale' => 'fi',
];
/**
* @return BelongsTo<User, $this>
*/
@@ -66,6 +78,14 @@ class User extends Authenticatable
&& ($this->moneyPermissions?->allows($permission) ?? false);
}
/**
* The language of the user's emails, such as the set-password link.
*/
public function preferredLocale(): string
{
return $this->locale->value;
}
/**
* Get the attributes that should be cast.
*
@@ -79,6 +99,7 @@ class User extends Authenticatable
'role' => Role::class,
'is_active' => 'boolean',
'employment_ends_on' => 'date',
'locale' => Locale::class,
];
}
}
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\Modules\Users\Application\UseCases;
use App\Modules\Users\Domain\Contracts\UserAccountRepository;
use App\Modules\Users\Domain\Enums\Locale;
/**
* A user chooses the language of the user interface from their profile menu.
* The choice is kept on the account, so it follows them to every device.
*/
final class ChangePreferredLocale
{
public function __construct(private UserAccountRepository $users) {}
public function execute(int $userId, Locale $locale): void
{
$this->users->changeLocale($userId, $locale);
}
}
@@ -6,6 +6,7 @@ namespace App\Modules\Users\Domain\Contracts;
use App\Modules\Shared\Domain\ValueObjects\EmailAddress;
use App\Modules\Users\Domain\Entities\UserAccount;
use App\Modules\Users\Domain\Enums\Locale;
interface UserAccountRepository
{
@@ -29,4 +30,9 @@ interface UserAccountRepository
public function countActiveAdmins(?int $exceptUserId = null): int;
public function countActiveInternsSupervisedBy(int $supervisorId): int;
/**
* Stores the user's language of the user interface.
*/
public function changeLocale(int $userId, Locale $locale): void;
}
+26
View File
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace App\Modules\Users\Domain\Enums;
/**
* The languages of the user interface. A user's choice is kept on their
* account; signed-out pages keep it in the session.
*/
enum Locale: string
{
case Finnish = 'fi';
case English = 'en';
/**
* The language's name in the language itself, as language pickers show it.
*/
public function nativeName(): string
{
return match ($this) {
self::Finnish => 'Suomi',
self::English => 'English',
};
}
}
@@ -6,6 +6,7 @@ use App\Models\User;
use App\Modules\Shared\Domain\ValueObjects\EmailAddress;
use App\Modules\Users\Domain\Contracts\UserAccountRepository;
use App\Modules\Users\Domain\Entities\UserAccount;
use App\Modules\Users\Domain\Enums\Locale;
use App\Modules\Users\Domain\Enums\Role;
use App\Modules\Users\Domain\ValueObjects\PersonName;
use App\Modules\Users\Infrastructure\Persistence\Eloquent\UserMoneyPermissionsModel;
@@ -87,6 +88,11 @@ class EloquentUserAccountRepository implements UserAccountRepository
->count();
}
public function changeLocale(int $userId, Locale $locale): void
{
User::query()->whereKey($userId)->update(['locale' => $locale->value]);
}
private function toEntity(User $model): UserAccount
{
return UserAccount::reconstitute(
@@ -15,6 +15,7 @@ use App\Modules\Users\Infrastructure\Persistence\Repositories\EloquentUserAccoun
use App\Modules\Users\Infrastructure\SystemClock;
use App\Modules\Users\Presentation\Console\CreateAdminCommand;
use App\Modules\Users\Presentation\Http\Middleware\EnsureAccountIsUsable;
use App\Modules\Users\Presentation\Http\Middleware\SetLocale;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Contracts\Auth\CanResetPassword;
use Illuminate\Notifications\Messages\MailMessage;
@@ -60,6 +61,9 @@ class UsersServiceProvider extends ServiceProvider
// Ends the user's other sessions when their password changes.
$router->pushMiddlewareToGroup('web', AuthenticateSession::class);
// The language of the user interface; after the session has started.
$router->pushMiddlewareToGroup('web', SetLocale::class);
if ($this->app->runningInConsole()) {
$this->commands([CreateAdminCommand::class]);
}
@@ -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);
}
}
@@ -1,8 +1,11 @@
<?php
use App\Modules\Users\Presentation\Http\Controllers\LogoutController;
use App\Modules\Users\Presentation\Http\Controllers\SwitchLocaleController;
use Illuminate\Support\Facades\Route;
Route::middleware(['web', 'throttle:20,1'])->post('/locale', SwitchLocaleController::class)->name('locale.update');
Route::middleware(['web', 'guest'])->group(function () {
Route::livewire('/login', 'users::login')->name('login');
Route::livewire('/forgot-password', 'users::forgot-password')->name('password.request');