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]>
This commit is contained in:
@@ -2,17 +2,15 @@
|
||||
|
||||
use App\Modules\Users\Application\UseCases\ChangePreferredLocale;
|
||||
use App\Modules\Users\Domain\Enums\Locale;
|
||||
use Tests\Fakes\FakeTransactionManager;
|
||||
use Tests\Fakes\InMemoryUserAccountRepository;
|
||||
|
||||
it('stores the chosen language on the account', function () {
|
||||
it('stores the chosen language on the account in a transaction', function () {
|
||||
$users = new InMemoryUserAccountRepository;
|
||||
$transactions = new FakeTransactionManager;
|
||||
|
||||
(new ChangePreferredLocale($users))->execute(7, Locale::English);
|
||||
(new ChangePreferredLocale($users, $transactions))->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');
|
||||
expect($users->locales)->toBe([7 => Locale::English])
|
||||
->and($transactions->transactionCount)->toBe(1);
|
||||
});
|
||||
|
||||
@@ -4,9 +4,17 @@
|
||||
* 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 langPath(string $path = ''): string
|
||||
{
|
||||
return dirname(__DIR__, 2).'/lang'.($path === '' ? '' : '/'.$path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
function translationLines(string $locale, string $file): array
|
||||
{
|
||||
$lines = require dirname(__DIR__, 2)."/lang/{$locale}/{$file}";
|
||||
$lines = require langPath("{$locale}/{$file}");
|
||||
$flat = [];
|
||||
$flatten = function (array $values, string $prefix) use (&$flatten, &$flat): void {
|
||||
foreach ($values as $key => $value) {
|
||||
@@ -18,27 +26,50 @@ function translationLines(string $locale, string $file): array
|
||||
return $flat;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
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]);
|
||||
$names = array_values(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);
|
||||
|
||||
/**
|
||||
* @param array<string, string> $finnish
|
||||
* @param array<string, string> $english
|
||||
*/
|
||||
function expectSameTexts(array $finnish, array $english, string $file): void
|
||||
{
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
dataset('translation files', fn () => array_map('basename', glob(langPath('fi/*.php'))));
|
||||
|
||||
it('has every Finnish text in English with the same placeholders', function (string $file) {
|
||||
expect(file_exists(langPath("en/{$file}")))->toBeTrue("lang/en/{$file} is missing");
|
||||
|
||||
expectSameTexts(translationLines('fi', $file), translationLines('en', $file), $file);
|
||||
})->with('translation files');
|
||||
|
||||
it('has no English translation file without a Finnish one', function () {
|
||||
$finnish = array_map('basename', glob(langPath('fi/*.php')));
|
||||
$english = array_map('basename', glob(langPath('en/*.php')));
|
||||
|
||||
expect($english)->toEqualCanonicalizing($finnish);
|
||||
});
|
||||
|
||||
it('has the same JSON texts in Finnish and English', function () {
|
||||
$finnish = json_decode((string) file_get_contents(langPath('fi.json')), true, flags: JSON_THROW_ON_ERROR);
|
||||
$english = json_decode((string) file_get_contents(langPath('en.json')), true, flags: JSON_THROW_ON_ERROR);
|
||||
|
||||
expectSameTexts($finnish, $english, 'fi.json / en.json');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user