From 35cb193c76a01b0bdea862b423cfc6721f42529a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Velad=20Galv=C3=A1n?= Date: Wed, 28 Jun 2023 14:11:05 +0200 Subject: [PATCH] feat(UI): Use Intl.DisplayNames to show the language name (#5365) --- ui/language_utils.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ui/language_utils.js b/ui/language_utils.js index 32948d334d..c909b60783 100644 --- a/ui/language_utils.js +++ b/ui/language_utils.js @@ -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".