mirror of
https://github.com/cheeaun/phanpy.git
synced 2025-01-22 16:46:28 +01:00
Better locale matching
This commit is contained in:
parent
e5f674cf38
commit
482d8c3f2e
2 changed files with 9 additions and 8 deletions
|
@ -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) {}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in a new issue