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:
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
const PROFILE_MENU = '[data-profile-menu]';
|
||||
|
||||
beforeEach(function () {
|
||||
$this->user = User::factory()->create(['name' => 'Eero Esimerkki']);
|
||||
$this->actingAs($this->user);
|
||||
});
|
||||
|
||||
it('opens and closes the profile menu with the keyboard', function () {
|
||||
visit(route('customers.index'))->inLightMode()
|
||||
->assertAttribute(PROFILE_MENU, 'aria-label', 'Profiili: Eero Esimerkki')
|
||||
->assertAttribute(PROFILE_MENU, 'aria-expanded', 'false')
|
||||
->assertMissing('#profile-menu-panel')
|
||||
->keys(PROFILE_MENU, 'Enter')
|
||||
->assertAttribute(PROFILE_MENU, 'aria-expanded', 'true')
|
||||
->assertVisible('#profile-menu-panel')
|
||||
->assertChecked('[data-theme-option="system"]')
|
||||
->assertAttribute('#profile-menu-panel button[value="fi"]', 'aria-pressed', 'true')
|
||||
->keys('[data-theme-option="system"]', 'Escape')
|
||||
->assertAttribute(PROFILE_MENU, 'aria-expanded', 'false')
|
||||
->assertScript('document.activeElement.hasAttribute("data-profile-menu")', true);
|
||||
});
|
||||
|
||||
it('changes the language from the profile menu', function () {
|
||||
$page = visit(route('customers.index'))->inLightMode()
|
||||
->click(PROFILE_MENU)
|
||||
->click('#profile-menu-panel button[value="en"]')
|
||||
->assertScript('document.documentElement.lang', 'en')
|
||||
->assertAttribute(PROFILE_MENU, 'aria-label', 'Profile: Eero Esimerkki');
|
||||
|
||||
expect($this->user->fresh()->locale->value)->toBe('en');
|
||||
|
||||
$page->click(PROFILE_MENU)
|
||||
->assertAttribute('#profile-menu-panel button[value="en"]', 'aria-pressed', 'true')
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
});
|
||||
|
||||
it('changes the language on the login page', function () {
|
||||
auth()->logout();
|
||||
|
||||
visit(route('login'))
|
||||
->click('button[value="en"]')
|
||||
->assertScript('document.documentElement.lang', 'en')
|
||||
->assertAttribute('button[value="en"]', 'aria-pressed', 'true')
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
});
|
||||
|
||||
it('has no accessibility issues in the open profile menu', function (string $device, string $colorScheme) {
|
||||
$page = visit(route('customers.index'));
|
||||
$page = $device === 'mobile' ? $page->on()->mobile() : $page->on()->desktop();
|
||||
$page = $colorScheme === 'dark' ? $page->inDarkMode() : $page->inLightMode();
|
||||
|
||||
$page->click(PROFILE_MENU)
|
||||
->assertVisible('#profile-menu-panel')
|
||||
->assertNoJavaScriptErrors()
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
})->with([
|
||||
'desktop, light' => ['desktop', 'light'],
|
||||
'desktop, dark' => ['desktop', 'dark'],
|
||||
'mobile, light' => ['mobile', 'light'],
|
||||
'mobile, dark' => ['mobile', 'dark'],
|
||||
]);
|
||||
@@ -37,32 +37,23 @@ it('overrides a dark system theme', function () {
|
||||
->assertScript('localStorage.getItem("dark-theme")', 'light');
|
||||
});
|
||||
|
||||
it('keeps the chosen theme when moving between pages with wire:navigate', function () {
|
||||
it('keeps the theme chosen in the profile menu when moving between pages with wire:navigate', function () {
|
||||
$this->actingAs(User::factory()->admin()->create());
|
||||
|
||||
$page = visit(route('customers.index'))->inLightMode()
|
||||
->click(THEME_TOGGLE)
|
||||
->assertScript('document.documentElement.classList.contains("dark")', true);
|
||||
->click('[data-profile-menu]')
|
||||
->click('[data-theme-option="dark"]')
|
||||
->assertScript('document.documentElement.classList.contains("dark")', true)
|
||||
->assertScript('localStorage.getItem("dark-theme")', 'dark');
|
||||
|
||||
$page->click('[aria-controls="nav-group-maintenance"]')
|
||||
->click('Käyttäjät')
|
||||
->assertPathIs('/users')
|
||||
->assertScript('document.documentElement.classList.contains("dark")', true)
|
||||
->assertAttribute(THEME_TOGGLE, 'aria-pressed', 'true')
|
||||
->click('[aria-controls="nav-group-sales"]')
|
||||
->click('Asiakkaat')
|
||||
->assertPathIs('/customers')
|
||||
->assertScript('document.documentElement.classList.contains("dark")', true)
|
||||
->assertAttribute(THEME_TOGGLE, 'aria-pressed', 'true')
|
||||
->click('[data-profile-menu]')
|
||||
->assertChecked('[data-theme-option="dark"]')
|
||||
->click('[data-theme-option="system"]')
|
||||
->assertScript('localStorage.getItem("dark-theme")', 'system')
|
||||
->assertScript('document.documentElement.classList.contains("dark")', false)
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
});
|
||||
|
||||
it('is in the header for signed-in users and works with the keyboard', function () {
|
||||
$this->actingAs(User::factory()->create());
|
||||
|
||||
visit(route('customers.index'))->inLightMode()
|
||||
->assertPresent('header '.THEME_TOGGLE)
|
||||
->keys(THEME_TOGGLE, 'Enter')
|
||||
->assertScript('document.documentElement.classList.contains("dark")', true)
|
||||
->assertScript('localStorage.getItem("dark-theme")', 'dark');
|
||||
});
|
||||
|
||||
@@ -116,5 +116,5 @@ it('logs in with the keyboard alone', function () {
|
||||
->type('[wire\\:model="password"]', 'password')
|
||||
->keys('[wire\\:model="password"]', 'Enter')
|
||||
->assertPathIs('/')
|
||||
->assertSee('Kirjaudu ulos');
|
||||
->assertPresent('[data-profile-menu]');
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Tests\Fakes;
|
||||
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;
|
||||
|
||||
class InMemoryUserAccountRepository implements UserAccountRepository
|
||||
{
|
||||
@@ -13,8 +14,18 @@ class InMemoryUserAccountRepository implements UserAccountRepository
|
||||
*/
|
||||
public array $users = [];
|
||||
|
||||
/**
|
||||
* @var array<int, Locale>
|
||||
*/
|
||||
public array $locales = [];
|
||||
|
||||
private int $nextId = 1;
|
||||
|
||||
public function changeLocale(int $userId, Locale $locale): void
|
||||
{
|
||||
$this->locales[$userId] = $locale;
|
||||
}
|
||||
|
||||
public function findById(int $id): ?UserAccount
|
||||
{
|
||||
return isset($this->users[$id]) ? clone $this->users[$id] : null;
|
||||
|
||||
@@ -17,8 +17,8 @@ it('renders the home page with the main menu', function () {
|
||||
->assertSee('Siirry pääsisältöön')
|
||||
->assertSee('id="main-content"', false)
|
||||
->assertSee('aria-label="Päävalikko"', false)
|
||||
->assertSee('Kirjautuneena: Eero Esimerkki')
|
||||
->assertSee('aria-label="Tumma teema"', false);
|
||||
->assertSee('aria-label="Profiili: Eero Esimerkki"', false)
|
||||
->assertSee('Kirjaudu ulos');
|
||||
});
|
||||
|
||||
it('increments the click count', function () {
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Users\Domain\Enums\Locale;
|
||||
use Illuminate\Auth\Notifications\ResetPassword;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
it('stores the language a signed-in user chooses and shows the pages in it', function () {
|
||||
$user = User::factory()->create(['name' => 'Eero Esimerkki']);
|
||||
|
||||
$this->actingAs($user)
|
||||
->from(route('home'))
|
||||
->post(route('locale.update'), ['locale' => 'en'])
|
||||
->assertRedirect(route('home'));
|
||||
|
||||
expect($user->fresh()->locale)->toBe(Locale::English);
|
||||
|
||||
// A new request loads the user again from the database.
|
||||
$this->actingAs($user->fresh())
|
||||
->get(route('home'))
|
||||
->assertOk()
|
||||
->assertSee('lang="en"', false)
|
||||
->assertSee('Sign out');
|
||||
});
|
||||
|
||||
it('keeps the language chosen on the signed-out pages in the session', function () {
|
||||
$this->from(route('login'))
|
||||
->post(route('locale.update'), ['locale' => 'en'])
|
||||
->assertRedirect(route('login'))
|
||||
->assertSessionHas('locale', 'en');
|
||||
|
||||
$this->get(route('login'))
|
||||
->assertOk()
|
||||
->assertSee('lang="en"', false);
|
||||
});
|
||||
|
||||
it('uses the account language over the one in the session', function () {
|
||||
$user = User::factory()->create(['locale' => 'fi']);
|
||||
|
||||
$this->actingAs($user)
|
||||
->withSession(['locale' => 'en'])
|
||||
->get(route('home'))
|
||||
->assertSee('lang="fi"', false)
|
||||
->assertSee('Kirjaudu ulos');
|
||||
});
|
||||
|
||||
it('keeps the language on the login page after logging out', function () {
|
||||
$user = User::factory()->create(['locale' => 'en']);
|
||||
|
||||
$this->actingAs($user)->post(route('logout'))->assertSessionHas('locale', 'en');
|
||||
});
|
||||
|
||||
it('rejects an unknown language', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->post(route('locale.update'), ['locale' => 'sv'])
|
||||
->assertSessionHasErrors('locale');
|
||||
|
||||
expect($user->fresh()->locale)->toBe(Locale::Finnish);
|
||||
});
|
||||
|
||||
it('sends the set-password email in the language of the user', function () {
|
||||
Notification::fake();
|
||||
$user = User::factory()->create(['locale' => 'en']);
|
||||
|
||||
$user->notify(new ResetPassword('token'));
|
||||
|
||||
Notification::assertSentTo($user, ResetPassword::class, function (ResetPassword $notification, array $channels, User $notifiable, ?string $locale) {
|
||||
return $locale === 'en';
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Users\Application\UseCases\ChangePreferredLocale;
|
||||
use App\Modules\Users\Domain\Enums\Locale;
|
||||
use Tests\Fakes\InMemoryUserAccountRepository;
|
||||
|
||||
it('stores the chosen language on the account', function () {
|
||||
$users = new InMemoryUserAccountRepository;
|
||||
|
||||
(new ChangePreferredLocale($users))->execute(7, Locale::English);
|
||||
|
||||
expect($users->locales)->toBe([7 => Locale::English]);
|
||||
});
|
||||
|
||||
it('names each language in the language itself', function () {
|
||||
expect(Locale::Finnish->nativeName())->toBe('Suomi')
|
||||
->and(Locale::English->nativeName())->toBe('English');
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Every user interface text exists in every language, with the same placeholders,
|
||||
* so a new Finnish text cannot be left without its English counterpart.
|
||||
*/
|
||||
function translationLines(string $locale, string $file): array
|
||||
{
|
||||
$lines = require dirname(__DIR__, 2)."/lang/{$locale}/{$file}";
|
||||
$flat = [];
|
||||
$flatten = function (array $values, string $prefix) use (&$flatten, &$flat): void {
|
||||
foreach ($values as $key => $value) {
|
||||
is_array($value) ? $flatten($value, $prefix.$key.'.') : $flat[$prefix.$key] = (string) $value;
|
||||
}
|
||||
};
|
||||
$flatten($lines, '');
|
||||
|
||||
return $flat;
|
||||
}
|
||||
|
||||
function placeholdersOf(string $text): array
|
||||
{
|
||||
// A placeholder starts a word: "ALV:n" (Finnish case ending) is not one.
|
||||
preg_match_all('/(?<![\p{L}\d]):([a-z_]+)/u', $text, $matches);
|
||||
$names = array_unique($matches[1]);
|
||||
sort($names);
|
||||
|
||||
return $names;
|
||||
}
|
||||
|
||||
dataset('translation files', fn () => array_map('basename', glob(dirname(__DIR__, 2).'/lang/fi/*.php')));
|
||||
|
||||
it('has every Finnish text in English with the same placeholders', function (string $file) {
|
||||
expect(file_exists(dirname(__DIR__, 2)."/lang/en/{$file}"))->toBeTrue("lang/en/{$file} is missing");
|
||||
|
||||
$finnish = translationLines('fi', $file);
|
||||
$english = translationLines('en', $file);
|
||||
|
||||
expect(array_keys($english))->toEqualCanonicalizing(array_keys($finnish));
|
||||
|
||||
foreach ($finnish as $key => $text) {
|
||||
expect(placeholdersOf($english[$key]))->toBe(placeholdersOf($text), "Placeholders differ in {$file}: {$key}");
|
||||
}
|
||||
})->with('translation files');
|
||||
Reference in New Issue
Block a user