Translation followups (#9980)
This commit is contained in:
+31
-16
@@ -32,21 +32,27 @@ export function code3ToCode2Strict(lang: string): string | undefined {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const displayNamesCache = new Map<string, Intl.DisplayNames>()
|
||||
|
||||
function getDisplayNames(appLang: string): Intl.DisplayNames {
|
||||
let cached = displayNamesCache.get(appLang)
|
||||
if (!cached) {
|
||||
cached = new Intl.DisplayNames([appLang], {
|
||||
type: 'language',
|
||||
fallback: 'none',
|
||||
languageDisplay: 'standard',
|
||||
})
|
||||
displayNamesCache.set(appLang, cached)
|
||||
}
|
||||
return cached
|
||||
}
|
||||
|
||||
function getLocalizedLanguage(
|
||||
langCode: string,
|
||||
appLang: string,
|
||||
): string | undefined {
|
||||
try {
|
||||
const allNames = new Intl.DisplayNames([appLang], {
|
||||
type: 'language',
|
||||
fallback: 'none',
|
||||
languageDisplay: 'standard',
|
||||
})
|
||||
const translatedName = allNames.of(langCode)
|
||||
|
||||
if (translatedName) {
|
||||
return translatedName
|
||||
}
|
||||
return getDisplayNames(appLang).of(langCode) || undefined
|
||||
} catch (e) {
|
||||
// ignore RangeError from Intl.DisplayNames APIs
|
||||
if (!(e instanceof RangeError)) {
|
||||
@@ -308,17 +314,26 @@ export function regionName(countryCode: string, appLang: string): string {
|
||||
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(
|
||||
countryCode: string,
|
||||
appLang: string,
|
||||
): string | undefined {
|
||||
try {
|
||||
const allNames = new Intl.DisplayNames([appLang], {
|
||||
type: 'region',
|
||||
fallback: 'none',
|
||||
})
|
||||
|
||||
return allNames.of(countryCode)
|
||||
return getRegionNames(appLang).of(countryCode)
|
||||
} catch (err) {
|
||||
console.warn('Error getting localized region name:', err)
|
||||
return undefined
|
||||
|
||||
+66
-572
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user