@@ -5,6 +5,18 @@ import { locale as appLocale, isRTL } from '@sveltia/i18n';
55 * @import { LocaleCode } from '$lib/types/public';
66 */
77
8+ /**
9+ * Default options for `Intl.DisplayNames()`. Use `English (US)` instead of `American English` for a
10+ * better language listing.
11+ * @type {Intl.DisplayNamesOptions }
12+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/DisplayNames
13+ */
14+ const LANG_FORMATTER_OPTIONS = {
15+ type : 'language' ,
16+ languageDisplay : 'standard' ,
17+ style : 'short' ,
18+ } ;
19+
820/**
921 * Get the canonical locale of the given locale that can be used for various `Intl` methods.
1022 * @param {InternalLocaleCode } locale Locale.
@@ -46,12 +58,16 @@ const displayNamesCache = new Map();
4658 * @param {object } [options] Options.
4759 * @param {InternalLocaleCode } [options.displayLocale] Locale code to display the locale name. If
4860 * not given, use the current application locale. Default is `en`.
61+ * @param {Intl.DisplayNamesOptions } [options.formatterOptions] Options for `Intl.DisplayNames()`.
4962 * @returns {string | undefined } Locale label like `English`. If the locale is not valid, returns
5063 * `undefined`.
5164 */
5265export const getLocaleLabel = (
5366 locale ,
54- { displayLocale = getCanonicalLocale ( appLocale . current ?? 'en' ) } = { } ,
67+ {
68+ displayLocale = getCanonicalLocale ( appLocale . current ?? 'en' ) ,
69+ formatterOptions = LANG_FORMATTER_OPTIONS ,
70+ } = { } ,
5571) => {
5672 const canonicalLocale = getCanonicalLocale ( locale ) ;
5773
@@ -65,11 +81,11 @@ export const getLocaleLabel = (
6581 formatter = displayNamesCache . get ( displayLocale ) ;
6682
6783 if ( ! formatter ) {
68- formatter = new Intl . DisplayNames ( displayLocale , { type : 'language' } ) ;
84+ formatter = new Intl . DisplayNames ( displayLocale , formatterOptions ) ;
6985 displayNamesCache . set ( displayLocale , formatter ) ;
7086 }
7187 } else {
72- formatter = new Intl . DisplayNames ( undefined , { type : 'language' } ) ;
88+ formatter = new Intl . DisplayNames ( undefined , formatterOptions ) ;
7389 }
7490
7591 try {
0 commit comments