Intl.LocaleMatcher ponyfill providing comprehensive locale matching algorithms with support for 'lookup' and 'best fit' strategies
94
A global web application needs to deliver region-appropriate content to users. The service receives user locale preferences and must match them against available regional content variations, using intelligent region-aware matching to provide the best user experience.
Build a content localization service that:
The service should handle various scenarios:
When a user prefers ['zh-HK'] and available locales are ['zh-Hans', 'zh-Hant', 'en'], the service returns 'zh-Hant' because Hong Kong uses Traditional Chinese @test
When a user prefers ['zh-CN'] and available locales are ['zh-Hans', 'zh-Hant', 'en'], the service returns 'zh-Hans' because Mainland China uses Simplified Chinese @test
When a user prefers ['en-GB'] and available locales are ['en-US', 'en', 'de'], the service returns a reasonable English match rather than defaulting to German @test
When a user prefers ['es-MX'] and available locales are ['es', 'es-ES', 'en'], the service returns an appropriate Spanish variant @test
@generates
/**
* Matches user locale preferences against available content locales
* using region-aware matching algorithms
*
* @param userLocales - Array of user-preferred locale codes in priority order
* @param availableLocales - Array of locale codes for which content is available
* @param defaultLocale - Default locale to use when no match is found
* @returns The best matching locale code from availableLocales
*/
export function matchContentLocale(
userLocales: string[],
availableLocales: string[],
defaultLocale: string
): string;Provides sophisticated locale matching with region-aware algorithms.
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