Files
Toiminnanohjaus/tests/Unit/TranslationFilesTest.php
T
petriandClaude Opus 5.5 8aead06972 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]>
2026-11-25 06:28:39 +02:00

45 lines
1.5 KiB
PHP

<?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');