Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ import type { c2mOptions } from "./types";
export const DEFAULT_LANGUAGE = "en";
export const AVAILABLE_LANGUAGES = Object.keys(translations);

/**
* Fallback locales for languages not supported by Intl.NumberFormat
*/
const FALLBACK_LOCALES: Record<string, string> = {
hmn: "en" // Hmong uses English numeric notation
};

/**
* Resolve locale to one supported by Intl.NumberFormat
* @param locale - the desired locale
* @returns a supported locale code
*/
function resolveLocale(locale: string): string {
return Intl.NumberFormat.supportedLocalesOf(locale).length
? locale
: (FALLBACK_LOCALES[locale] ?? DEFAULT_LANGUAGE);
}

/**
* Manages translations, including importing content, switching languages, and returning translated strings
*/
Expand Down Expand Up @@ -77,7 +95,7 @@ export class TranslationManager {
this._loadedLanguages.set(
code,
createIntl({
locale: code,
locale: resolveLocale(code),
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
messages: translations[code]
})
Expand Down