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,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