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
getScriptSubtag
ResolveLocaleUtils.getScriptSubtag(locale: string?) → string?
Returns the script subtag, or nil when the locale does not carry one. A script subtag is
the four-letter subtag directly after the language (zh-Hant, zh-Hant-TW, sr-Latn);
a two-letter or three-digit subtag in that position is a region, not a script.
ResolveLocaleUtils.getScriptSubtag("zh-Hant-TW") --> "hant"
ResolveLocaleUtils.getScriptSubtag("zh-TW") --> nil
isTraditionalChinese
ResolveLocaleUtils.isTraditionalChinese(locale: string?) → boolean
Whether a Chinese locale is Traditional. The hant script subtag, and the tw / hk /
mo regions, are Traditional; everything else (hans, cn, sg, bare zh) is
Simplified. Only meaningful for zh locales.
isCompatibleLocale
ResolveLocaleUtils.isCompatibleLocale(locale: string?,otherLocale: string?) → booleanWhether two locales are close enough that one can be read in place of the other: the same language, written in the same script.
Regional variants of a language substitute for one another, so es-mx reads es-es and
en-gb reads en-us. Scripts do not: Traditional and Simplified Chinese are not mutually
readable, so zh-tw never reads zh-cn, whether the script is spelled out (zh-Hant vs
zh-Hans) or implied by region (zh-tw vs zh-cn). The same holds for any language
whose locales carry differing script subtags, such as sr-Latn and sr-Cyrl.
ResolveLocaleUtils.isCompatibleLocale("es-mx", "es-es") --> true
ResolveLocaleUtils.isCompatibleLocale("zh-tw", "zh-cn") --> false
ResolveLocaleUtils.isCompatibleLocale("pt-br", "en-us") --> false
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.