MCPcopy Index your code
hub / github.com/formatjs/formatjs / ResolveLocale

Function ResolveLocale

packages/intl-localematcher/abstract/ResolveLocale.ts:21–136  ·  view source on GitHub ↗
(
  availableLocales: Set<string> | readonly string[],
  requestedLocales: readonly string[],
  options: {
    localeMatcher: string
    [k: string]: string
  },
  relevantExtensionKeys: K[],
  localeData: Record<string, D | undefined>,
  getDefaultLocale: () => string
)

Source from the content-addressed store, hash-verified

19 * https://tc39.es/ecma402/#sec-resolvelocale
20 */
21export function ResolveLocale<K extends string, D extends {[k in K]: any}>(
22 availableLocales: Set<string> | readonly string[],
23 requestedLocales: readonly string[],
24 options: {
25 localeMatcher: string
26 [k: string]: string
27 },
28 relevantExtensionKeys: K[],
29 localeData: Record<string, D | undefined>,
30 getDefaultLocale: () => string
31): ResolveLocaleResult {
32 const matcher = options.localeMatcher
33 let r: LookupMatcherResult
34 if (matcher === 'lookup') {
35 r = LookupMatcher(
36 Array.from(availableLocales),
37 requestedLocales,
38 getDefaultLocale
39 )
40 } else {
41 r = BestFitMatcher(
42 Array.from(availableLocales),
43 requestedLocales,
44 getDefaultLocale
45 )
46 }
47 if (r == null) {
48 r = {
49 locale: getDefaultLocale(),
50 extension: '',
51 }
52 }
53 let foundLocale = r.locale
54 let foundLocaleData = localeData[foundLocale]
55 // TODO: We can't really guarantee that the locale data is available
56 // invariant(
57 // foundLocaleData !== undefined,
58 // `Missing locale data for ${foundLocale}`
59 // )
60 const result: ResolveLocaleResult = {locale: 'en', dataLocale: foundLocale}
61 let components
62 let keywords: Keyword[]
63 if (r.extension) {
64 components = UnicodeExtensionComponents(r.extension)
65 keywords = components.keywords
66 } else {
67 keywords = []
68 }
69 let supportedKeywords: Keyword[] = []
70 for (const key of relevantExtensionKeys) {
71 // TODO: Shouldn't default to empty array, see TODO above
72 let keyLocaleData: string[] = foundLocaleData?.[key] ?? []
73 invariant(
74 Array.isArray(keyLocaleData),
75 `keyLocaleData for ${key} must be an array`
76 )
77 let value = keyLocaleData[0]
78 invariant(

Callers 11

core.tsFile · 0.90
constructorMethod · 0.90
InitializeDateTimeFormatFunction · 0.90
InitializePluralRulesFunction · 0.90
InitializeNumberFormatFunction · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
matchFunction · 0.85

Calls 6

LookupMatcherFunction · 0.85
BestFitMatcherFunction · 0.85
CanonicalizeUValueFunction · 0.85
invariantFunction · 0.70

Tested by

no test coverage detected