fix: normalize i18n registry lookup keys

Issue: #62
User-Visible: no
This commit is contained in:
Matysh
2026-08-28 02:55:22 +03:00
parent d0d6cf57a7
commit c755a0efc4
5 changed files with 36 additions and 19 deletions
+9 -4
View File
@@ -23,10 +23,6 @@ export type Lang = (typeof LANGUAGE_REGISTRY)[number]['code'];
export const FALLBACK_LANGUAGE_CODE: Lang = 'en';
export const FALLBACK_DICTIONARY = en;
const LANGUAGE_BY_CODE = new Map<string, LanguageEntry>(
LANGUAGE_REGISTRY.map((entry) => [entry.code, entry]),
);
/** Normalize HA locale tags for registry lookup. */
export function normalizeLanguageTag(value: unknown): string {
return typeof value === 'string'
@@ -34,6 +30,15 @@ export function normalizeLanguageTag(value: unknown): string {
: '';
}
/** Build a case-insensitive BCP 47 lookup while retaining canonical codes. */
export function buildLanguageLookup<T extends { code: string }>(
entries: readonly T[],
): ReadonlyMap<string, T> {
return new Map(entries.map((entry) => [normalizeLanguageTag(entry.code), entry]));
}
const LANGUAGE_BY_CODE = buildLanguageLookup(LANGUAGE_REGISTRY);
/** Return the canonical registry entry for a locale code, if it is shipped. */
export function languageEntry(value: unknown): LanguageEntry | undefined {
return LANGUAGE_BY_CODE.get(normalizeLanguageTag(value));