Intl.LocaleMatcher ponyfill providing comprehensive locale matching algorithms with support for 'lookup' and 'best fit' strategies
94
Build a utility that determines the best locale to use for a user based on their browser preferences and the locales available in your application.
Your implementation should provide a function resolveUserLocale(requestedLocales, availableLocales, options) that:
defaultLocale: The fallback locale to use when no match is found (default: 'en-US')algorithm: The matching strategy to use - either 'lookup' or 'best fit' (default: 'best fit')The function should handle edge cases appropriately:
['fr-CA', 'fr', 'en'], available locales ['en-US', 'fr-FR', 'es-ES'], and default 'en-US' using 'best fit' algorithm, it returns 'fr-FR' @test['de-CH'], available locales ['de-DE', 'en-US'], and default 'en-US' using 'lookup' algorithm, it returns 'en-US' (no match found with strict lookup) @test['zh-TW'], available locales ['zh-CN', 'zh-Hant', 'en'], and default 'en' using 'best fit' algorithm, it returns 'zh-Hant' (matches traditional Chinese variant) @test[] (empty), available locales ['en-US', 'fr-FR'], and default 'fr-FR', it returns 'fr-FR' (default locale) @test@generates
/**
* Resolves the best locale for a user based on their preferences and available locales.
*
* @param {string[]} requestedLocales - Array of locale strings requested by the user (e.g., from browser settings)
* @param {string[]} availableLocales - Array of locale strings that the application supports
* @param {Object} options - Configuration options
* @param {string} options.defaultLocale - The fallback locale to use when no match is found (default: 'en-US')
* @param {string} options.algorithm - The matching algorithm: 'lookup' or 'best fit' (default: 'best fit')
* @returns {string} The best matching locale string
*/
function resolveUserLocale(requestedLocales, availableLocales, options = {}) {
// IMPLEMENTATION HERE
}
module.exports = { resolveUserLocale };Provides locale matching functionality with support for different matching algorithms.
@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