1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
| import type { LocaleSetupOptions, SupportedLanguagesType } from '@vben/locales';
|
| import type { App } from 'vue';
|
| import {
| $t,
| setupI18n as coreSetup,
| loadLocalesMapFromDir,
| } from '@vben/locales';
| import { preferences } from '@vben/preferences';
|
| const modules = import.meta.glob('./langs/**/*.json');
|
| const localesMap = loadLocalesMapFromDir(
| /\.\/langs\/([^/]+)\/(.*)\.json$/,
| modules,
| );
|
| /**
| * 加载应用特有的语言包
| * 这里也可以改造为从服务端获取翻译数据
| * @param lang
| */
| async function loadMessages(lang: SupportedLanguagesType) {
| const appLocaleMessages = await localesMap[lang]?.();
| return appLocaleMessages?.default;
| }
|
| async function setupI18n(app: App, options: LocaleSetupOptions = {}) {
| await coreSetup(app, {
| defaultLocale: preferences.app.locale,
| loadMessages,
| missingWarn: !import.meta.env.PROD,
| ...options,
| });
| }
|
| export { $t, setupI18n };
|
|