ResolveLocaleUtils
Shared locale-id parsing and resolution. Keeps the "given a locale like
en-us, what do we actually use?" logic in one place so number formatting,
dialog pacing, and any other locale-sensitive feature resolve identically.
Locale ids follow BCP-47-ish shapes: a language subtag, then optional script
and/or region subtags separated by - (or sometimes _), e.g. en, en-us,
pt-br, zh-Hant-TW. Matching is case-insensitive.
Functions
getLanguageSubtag
ResolveLocaleUtils.getLanguageSubtag(locale: string?) → string?Returns the lowercased primary language subtag, or nil when the input has no language subtag (nil, non-string, empty, or leading punctuation/digits).
ResolveLocaleUtils.getLanguageSubtag("en-us") --> "en"
ResolveLocaleUtils.getLanguageSubtag("zh_Hant_TW") --> "zh"
ResolveLocaleUtils.getLanguageSubtag("PT-BR") --> "pt"
ResolveLocaleUtils.getLanguageSubtag(nil) --> nil
isTraditionalChinese
ResolveLocaleUtils.isTraditionalChinese(locale: string?) → boolean
Whether a Chinese locale is Traditional. hant, and the tw / hk / mo
regions are Traditional; everything else (hans, cn, sg, bare zh) is
Simplified. Only meaningful for zh locales.
resolveClosestKey
ResolveLocaleUtils.resolveClosestKey(locale: string?,availableLocales: {[string]: T}) → string?Resolves a locale to the best-matching key present in availableLocales:
- Exact (case-insensitive) match.
- Chinese Traditional/Simplified routing to whichever variant keys exist.
-
Closest key sharing the language subtag (smallest key, so the pick is
deterministic), so e.g.
en-gbfalls back toen-usandes-mxtoes-esrather than to an unrelated default.
Returns nil when nothing shares the language subtag; callers apply their own default in that case.