Intl.LocaleMatcher ponyfill providing comprehensive locale matching algorithms with support for 'lookup' and 'best fit' strategies
94
Build a smart locale matching tool that finds the linguistically best matching locale from a set of available options.
Implement a tool that:
en-US is linguistically closer to en-GB than to fr-FR)en-US can match en-GB)en-US, available locales ['en-GB', 'fr-FR', 'de-DE'], and default en-GB, returns en-GB (closest linguistic match) @testzh-TW, available locales ['zh-CN', 'zh-HK', 'ja-JP'], and default zh-CN, returns zh-CN or zh-HK (linguistically related Chinese variants) @testen, available locales ['en-US', 'en-GB', 'en-AU'], and default en-US, returns one of the English variants @testfr-CA, available locales ['zh-CN', 'ja-JP', 'ko-KR'], and default en-US, returns en-US (no linguistically close match found) @test@generates
/**
* Finds the best matching locale from available options
*
* @param requestedLocale - The requested locale to match
* @param availableLocales - Array of available locale strings
* @param defaultLocale - Fallback locale when no suitable match is found
* @returns The best matching locale string
*/
export function matchLocale(
requestedLocale: string,
availableLocales: string[],
defaultLocale: string
): string;Provides locale matching with CLDR-based language distance calculation.
Install with Tessl CLI
npx tessl i tessl/npm-formatjs--intl-localematcherdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10