From 208d4188fc0a9d64fed520204765c92274a0f615 Mon Sep 17 00:00:00 2001 From: vben Date: Sun, 7 Jul 2024 00:17:44 +0800 Subject: [PATCH] refactor: refactor locales, separate locales within apps,fixed #12 --- .vscode/settings.json | 5 +- apps/web-antd/CHANGELOG.md | 2 +- apps/web-antd/package.json | 2 +- apps/web-antd/src/app.vue | 2 +- apps/web-antd/src/bootstrap.ts | 5 +- apps/web-antd/src/forward/access.ts | 2 +- apps/web-antd/src/forward/index.ts | 2 +- apps/web-antd/src/layouts/basic.vue | 2 +- .../{forward/locale.ts => locales/index.ts} | 43 +- apps/web-antd/src/locales/langs/en-US.yaml | 28 ++ apps/web-antd/src/locales/langs/zh-CN.yaml | 27 ++ apps/web-antd/src/router/guard.ts | 4 +- .../web-antd/src/router/routes/_essentials.ts | 2 +- .../src/router/routes/modules/dashboard.ts | 3 +- .../src/router/routes/modules/demos.ts | 3 +- .../src/router/routes/modules/vben.ts | 3 +- .../views/_essential/authentication/login.vue | 2 +- internal/tailwind-config/package.json | 2 +- .../vite-config/src/config/application.ts | 4 +- internal/vite-config/src/config/library.ts | 4 +- internal/vite-config/src/plugins/index.ts | 46 +- packages/{ => @core}/locales/build.config.ts | 1 - packages/{ => @core}/locales/package.json | 12 +- packages/{ => @core}/locales/src/i18n.ts | 26 +- packages/@core/locales/src/index.ts | 33 ++ .../{ => @core}/locales/src/langs/en-US.yaml | 28 -- .../{ => @core}/locales/src/langs/zh-CN.yaml | 28 -- packages/{ => @core}/locales/src/typing.ts | 15 +- packages/{ => @core}/locales/tsconfig.json | 0 packages/business/access/package.json | 2 +- .../generate-routes-backend.ts | 2 +- packages/business/layouts/package.json | 2 +- .../src/authentication/authentication.vue | 2 +- .../authentication/widgets/layout-toggle.vue | 2 +- .../business/layouts/src/basic/layout.vue | 2 +- .../layouts/src/basic/tabbar/use-tabs.ts | 2 +- .../layouts/src/basic/widgets/breadcrumb.vue | 2 +- packages/business/universal-ui/package.json | 2 +- .../src/authentication/code-login.vue | 2 +- .../src/authentication/forget-password.vue | 2 +- .../universal-ui/src/authentication/login.vue | 2 +- .../src/authentication/qrcode-login.vue | 2 +- .../src/authentication/register.vue | 2 +- .../src/authentication/third-party-login.vue | 2 +- .../universal-ui/src/fallback/fallback.vue | 2 +- packages/business/widgets/package.json | 2 +- .../widgets/src/coze-assistant/assistant.vue | 8 +- .../src/global-search/global-search.vue | 2 +- .../src/global-search/search-panel.vue | 2 +- .../src/language-toggle/language-toggle.vue | 2 +- .../widgets/src/notification/notification.vue | 2 +- .../preferences/blocks/general/animation.vue | 2 +- .../preferences/blocks/general/general.vue | 2 +- .../preferences/blocks/layout/breadcrumb.vue | 2 +- .../src/preferences/blocks/layout/content.vue | 2 +- .../preferences/blocks/layout/copyright.vue | 2 +- .../src/preferences/blocks/layout/footer.vue | 2 +- .../src/preferences/blocks/layout/header.vue | 2 +- .../blocks/layout/interface-control.vue | 2 +- .../src/preferences/blocks/layout/layout.vue | 2 +- .../preferences/blocks/layout/navigation.vue | 2 +- .../src/preferences/blocks/layout/sidebar.vue | 2 +- .../src/preferences/blocks/layout/tabbar.vue | 2 +- .../blocks/shortcut-keys/global.vue | 2 +- .../src/preferences/blocks/theme/builtin.vue | 2 +- .../preferences/blocks/theme/color-mode.vue | 2 +- .../src/preferences/blocks/theme/theme.vue | 2 +- .../src/preferences/preferences-widget.vue | 2 +- .../widgets/src/preferences/preferences.vue | 2 +- .../widgets/src/preferences/trigger.vue | 2 +- .../widgets/src/theme-toggle/theme-toggle.vue | 2 +- .../src/user-dropdown/user-dropdown.vue | 2 +- packages/locales/src/helper.ts | 8 - packages/locales/src/index.ts | 26 -- pnpm-lock.yaml | 409 ++++++++++-------- pnpm-workspace.yaml | 1 + vben-admin.code-workspace | 8 +- 77 files changed, 486 insertions(+), 394 deletions(-) rename apps/web-antd/src/{forward/locale.ts => locales/index.ts} (54%) create mode 100644 apps/web-antd/src/locales/langs/en-US.yaml create mode 100644 apps/web-antd/src/locales/langs/zh-CN.yaml rename packages/{ => @core}/locales/build.config.ts (94%) rename packages/{ => @core}/locales/package.json (83%) rename packages/{ => @core}/locales/src/i18n.ts (60%) create mode 100644 packages/@core/locales/src/index.ts rename packages/{ => @core}/locales/src/langs/en-US.yaml (90%) rename packages/{ => @core}/locales/src/langs/zh-CN.yaml (90%) rename packages/{ => @core}/locales/src/typing.ts (55%) rename packages/{ => @core}/locales/tsconfig.json (100%) delete mode 100644 packages/locales/src/helper.ts delete mode 100644 packages/locales/src/index.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index ea8dc42b..39bce5fb 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -169,7 +169,10 @@ "packages/@vben-core/shared/design-tokens/src/**/*.css" ], - "i18n-ally.localesPaths": ["packages/locales/src/langs"], + "i18n-ally.localesPaths": [ + "packages/@core/locales/src/langs", + "apps/*/src/locales/langs" + ], "i18n-ally.enabledParsers": ["json", "ts", "js", "yaml"], "i18n-ally.sourceLanguage": "en", "i18n-ally.displayLanguage": "zh-CN", diff --git a/apps/web-antd/CHANGELOG.md b/apps/web-antd/CHANGELOG.md index 08d1fbf0..5435b2ff 100644 --- a/apps/web-antd/CHANGELOG.md +++ b/apps/web-antd/CHANGELOG.md @@ -16,7 +16,7 @@ - @vben/constants@5.0.1 - @vben/hooks@5.0.1 - @vben/icons@5.0.1 - - @vben/locales@5.0.1 + - #/locales@5.0.1 - @vben/styles@5.0.1 - @vben/types@5.0.1 - @vben/utils@5.0.1 diff --git a/apps/web-antd/package.json b/apps/web-antd/package.json index cc73e943..7d430d0d 100644 --- a/apps/web-antd/package.json +++ b/apps/web-antd/package.json @@ -26,6 +26,7 @@ }, "dependencies": { "@vben-core/helpers": "workspace:*", + "@vben-core/locales": "workspace:*", "@vben-core/preferences": "workspace:*", "@vben-core/request": "workspace:*", "@vben-core/stores": "workspace:*", @@ -34,7 +35,6 @@ "@vben/constants": "workspace:*", "@vben/icons": "workspace:*", "@vben/layouts": "workspace:*", - "@vben/locales": "workspace:*", "@vben/styles": "workspace:*", "@vben/types": "workspace:*", "@vben/universal-ui": "workspace:*", diff --git a/apps/web-antd/src/app.vue b/apps/web-antd/src/app.vue index 8b947a6c..694c77ed 100644 --- a/apps/web-antd/src/app.vue +++ b/apps/web-antd/src/app.vue @@ -6,7 +6,7 @@ import { preferences, usePreferences } from '@vben-core/preferences'; import { App, ConfigProvider, theme } from 'ant-design-vue'; -import { antdLocale } from '#/forward'; +import { antdLocale } from '#/locales'; defineOptions({ name: 'App' }); diff --git a/apps/web-antd/src/bootstrap.ts b/apps/web-antd/src/bootstrap.ts index d2b66807..f8cda79a 100644 --- a/apps/web-antd/src/bootstrap.ts +++ b/apps/web-antd/src/bootstrap.ts @@ -1,10 +1,9 @@ import { createApp } from 'vue'; -import { setupI18n } from '@vben/locales'; import '@vben/styles'; import { preferences } from '@vben-core/preferences'; -import { loadThirdPartyMessage } from '#/forward'; +import { loadMessages, setupI18n } from '#/locales'; import { setupStore } from '#/store'; import App from './app.vue'; @@ -16,7 +15,7 @@ async function bootstrap(namespace: string) { // 国际化 i18n 配置 await setupI18n(app, { defaultLocale: preferences.app.locale, - loadThirdPartyMessage, + loadMessages, }); // 配置 pinia-store diff --git a/apps/web-antd/src/forward/access.ts b/apps/web-antd/src/forward/access.ts index 8599e314..03cbf659 100644 --- a/apps/web-antd/src/forward/access.ts +++ b/apps/web-antd/src/forward/access.ts @@ -2,13 +2,13 @@ import type { GeneratorMenuAndRoutesOptions } from '@vben/access'; import type { ComponentRecordType } from '@vben/types'; import { generateMenusAndRoutes } from '@vben/access'; -import { $t } from '@vben/locales'; import { preferences } from '@vben-core/preferences'; import { message } from 'ant-design-vue'; import { getAllMenus } from '#/apis'; import { BasicLayout, IFrameView } from '#/layouts'; +import { $t } from '#/locales'; const forbiddenPage = () => import('#/views/_essential/fallback/forbidden.vue'); diff --git a/apps/web-antd/src/forward/index.ts b/apps/web-antd/src/forward/index.ts index 54e1e279..2766e57b 100644 --- a/apps/web-antd/src/forward/index.ts +++ b/apps/web-antd/src/forward/index.ts @@ -1,2 +1,2 @@ -export * from './locale'; +export * from './access'; export * from './request'; diff --git a/apps/web-antd/src/layouts/basic.vue b/apps/web-antd/src/layouts/basic.vue index 5b2bf041..fb873271 100644 --- a/apps/web-antd/src/layouts/basic.vue +++ b/apps/web-antd/src/layouts/basic.vue @@ -7,11 +7,11 @@ import { useRouter } from 'vue-router'; import { LOGIN_PATH } from '@vben/constants'; import { IcRoundCreditScore, MdiDriveDocument, MdiGithub } from '@vben/icons'; import { BasicLayout } from '@vben/layouts'; -import { $t } from '@vben/locales'; import { openWindow } from '@vben/utils'; import { Notification, UserDropdown } from '@vben/widgets'; import { preferences } from '@vben-core/preferences'; +import { $t } from '#/locales'; import { resetRoutes } from '#/router'; import { useAppStore } from '#/store'; diff --git a/apps/web-antd/src/forward/locale.ts b/apps/web-antd/src/locales/index.ts similarity index 54% rename from apps/web-antd/src/forward/locale.ts rename to apps/web-antd/src/locales/index.ts index 1af8a109..2e26a220 100644 --- a/apps/web-antd/src/forward/locale.ts +++ b/apps/web-antd/src/locales/index.ts @@ -3,11 +3,41 @@ import type { Locale } from 'ant-design-vue/es/locale'; import { ref } from 'vue'; +import { $t, loadLocalesMap, setupI18n } from '@vben-core/locales'; + import defaultLocale from 'ant-design-vue/es/locale/zh_CN'; import dayjs from 'dayjs'; const antdLocale = ref(defaultLocale); +const modules = import.meta.glob('./langs/*.y(a)?ml'); + +const localesMap = loadLocalesMap(modules); + +/** + * 加载应用特有的语言包 + * @param lang + */ +async function loadMessages(lang: SupportedLanguagesType) { + const [appLocaleMessages] = await Promise.all([ + localesMap[lang](), + loadThirdPartyMessage(lang), + ]); + return appLocaleMessages.default; +} + +/** + * 加载第三方组件库的语言包 + * @param lang + */ +async function loadThirdPartyMessage(lang: SupportedLanguagesType) { + await Promise.all([loadAntdLocale(lang), loadDayjsLocale(lang)]); +} + +/** + * 加载dayjs的语言包 + * @param lang + */ async function loadDayjsLocale(lang: SupportedLanguagesType) { let locale; switch (lang) { @@ -19,13 +49,18 @@ async function loadDayjsLocale(lang: SupportedLanguagesType) { locale = await import('dayjs/locale/en'); break; } + // 默认使用英语 default: { locale = await import('dayjs/locale/en'); - } // 默认使用英语 + } } dayjs.locale(locale); } +/** + * 加载antd的语言包 + * @param lang + */ async function loadAntdLocale(lang: SupportedLanguagesType) { switch (lang) { case 'zh-CN': { @@ -41,8 +76,4 @@ async function loadAntdLocale(lang: SupportedLanguagesType) { } } -async function loadThirdPartyMessage(land: SupportedLanguagesType) { - await Promise.all([loadAntdLocale(land), loadDayjsLocale(land)]); -} - -export { antdLocale, loadThirdPartyMessage }; +export { $t, antdLocale, loadMessages, setupI18n }; diff --git a/apps/web-antd/src/locales/langs/en-US.yaml b/apps/web-antd/src/locales/langs/en-US.yaml new file mode 100644 index 00000000..4b59e7f1 --- /dev/null +++ b/apps/web-antd/src/locales/langs/en-US.yaml @@ -0,0 +1,28 @@ +page: + demos: + title: Demos + access: + title: Access Control + frontend-control: Front-end Control + backend-control: Backend Control + page: Page visit + button: Button control + loading-menu: In the loading menu + access-test-1: Super visit + access-test-2: Admin visit + access-test-3: User visit + nested: + title: Nested Menu + menu1: Menu 1 + menu2: Menu 2 + menu21: Menu 2-1 + menu3: Menu 3 + menu31: Menu 3-1 + menu32: Menu 3-2 + menu321: Menu 3-2-1 + outside: + title: External Page + embedded: embedded Page + external-link: External Link + fallback: + title: Fallback Page diff --git a/apps/web-antd/src/locales/langs/zh-CN.yaml b/apps/web-antd/src/locales/langs/zh-CN.yaml new file mode 100644 index 00000000..d3be1f98 --- /dev/null +++ b/apps/web-antd/src/locales/langs/zh-CN.yaml @@ -0,0 +1,27 @@ +page: + demos: + title: 演示 + access: + title: 访问控制 + frontend-control: 前端控制 + backend-control: 后端控制 + page: 页面访问 + button: 按钮控制 + access-test-1: Super 可见 + access-test-2: Admin 可见 + access-test-3: User 可见 + nested: + title: 嵌套菜单 + menu1: 菜单 1 + menu2: 菜单 2 + menu21: 菜单 2-1 + menu3: 菜单 3 + menu31: 菜单 3-1 + menu32: 菜单 3-2 + menu321: 菜单 3-2-1 + outside: + title: 外部页面 + embedded: 内嵌 + external-link: 外链 + fallback: + title: 缺省页 diff --git a/apps/web-antd/src/router/guard.ts b/apps/web-antd/src/router/guard.ts index cc554c3d..9b494d2f 100644 --- a/apps/web-antd/src/router/guard.ts +++ b/apps/web-antd/src/router/guard.ts @@ -1,13 +1,13 @@ import type { Router } from 'vue-router'; import { LOGIN_PATH } from '@vben/constants'; -import { $t } from '@vben/locales'; import { startProgress, stopProgress } from '@vben/utils'; import { preferences } from '@vben-core/preferences'; import { useTitle } from '@vueuse/core'; -import { generateAccess } from '#/forward/access'; +import { generateAccess } from '#/forward'; +import { $t } from '#/locales'; import { dynamicRoutes, essentialsRouteNames } from '#/router/routes'; import { useAccessStore } from '#/store'; diff --git a/apps/web-antd/src/router/routes/_essentials.ts b/apps/web-antd/src/router/routes/_essentials.ts index ef92bfdc..bb619a39 100644 --- a/apps/web-antd/src/router/routes/_essentials.ts +++ b/apps/web-antd/src/router/routes/_essentials.ts @@ -1,9 +1,9 @@ import type { RouteRecordRaw } from 'vue-router'; import { DEFAULT_HOME_PATH } from '@vben/constants'; -import { $t } from '@vben/locales'; import { AuthPageLayout } from '#/layouts'; +import { $t } from '#/locales'; import Login from '#/views/_essential/authentication/login.vue'; /** 全局404页面 */ diff --git a/apps/web-antd/src/router/routes/modules/dashboard.ts b/apps/web-antd/src/router/routes/modules/dashboard.ts index cea314a3..ecfcbe0c 100644 --- a/apps/web-antd/src/router/routes/modules/dashboard.ts +++ b/apps/web-antd/src/router/routes/modules/dashboard.ts @@ -1,8 +1,7 @@ import type { RouteRecordRaw } from 'vue-router'; -import { $t } from '@vben/locales/helper'; - import { BasicLayout } from '#/layouts'; +import { $t } from '#/locales'; const routes: RouteRecordRaw[] = [ { diff --git a/apps/web-antd/src/router/routes/modules/demos.ts b/apps/web-antd/src/router/routes/modules/demos.ts index 31b4d5e8..f8f28cba 100644 --- a/apps/web-antd/src/router/routes/modules/demos.ts +++ b/apps/web-antd/src/router/routes/modules/demos.ts @@ -1,8 +1,7 @@ import type { RouteRecordRaw } from 'vue-router'; -import { $t } from '@vben/locales/helper'; - import { BasicLayout, IFrameView } from '#/layouts'; +import { $t } from '#/locales'; const routes: RouteRecordRaw[] = [ { diff --git a/apps/web-antd/src/router/routes/modules/vben.ts b/apps/web-antd/src/router/routes/modules/vben.ts index 186e9380..21fed101 100644 --- a/apps/web-antd/src/router/routes/modules/vben.ts +++ b/apps/web-antd/src/router/routes/modules/vben.ts @@ -2,9 +2,8 @@ import type { RouteRecordRaw } from 'vue-router'; import { VBEN_GITHUB_URL, VBEN_LOGO } from '@vben/constants'; -import { $t } from '@vben/locales/helper'; - import { BasicLayout, IFrameView } from '#/layouts'; +import { $t } from '#/locales'; const routes: RouteRecordRaw[] = [ { diff --git a/apps/web-antd/src/views/_essential/authentication/login.vue b/apps/web-antd/src/views/_essential/authentication/login.vue index 45ae94e5..8dc79279 100644 --- a/apps/web-antd/src/views/_essential/authentication/login.vue +++ b/apps/web-antd/src/views/_essential/authentication/login.vue @@ -1,11 +1,11 @@