Better locale matching

This commit is contained in:
Lim Chee Aun 2024-08-23 18:00:04 +08:00
parent e5f674cf38
commit 482d8c3f2e
2 changed files with 9 additions and 8 deletions

View file

@ -16,7 +16,8 @@ function isValidDate(value) {
const resolvedLocale = new Intl.DateTimeFormat().resolvedOptions().locale;
const DTF = mem((locale, opts = {}) => {
const lang = localeMatch([locale], [resolvedLocale], locale);
const regionlessLocale = locale.replace(/-[a-z]+$/i, '');
const lang = localeMatch([regionlessLocale], [resolvedLocale], locale);
try {
return new Intl.DateTimeFormat(lang, opts);
} catch (e) {}

View file

@ -7,10 +7,8 @@ const defaultLocale = new Intl.DateTimeFormat().resolvedOptions().locale;
const _DateTimeFormat = (opts) => {
const { locale, dateYear, hideTime, formatOpts } = opts || {};
const loc =
locale && !/pseudo/i.test(locale)
? localeMatch([locale], [defaultLocale], locale)
: defaultLocale;
const regionlessLocale = locale.replace(/-[a-z]+$/i, '');
const loc = localeMatch([regionlessLocale], [defaultLocale], locale);
const currentYear = new Date().getFullYear();
const options = {
// Show year if not current year
@ -24,9 +22,11 @@ const _DateTimeFormat = (opts) => {
};
try {
return Intl.DateTimeFormat(loc, options);
} catch (e) {
return Intl.DateTimeFormat(undefined, options);
}
} catch (e) {}
try {
return Intl.DateTimeFormat(locale, options);
} catch (e) {}
return Intl.DateTimeFormat(undefined, options);
};
const DateTimeFormat = mem(_DateTimeFormat);