Skip to content

Commit

Permalink
Explicitly ignore RangeError when translating locale names
Browse files Browse the repository at this point in the history
  • Loading branch information
Signez committed Dec 26, 2024
1 parent 091c4ff commit 145a91b
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/locale/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,23 @@ function getLocalizedLanguage(
langCode: string,
appLang: string,
): string | undefined {
const allNames = new Intl.DisplayNames([appLang], {
type: 'language',
fallback: 'none',
languageDisplay: 'standard',
})
const translatedName = allNames.of(langCode)

if (translatedName) {
// force simple title case (as languages do not always start with an uppercase in Unicode data)
return translatedName[0].toLocaleUpperCase() + translatedName.slice(1)
try {
const allNames = new Intl.DisplayNames([appLang], {
type: 'language',
fallback: 'none',
languageDisplay: 'standard',
})
const translatedName = allNames.of(langCode)

if (translatedName) {
// force simple title case (as languages do not always start with an uppercase in Unicode data)
return translatedName[0].toLocaleUpperCase() + translatedName.slice(1)
}
} catch (e) {
// ignore RangeError from Intl.DisplayNames APIs
if (!(e instanceof RangeError)) {
throw e
}
}
}

Expand Down

0 comments on commit 145a91b

Please sign in to comment.