CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-formatjs--intl-localematcher

Intl.LocaleMatcher ponyfill providing comprehensive locale matching algorithms with support for 'lookup' and 'best fit' strategies

94

1.09x
Overview
Eval results
Files

task.mdevals/scenario-10/

Locale Fallback Resolver

Build a locale fallback resolver that implements a hierarchical locale matching algorithm. Given a requested locale and a list of available locales, your resolver should find the best match by progressively removing subtags from right to left.

Requirements

Your resolver should:

  1. Accept a requested locale identifier (e.g., "en-Latn-US") and a list of available locale identifiers
  2. Check if the exact requested locale exists in the available locales
  3. If not found, progressively remove subtags from right to left to find a match
  4. Handle special cases where singleton subtags (single character tags) should be removed together with the following subtag
  5. Return the matched locale string if found, or undefined if no match exists

The fallback chain should work as follows:

  • "en-Latn-US" → "en-Latn" → "en"
  • "zh-Hans-CN-x-custom" → "zh-Hans-CN" → "zh-Hans" → "zh"
  • "fr-CA" → "fr"

Implementation

@generates

API

/**
 * Finds the best available locale using progressive subtag removal
 * @param requestedLocale - The locale identifier to match (e.g., "en-Latn-US")
 * @param availableLocales - Array of available locale identifiers
 * @returns The matched locale string or undefined if no match found
 */
export function findBestAvailableLocale(
  requestedLocale: string,
  availableLocales: string[]
): string | undefined;

Test Cases

  • When requested locale "en-US" and available locales ["en", "fr", "de"], returns "en" @test
  • When requested locale "en-Latn-US" and available locales ["en-Latn", "en", "fr"], returns "en-Latn" @test
  • When requested locale "zh-Hans-CN" and available locales ["zh-Hans", "ja", "ko"], returns "zh-Hans" @test
  • When requested locale "fr-CA" and available locales ["de", "es", "it"], returns undefined @test
  • When requested locale "en-US" is in available locales ["en-US", "en-GB", "en"], returns "en-US" (exact match) @test

Dependencies { .dependencies }

@formatjs/intl-localematcher { .dependency }

Provides locale matching and resolution algorithms.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/npm-formatjs--intl-localematcher

tile.json