mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Optimize loading of date-fns
This commit is contained in:
parent
88a01888bf
commit
b3febe49ad
4 changed files with 97 additions and 74 deletions
|
@ -2,8 +2,8 @@ import escapeHtml from 'escape-html';
|
||||||
import Events from '../utils/events.ts';
|
import Events from '../utils/events.ts';
|
||||||
import globalize from '../scripts/globalize';
|
import globalize from '../scripts/globalize';
|
||||||
import dom from '../scripts/dom';
|
import dom from '../scripts/dom';
|
||||||
import * as datefns from 'date-fns';
|
import { formatRelative } from 'date-fns';
|
||||||
import dfnshelper from '../scripts/dfnshelper';
|
import { getLocale } from '../scripts/dfnshelper';
|
||||||
import serverNotifications from '../scripts/serverNotifications';
|
import serverNotifications from '../scripts/serverNotifications';
|
||||||
import '../elements/emby-button/emby-button';
|
import '../elements/emby-button/emby-button';
|
||||||
import './listview/listview.scss';
|
import './listview/listview.scss';
|
||||||
|
@ -38,7 +38,7 @@ import { toBoolean } from '../utils/string.ts';
|
||||||
html += escapeHtml(entry.Name);
|
html += escapeHtml(entry.Name);
|
||||||
html += '</div>';
|
html += '</div>';
|
||||||
html += '<div class="listItemBodyText secondary">';
|
html += '<div class="listItemBodyText secondary">';
|
||||||
html += datefns.formatRelative(Date.parse(entry.Date), Date.parse(new Date()), { locale: dfnshelper.getLocale() });
|
html += formatRelative(Date.parse(entry.Date), Date.now(), { locale: getLocale() });
|
||||||
html += '</div>';
|
html += '</div>';
|
||||||
html += '<div class="listItemBodyText secondary listItemBodyText-nowrap">';
|
html += '<div class="listItemBodyText secondary listItemBodyText-nowrap">';
|
||||||
html += escapeHtml(entry.ShortOverview || '');
|
html += escapeHtml(entry.ShortOverview || '');
|
||||||
|
|
|
@ -1,81 +1,86 @@
|
||||||
import { af, arDZ, be, bg, bn, ca, cs, cy, da, de, el, enGB, enUS, eo, es, et, eu, faIR, fi, fr, frCA, gl, he, hi, hr, hu, id, is, it, ja, kk, ko, lt, lv, ms, nb,
|
import enUS from 'date-fns/locale/en-US';
|
||||||
nl, nn, pl, ptBR, pt, ro, ru, sk, sl, sv, ta, th, tr, uk, vi, zhCN, zhTW } from 'date-fns/locale';
|
|
||||||
import globalize from './globalize';
|
|
||||||
|
|
||||||
const dateLocales = (locale) => ({
|
const LOCALE_MAP = {
|
||||||
'af': af,
|
'af': 'af',
|
||||||
'ar': arDZ,
|
'ar': 'ar-DZ',
|
||||||
'be-by': be,
|
'be-by': 'be',
|
||||||
'bg-bg': bg,
|
'bg-bg': 'bg',
|
||||||
'bn': bn,
|
'bn': 'bn',
|
||||||
'ca': ca,
|
'ca': 'ca',
|
||||||
'cs': cs,
|
'cs': 'cs',
|
||||||
'cy': cy,
|
'cy': 'cy',
|
||||||
'da': da,
|
'da': 'da',
|
||||||
'de': de,
|
'de': 'de',
|
||||||
'el': el,
|
'el': 'el',
|
||||||
'en-gb': enGB,
|
'en-gb': 'en-GB',
|
||||||
'en-us': enUS,
|
'en-us': 'en-US',
|
||||||
'eo': eo,
|
'eo': 'eo',
|
||||||
'es': es,
|
'es': 'es',
|
||||||
'es-ar': es,
|
'es-ar': 'es',
|
||||||
'es-do': es,
|
'es-do': 'es',
|
||||||
'es-mx': es,
|
'es-mx': 'es',
|
||||||
'et': et,
|
'et': 'et',
|
||||||
'eu': eu,
|
'eu': 'eu',
|
||||||
'fa': faIR,
|
'fa': 'fa-IR',
|
||||||
'fi': fi,
|
'fi': 'fi',
|
||||||
'fr': fr,
|
'fr': 'fr',
|
||||||
'fr-ca': frCA,
|
'fr-ca': 'fr-CA',
|
||||||
'gl': gl,
|
'gl': 'gl',
|
||||||
'gsw': de,
|
'gsw': 'de',
|
||||||
'he': he,
|
'he': 'he',
|
||||||
'hi-in': hi,
|
'hi-in': 'hi',
|
||||||
'hr': hr,
|
'hr': 'hr',
|
||||||
'hu': hu,
|
'hu': 'hu',
|
||||||
'id': id,
|
'id': 'id',
|
||||||
'is': is,
|
'is': 'is',
|
||||||
'it': it,
|
'it': 'it',
|
||||||
'ja': ja,
|
'ja': 'ja',
|
||||||
'kk': kk,
|
'kk': 'kk',
|
||||||
'ko': ko,
|
'ko': 'ko',
|
||||||
'lt-lt': lt,
|
'lt-lt': 'lt',
|
||||||
'lv': lv,
|
'lv': 'lv',
|
||||||
'ms': ms,
|
'ms': 'ms',
|
||||||
'nb': nb,
|
'nb': 'nb',
|
||||||
'nl': nl,
|
'nl': 'nl',
|
||||||
'nn': nn,
|
'nn': 'nn',
|
||||||
'pl': pl,
|
'pl': 'pl',
|
||||||
'pt': pt,
|
'pt': 'pt',
|
||||||
'pt-br': ptBR,
|
'pt-br': 'pt-BR',
|
||||||
'pt-pt': pt,
|
'pt-pt': 'pt',
|
||||||
'ro': ro,
|
'ro': 'ro',
|
||||||
'ru': ru,
|
'ru': 'ru',
|
||||||
'sk': sk,
|
'sk': 'sk',
|
||||||
'sl-si': sl,
|
'sl-si': 'sl',
|
||||||
'sv': sv,
|
'sv': 'sv',
|
||||||
'ta': ta,
|
'ta': 'ta',
|
||||||
'th': th,
|
'th': 'th',
|
||||||
'tr': tr,
|
'tr': 'tr',
|
||||||
'uk': uk,
|
'uk': 'uk',
|
||||||
'vi': vi,
|
'vi': 'vi',
|
||||||
'zh-cn': zhCN,
|
'zh-cn': 'zh-CN',
|
||||||
'zh-hk': zhCN,
|
'zh-hk': 'zh-HK',
|
||||||
'zh-tw': zhTW
|
'zh-tw': 'zh-TW'
|
||||||
})[locale];
|
};
|
||||||
|
|
||||||
|
const DEFAULT_LOCALE = 'en-US';
|
||||||
|
|
||||||
|
let localeString = DEFAULT_LOCALE;
|
||||||
|
let locale = enUS;
|
||||||
|
|
||||||
|
export async function updateLocale(newLocale) {
|
||||||
|
console.debug('[dfnshelper] updating date-fns locale', newLocale);
|
||||||
|
localeString = LOCALE_MAP[newLocale] || LOCALE_MAP[newLocale.replace(/-.*/, '')] || DEFAULT_LOCALE;
|
||||||
|
console.debug('[dfnshelper] mapped to date-fns locale', localeString);
|
||||||
|
locale = await import(`date-fns/locale/${localeString}/index.js`);
|
||||||
|
}
|
||||||
|
|
||||||
export function getLocale() {
|
export function getLocale() {
|
||||||
return dateLocales(globalize.getCurrentLocale()) || dateLocales(globalize.getCurrentLocale().replace(/-.*/, '')) || enUS;
|
return locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getLocaleWithSuffix() {
|
export function getLocaleWithSuffix() {
|
||||||
return {
|
return {
|
||||||
addSuffix: true,
|
addSuffix: true,
|
||||||
locale: getLocale()
|
locale
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
|
||||||
getLocale: getLocale,
|
|
||||||
getLocaleWithSuffix
|
|
||||||
};
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ import isEmpty from 'lodash-es/isEmpty';
|
||||||
|
|
||||||
import { currentSettings as userSettings } from './settings/userSettings';
|
import { currentSettings as userSettings } from './settings/userSettings';
|
||||||
import Events from '../utils/events.ts';
|
import Events from '../utils/events.ts';
|
||||||
|
import { updateLocale } from './dfnshelper';
|
||||||
|
|
||||||
const Direction = {
|
const Direction = {
|
||||||
rtl: 'rtl',
|
rtl: 'rtl',
|
||||||
|
@ -100,6 +101,8 @@ const Direction = {
|
||||||
} else {
|
} else {
|
||||||
currentDateTimeCulture = currentCulture;
|
currentDateTimeCulture = currentCulture;
|
||||||
}
|
}
|
||||||
|
updateLocale(currentDateTimeCulture);
|
||||||
|
|
||||||
ensureTranslations(currentCulture);
|
ensureTranslations(currentCulture);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -113,6 +113,21 @@ const config = {
|
||||||
return `node_modules.${parts[0]}.${parts[1]}`;
|
return `node_modules.${parts[0]}.${parts[1]}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (packageName === 'date-fns') {
|
||||||
|
const parts = module.context
|
||||||
|
.substring(module.context.lastIndexOf(packageName))
|
||||||
|
.split(/[\\/]/);
|
||||||
|
|
||||||
|
let name = `node_modules.${parts[0]}`;
|
||||||
|
if (parts[1]) {
|
||||||
|
name += `.${parts[1]}`;
|
||||||
|
}
|
||||||
|
if (parts[1] === 'locale') {
|
||||||
|
name += `.${parts[2]}`;
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
return `node_modules.${packageName}`;
|
return `node_modules.${packageName}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue