add region name cache
This commit is contained in:
+15
-6
@@ -314,17 +314,26 @@ export function regionName(countryCode: string, appLang: string): string {
|
|||||||
return countryCode
|
return countryCode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const regionNamesCache = new Map<string, Intl.DisplayNames>()
|
||||||
|
|
||||||
|
function getRegionNames(appLang: string): Intl.DisplayNames {
|
||||||
|
let cached = regionNamesCache.get(appLang)
|
||||||
|
if (!cached) {
|
||||||
|
cached = new Intl.DisplayNames([appLang], {
|
||||||
|
type: 'region',
|
||||||
|
fallback: 'none',
|
||||||
|
})
|
||||||
|
regionNamesCache.set(appLang, cached)
|
||||||
|
}
|
||||||
|
return cached
|
||||||
|
}
|
||||||
|
|
||||||
function getLocalizedRegionName(
|
function getLocalizedRegionName(
|
||||||
countryCode: string,
|
countryCode: string,
|
||||||
appLang: string,
|
appLang: string,
|
||||||
): string | undefined {
|
): string | undefined {
|
||||||
try {
|
try {
|
||||||
const allNames = new Intl.DisplayNames([appLang], {
|
return getRegionNames(appLang).of(countryCode)
|
||||||
type: 'region',
|
|
||||||
fallback: 'none',
|
|
||||||
})
|
|
||||||
|
|
||||||
return allNames.of(countryCode)
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn('Error getting localized region name:', err)
|
console.warn('Error getting localized region name:', err)
|
||||||
return undefined
|
return undefined
|
||||||
|
|||||||
Reference in New Issue
Block a user