Translation followups (#9980)

This commit is contained in:
Samuel Newman
2026-03-02 19:01:09 +00:00
committed by GitHub
parent 5ee667f307
commit f614bf650d
5 changed files with 130 additions and 623 deletions
+31 -16
View File
@@ -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
View File
File diff suppressed because it is too large Load Diff