11import moment from 'moment' ;
22
3+ // Static imports register the locale with the same moment instance the app uses.
4+ // Dynamic imports (`import('moment/locale/${x}.js')`) were unreliable: Vite couldn't
5+ // always resolve the variable path at build time, the locale wouldn't end up in the
6+ // bundle, and `moment.locale()` would silently fall back to 'en'.
7+ //
8+ // Each locale file is ~10KB minified; the full set adds ~200KB once. That's the cost
9+ // of getting localized DateTimePicker formats reliably across all 22 supported UI locales.
10+ import 'moment/locale/cs' ;
11+ import 'moment/locale/da' ;
12+ import 'moment/locale/de' ;
13+ import 'moment/locale/el' ;
14+ import 'moment/locale/es' ;
15+ import 'moment/locale/fr' ;
16+ import 'moment/locale/hi' ;
17+ import 'moment/locale/hr' ;
18+ import 'moment/locale/hu' ;
19+ import 'moment/locale/it' ;
20+ import 'moment/locale/kk' ;
21+ import 'moment/locale/lv' ;
22+ import 'moment/locale/nb' ;
23+ import 'moment/locale/pl' ;
24+ import 'moment/locale/pt' ;
25+ import 'moment/locale/ro' ;
26+ import 'moment/locale/ru' ;
27+ import 'moment/locale/tr' ;
28+ import 'moment/locale/uk' ;
29+ import 'moment/locale/zh-cn' ;
30+ import 'moment/locale/zh-tw' ;
31+
332// Our translation file codes don't always match moment locale codes.
433// Moment uses IETF tags (zh-cn, zh-tw, da); our localeList uses shorter custom codes.
534const MOMENT_LOCALE_MAP : Record < string , string > = {
@@ -14,26 +43,13 @@ export const toMomentLocale = (fileCode: string | undefined): string => {
1443 return MOMENT_LOCALE_MAP [ fileCode ] ?? fileCode ;
1544} ;
1645
17- // Dynamically loads the moment locale bundle and sets it as active.
18- // No-op for 'en' (always preloaded in moment). Silently falls back to 'en'
19- // if the locale bundle isn't available.
46+ // Sets the active moment locale. All bundles are statically imported above, so this
47+ // always succeeds for any supported locale and there's no async work.
2048export const ensureMomentLocale = async ( fileCode : string | undefined ) : Promise < string > => {
2149 const target = toMomentLocale ( fileCode ) ;
2250
23- if ( target === 'en' ) {
24- moment . locale ( 'en' ) ;
25-
26- return 'en' ;
27- }
28- try {
29- // @vite -ignore
30- await import ( `moment/locale/${ target } .js` ) ;
31- moment . locale ( target ) ;
32-
33- return target ;
34- } catch {
35- moment . locale ( 'en' ) ;
51+ moment . locale ( target ) ;
3652
37- return 'en' ;
38- }
53+ // moment.locale() returns the active locale name; falls through to 'en' if unknown.
54+ return moment . locale ( ) ;
3955} ;
0 commit comments