From 376d1d9b5f8d3674d532034c00512e6d8d9dd079 Mon Sep 17 00:00:00 2001 From: PathGao Date: Mon, 3 Aug 2026 20:10:04 +0800 Subject: [PATCH] refactor(settings): derive the language codes and TOC bounds from one source MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two constants existed twice. Neither duplicate was type-checked against its twin, and one had already drifted. The language catalogue. `settings.svelte.ts` carried a 26-entry `{ code, name, nativeName }` table beside `getSupportedLanguages()` in `i18n.ts`. Only the `code` column was ever read — `SUPPORTED_LANGUAGE_CODES` mapped it for `isSupportedLanguage`, and nothing else in `src/` or `scripts/` touched the table — so the 52 display strings were dead data with nothing to compare them against. They drifted: `pt` read "Portuguese" in the catalogue the language ` with no dictionary renders as raw English, and a + // language the tag resolver can produce but the validator rejects makes + // detectSystemLanguage's result unstorable. + for (const code of offered) { + assert.ok(translations[code], `${code} is offered by the language renders and "Portuguese (European)" in the dead one. A drift no user can see is a drift no bug report can find.', + // Pins `nativeName`, the column a catalogue exists to carry and the one + // the ` renders. + * + * This module used to carry its own `{ code, name, nativeName }` table beside + * `getSupportedLanguages()`. Only the `code` column was ever read, so the two + * copies of the display columns drifted unnoticed: `pt` was "Portuguese" in + * the catalogue the dialog renders and "Portuguese (European)" in the copy + * here, and no user ever saw the second spelling. Deriving from the catalogue + * means a language can only be added, removed or renamed in one place. + */ +const SUPPORTED_LANGUAGE_CODES: readonly LanguageCode[] = getSupportedLanguages().map((entry) => entry.code); + +export function isSupportedLanguage(value: unknown): value is LanguageCode { return typeof value === 'string' && (SUPPORTED_LANGUAGE_CODES as readonly string[]).includes(value); }