Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(locale): support eager dayjs locale modules (#15711)

Inneid3e8e7cb6513
diff --git a/src/layout/SPAGlobalProvider/Locale.tsx b/src/layout/SPAGlobalProvider/Locale.tsx
index d4aef9bcb4c..a0b001c0c10 100644
--- a/src/layout/SPAGlobalProvider/Locale.tsx
+++ b/src/layout/SPAGlobalProvider/Locale.tsx
@@ -1,16 +1,18 @@
import { ConfigProvider } from 'antd';
import dayjs from 'dayjs';
-import { memo, type PropsWithChildren, useEffect, useState } from 'react';
+import type { PropsWithChildren } from 'react';
+import { memo, useEffect, useState } from 'react';
import { isRtlLang } from 'rtl-detect';
import Editor from '@/layout/GlobalProvider/Editor';
import { createI18nNext } from '@/locales/create';
-import { normalizeDayjsLocale } from '@/utils/dayjsLocale';
+import type { DayjsLocaleGlobEntry } from '@/utils/dayjsLocale';
+import { loadDayjsLocaleModule, normalizeDayjsLocale } from '@/utils/dayjsLocale';
import { getAntdLocale } from '@/utils/locale';
const dayjsLocaleLoaders = import.meta.glob<{ default: ILocale }>(
'/node_modules/dayjs/esm/locale/{ar,bg,de,en,es,fa,fr,it,ja,ko,nl,pl,pt-br,ru,tr,vi,zh-cn,zh-tw}.js',
-);
+) as Record<string, DayjsLocaleGlobEntry>;
const updateDayjs = async (lang: string) => {
const locale = normalizeDayjsLocale(lang);
@@ -19,13 +21,15 @@ const updateDayjs = async (lang: string) => {
dayjsLocaleLoaders[key] ?? dayjsLocaleLoaders['/node_modules/dayjs/esm/locale/en.js'];
try {
- const mod = await loader();
+ const mod = await loadDayjsLocaleModule(loader);
dayjs.locale(mod.default);
} catch (error) {
console.error('error', error);
console.error(`dayjs locale for ${lang} not found, fallback to en`);
- const fallback = await dayjsLocaleLoaders['/node_modules/dayjs/esm/locale/en.js']!();
+ const fallback = await loadDayjsLocaleModule(
+ dayjsLocaleLoaders['/node_modules/dayjs/esm/locale/en.js']!,
+ );
dayjs.locale(fallback.default);
}
};
diff --git a/src/utils/dayjsLocale.test.ts b/src/utils/dayjsLocale.test.ts
index 0ab03860ef6..46e4b9b5e04 100644
--- a/src/utils/dayjsLocale.test.ts
+++ b/src/utils/dayjsLocale.test.ts
@@ -1,6 +1,14 @@
import { describe, expect, it } from 'vitest';
-import { normalizeDayjsLocale } from './dayjsLocale';
+import { loadDayjsLocaleModule, normalizeDayjsLocale } from './dayjsLocale';
+
+const localeModule = {
+ default: {
+ formats: {},
+ name: 'test',
+ relativeTime: {},
+ } satisfies ILocale,
+};
describe('normalizeDayjsLocale', () => {
it('should normalize simplified Chinese script locales to zh-cn', () => {
@@ -13,3 +21,15 @@ describe('normalizeDayjsLocale', () => {
expect(normalizeDayjsLocale('zh-Hant-TW')).toBe('zh-tw');
});
});
+
+describe('loadDayjsLocaleModule', () => {
+ it('should load a lazy glob locale module', async () => {
+ await expect(loadDayjsLocaleModule(() => Promise.resolve(localeModule))).resolves.toBe(
+ localeModule,
+ );
+ });
+
+ it('should return an eager glob locale module', async () => {
+ await expect(loadDayjsLocaleModule(localeModule)).resolves.toBe(localeModule);
+ });
+});
diff --git a/src/utils/dayjsLocale.ts b/src/utils/dayjsLocale.ts
index 7969bc90293..d2126839793 100644
--- a/src/utils/dayjsLocale.ts
+++ b/src/utils/dayjsLocale.ts
@@ -6,6 +6,18 @@ const DAYJS_LOCALE_ALIASES: Record<string, string> = {
'zh': 'zh-cn',
};
+interface DayjsLocaleModule {
+ default: ILocale;
+}
+
+type DayjsLocaleLoader = () => DayjsLocaleModule | Promise<DayjsLocaleModule>;
+
+export type DayjsLocaleGlobEntry = DayjsLocaleLoader | DayjsLocaleModule;
+
+export const loadDayjsLocaleModule = async (
+ entry: DayjsLocaleGlobEntry,
+): Promise<DayjsLocaleModule> => (typeof entry === 'function' ? entry() : entry);
+
export const normalizeDayjsLocale = (lang: string): string => {
const lower = lang.toLowerCase();
if (lower.startsWith('zh-hans')) return 'zh-cn';