Intl.LocaleMatcher ponyfill providing comprehensive locale matching algorithms with support for 'lookup' and 'best fit' strategies
94
A utility that manages locale preferences for a multi-language web application, providing both strict hierarchical locale matching and flexible linguistically-aware matching based on different use cases.
Implements strict hierarchical locale matching that follows exact fallback chains without cross-locale matching.
['en-US', 'fr-CA'], available locales ['en', 'en-GB', 'fr', 'de'], and default locale 'en', the strict matcher returns 'en' (falls back hierarchically from en-US to en, ignoring en-GB). @test['zh-CN'], available locales ['zh-TW', 'en'], and default locale 'en', the strict matcher returns 'en' (does not match zh-TW even though both are Chinese variants). @testImplements linguistically-aware locale matching that can match across related locale variants using language distance calculations.
['en-US', 'fr-CA'], available locales ['en-GB', 'fr', 'de'], and default locale 'de', the flexible matcher returns 'en-GB' (matches en-US to en-GB as linguistically similar). @test['zh-TW'], available locales ['zh-Hant', 'en'], and default locale 'en', the flexible matcher returns 'zh-Hant' (matches Traditional Chinese variants). @testResolves the best locale based on user preferences and application requirements, selecting the appropriate matching strategy.
requireExactFallback is true, uses strict matching algorithm. @testrequireExactFallback is false, uses flexible matching algorithm. @test@generates
/**
* Resolves the best locale match based on user preferences and available locales.
*
* @param requestedLocales - Array of locale strings requested by the user (in priority order)
* @param availableLocales - Array of locale strings available in the application
* @param defaultLocale - The fallback locale to use if no match is found
* @param requireExactFallback - If true, uses strict hierarchical matching; if false, uses flexible linguistic matching
* @returns The best matched locale string
*/
export function resolveLocale(
requestedLocales: string[],
availableLocales: string[],
defaultLocale: string,
requireExactFallback: boolean
): string;Provides locale matching algorithms with support for both 'lookup' and 'best fit' strategies.
@satisfied-by
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