Skip to content

Commit

Permalink
feat(UI): Use Intl.DisplayNames to show the language name (shaka-proj…
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad committed Jun 28, 2023
1 parent 3863c73 commit 35cb193
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ui/language_utils.js
Expand Up @@ -193,6 +193,21 @@ shaka.ui.LanguageUtils = class {
// Extract the base language from the locale as a fallback step.
const language = shaka.util.LanguageUtils.getBase(locale);

// If Intl.DisplayNames is supported we prefer it, because the list of
// languages is up to date.
if (window.Intl && 'DisplayNames' in Intl) {
try {
if (Intl.DisplayNames.supportedLocalesOf([locale]).length) {
const languageNames =
new Intl.DisplayNames([locale], {type: 'language'});
const language = languageNames.of(locale);
return language.charAt(0).toUpperCase() + language.slice(1);
}
} catch (e) {
// Ignore errors and try the fallback
}
}

// First try to resolve the full language name.
// If that fails, try the base.
// Finally, report "unknown".
Expand Down

0 comments on commit 35cb193

Please sign in to comment.