mirror of
https://github.com/Matysh/houseplan-card
synced 2026-10-07 15:09:30 +00:00
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user