@qlover/create-app 1.1.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/dist/index.cjs +9 -9
- package/dist/index.js +9 -9
- package/package.json +5 -4
- package/dist/configs/_common/.editorconfig +0 -23
- package/dist/configs/_common/.env.template +0 -13
- package/dist/configs/_common/.gitattributes +0 -2
- package/dist/configs/_common/.github/workflows/general-check.yml +0 -41
- package/dist/configs/_common/.github/workflows/release.yml +0 -81
- package/dist/configs/_common/.gitignore.template +0 -64
- package/dist/configs/_common/.husky/commit-msg +0 -3
- package/dist/configs/_common/.husky/pre-commit +0 -3
- package/dist/configs/_common/.prettierignore +0 -17
- package/dist/configs/_common/.prettierrc.js +0 -7
- package/dist/configs/_common/.vscode/extensions.json +0 -9
- package/dist/configs/_common/.vscode/react.code-snippets +0 -19
- package/dist/configs/_common/.vscode/settings.json +0 -16
- package/dist/configs/_common/commitlint.config.js +0 -10
- package/dist/configs/_common/package.json.template +0 -75
- package/dist/configs/node-lib/eslint.config.js +0 -50
- package/dist/templates/next-app/.env.template +0 -25
- package/dist/templates/next-app/.prettierignore +0 -58
- package/dist/templates/next-app/README.en.md +0 -130
- package/dist/templates/next-app/README.md +0 -130
- package/dist/templates/next-app/config/IOCIdentifier.ts +0 -74
- package/dist/templates/next-app/config/Identifier/api.ts +0 -41
- package/dist/templates/next-app/config/Identifier/common/admint.table.ts +0 -69
- package/dist/templates/next-app/config/Identifier/common/common.ts +0 -90
- package/dist/templates/next-app/config/Identifier/common/index.ts +0 -3
- package/dist/templates/next-app/config/Identifier/common/validators.ts +0 -34
- package/dist/templates/next-app/config/Identifier/index.ts +0 -3
- package/dist/templates/next-app/config/Identifier/pages/index.ts +0 -7
- package/dist/templates/next-app/config/Identifier/pages/page.about.ts +0 -20
- package/dist/templates/next-app/config/Identifier/pages/page.admin.home.ts +0 -27
- package/dist/templates/next-app/config/Identifier/pages/page.admin.locales.ts +0 -266
- package/dist/templates/next-app/config/Identifier/pages/page.admin.user.ts +0 -293
- package/dist/templates/next-app/config/Identifier/pages/page.home.ts +0 -56
- package/dist/templates/next-app/config/Identifier/pages/page.login.ts +0 -159
- package/dist/templates/next-app/config/Identifier/pages/page.register.ts +0 -177
- package/dist/templates/next-app/config/adminNavs.ts +0 -19
- package/dist/templates/next-app/config/common.ts +0 -43
- package/dist/templates/next-app/config/cookies.ts +0 -23
- package/dist/templates/next-app/config/i18n/AboutI18n.ts +0 -14
- package/dist/templates/next-app/config/i18n/HomeI18n.ts +0 -24
- package/dist/templates/next-app/config/i18n/PageI18nInterface.ts +0 -51
- package/dist/templates/next-app/config/i18n/admin18n.ts +0 -75
- package/dist/templates/next-app/config/i18n/i18nConfig.ts +0 -16
- package/dist/templates/next-app/config/i18n/i18nKeyScheam.ts +0 -36
- package/dist/templates/next-app/config/i18n/index.ts +0 -7
- package/dist/templates/next-app/config/i18n/loginI18n.ts +0 -50
- package/dist/templates/next-app/config/i18n/register18n.ts +0 -44
- package/dist/templates/next-app/config/route.ts +0 -9
- package/dist/templates/next-app/config/theme.ts +0 -28
- package/dist/templates/next-app/docs/en/api.md +0 -387
- package/dist/templates/next-app/docs/en/component.md +0 -544
- package/dist/templates/next-app/docs/en/database.md +0 -496
- package/dist/templates/next-app/docs/en/development-guide.md +0 -727
- package/dist/templates/next-app/docs/en/env.md +0 -563
- package/dist/templates/next-app/docs/en/i18n.md +0 -287
- package/dist/templates/next-app/docs/en/index.md +0 -165
- package/dist/templates/next-app/docs/en/page.md +0 -457
- package/dist/templates/next-app/docs/en/project-structure.md +0 -176
- package/dist/templates/next-app/docs/en/router.md +0 -427
- package/dist/templates/next-app/docs/en/theme.md +0 -532
- package/dist/templates/next-app/docs/en/validator.md +0 -478
- package/dist/templates/next-app/docs/zh/api.md +0 -387
- package/dist/templates/next-app/docs/zh/component.md +0 -544
- package/dist/templates/next-app/docs/zh/database.md +0 -496
- package/dist/templates/next-app/docs/zh/development-guide.md +0 -727
- package/dist/templates/next-app/docs/zh/env.md +0 -563
- package/dist/templates/next-app/docs/zh/i18n.md +0 -287
- package/dist/templates/next-app/docs/zh/index.md +0 -165
- package/dist/templates/next-app/docs/zh/page.md +0 -457
- package/dist/templates/next-app/docs/zh/project-structure.md +0 -176
- package/dist/templates/next-app/docs/zh/router.md +0 -427
- package/dist/templates/next-app/docs/zh/theme.md +0 -532
- package/dist/templates/next-app/docs/zh/validator.md +0 -476
- package/dist/templates/next-app/eslint.config.mjs +0 -285
- package/dist/templates/next-app/make/generateLocales.ts +0 -32
- package/dist/templates/next-app/migrations/schema/LocalesSchema.ts +0 -15
- package/dist/templates/next-app/migrations/schema/UserSchema.ts +0 -38
- package/dist/templates/next-app/migrations/sql/1694244000000.sql +0 -21
- package/dist/templates/next-app/next.config.ts +0 -25
- package/dist/templates/next-app/package.json +0 -87
- package/dist/templates/next-app/postcss.config.mjs +0 -5
- package/dist/templates/next-app/public/favicon.ico +0 -0
- package/dist/templates/next-app/public/file.svg +0 -1
- package/dist/templates/next-app/public/globe.svg +0 -1
- package/dist/templates/next-app/public/locales/en.json +0 -182
- package/dist/templates/next-app/public/locales/zh.json +0 -182
- package/dist/templates/next-app/public/next.svg +0 -1
- package/dist/templates/next-app/public/vercel.svg +0 -1
- package/dist/templates/next-app/public/window.svg +0 -1
- package/dist/templates/next-app/src/app/[locale]/admin/AdminI18nProvider.tsx +0 -37
- package/dist/templates/next-app/src/app/[locale]/admin/layout.tsx +0 -42
- package/dist/templates/next-app/src/app/[locale]/admin/locales/page.tsx +0 -153
- package/dist/templates/next-app/src/app/[locale]/admin/page.tsx +0 -20
- package/dist/templates/next-app/src/app/[locale]/admin/users/page.tsx +0 -67
- package/dist/templates/next-app/src/app/[locale]/auth/layout.tsx +0 -18
- package/dist/templates/next-app/src/app/[locale]/auth/login/LoginForm.tsx +0 -126
- package/dist/templates/next-app/src/app/[locale]/auth/login/page.tsx +0 -90
- package/dist/templates/next-app/src/app/[locale]/auth/page.tsx +0 -8
- package/dist/templates/next-app/src/app/[locale]/auth/register/RegisterForm.tsx +0 -197
- package/dist/templates/next-app/src/app/[locale]/auth/register/page.tsx +0 -90
- package/dist/templates/next-app/src/app/[locale]/layout.tsx +0 -63
- package/dist/templates/next-app/src/app/[locale]/not-found.tsx +0 -24
- package/dist/templates/next-app/src/app/[locale]/page.tsx +0 -98
- package/dist/templates/next-app/src/app/api/admin/locales/create/route.ts +0 -13
- package/dist/templates/next-app/src/app/api/admin/locales/import/route.ts +0 -13
- package/dist/templates/next-app/src/app/api/admin/locales/route.ts +0 -20
- package/dist/templates/next-app/src/app/api/admin/locales/update/route.ts +0 -13
- package/dist/templates/next-app/src/app/api/admin/users/route.ts +0 -20
- package/dist/templates/next-app/src/app/api/ai/completions/route.ts +0 -32
- package/dist/templates/next-app/src/app/api/auth/callback/route.ts +0 -11
- package/dist/templates/next-app/src/app/api/callback/route.ts +0 -49
- package/dist/templates/next-app/src/app/api/locales/json/route.ts +0 -33
- package/dist/templates/next-app/src/app/api/user/login/route.ts +0 -10
- package/dist/templates/next-app/src/app/api/user/logout/route.ts +0 -8
- package/dist/templates/next-app/src/app/api/user/register/route.ts +0 -11
- package/dist/templates/next-app/src/app/manifest.ts +0 -16
- package/dist/templates/next-app/src/app/robots.txt +0 -2
- package/dist/templates/next-app/src/base/cases/AdminPageManager.ts +0 -28
- package/dist/templates/next-app/src/base/cases/AppConfig.ts +0 -40
- package/dist/templates/next-app/src/base/cases/Datetime.ts +0 -18
- package/dist/templates/next-app/src/base/cases/DialogErrorPlugin.ts +0 -57
- package/dist/templates/next-app/src/base/cases/DialogHandler.ts +0 -116
- package/dist/templates/next-app/src/base/cases/InversifyContainer.ts +0 -45
- package/dist/templates/next-app/src/base/cases/NavigateBridge.ts +0 -32
- package/dist/templates/next-app/src/base/cases/RequestEncryptPlugin.ts +0 -77
- package/dist/templates/next-app/src/base/cases/ResourceState.ts +0 -17
- package/dist/templates/next-app/src/base/cases/RouterService.ts +0 -52
- package/dist/templates/next-app/src/base/cases/StringEncryptor.ts +0 -73
- package/dist/templates/next-app/src/base/cases/TranslateI18nUtil.ts +0 -53
- package/dist/templates/next-app/src/base/cases/ZodColumnBuilder.ts +0 -212
- package/dist/templates/next-app/src/base/port/AdminLayoutInterface.ts +0 -26
- package/dist/templates/next-app/src/base/port/AppApiInterface.ts +0 -36
- package/dist/templates/next-app/src/base/port/AppUserApiInterface.ts +0 -27
- package/dist/templates/next-app/src/base/port/I18nServiceInterface.ts +0 -25
- package/dist/templates/next-app/src/base/port/IOCInterface.ts +0 -33
- package/dist/templates/next-app/src/base/port/RouterInterface.ts +0 -11
- package/dist/templates/next-app/src/base/port/UserServiceInterface.ts +0 -25
- package/dist/templates/next-app/src/base/port/ZodBuilderInterface.ts +0 -8
- package/dist/templates/next-app/src/base/services/AdminPageEvent.ts +0 -26
- package/dist/templates/next-app/src/base/services/AdminPageScheduler.ts +0 -42
- package/dist/templates/next-app/src/base/services/AppApiRequester.ts +0 -67
- package/dist/templates/next-app/src/base/services/AppUserGateway.ts +0 -110
- package/dist/templates/next-app/src/base/services/I18nService.ts +0 -87
- package/dist/templates/next-app/src/base/services/ResourceService.ts +0 -139
- package/dist/templates/next-app/src/base/services/UserService.ts +0 -68
- package/dist/templates/next-app/src/base/services/adminApi/AdminLocalesApi.ts +0 -106
- package/dist/templates/next-app/src/base/services/adminApi/AdminUserApi.ts +0 -87
- package/dist/templates/next-app/src/base/services/appApi/AppApiPlugin.ts +0 -110
- package/dist/templates/next-app/src/base/services/appApi/AppUserApiBootstrap.ts +0 -52
- package/dist/templates/next-app/src/base/types/AppPageRouter.ts +0 -12
- package/dist/templates/next-app/src/base/types/PagesRouter.ts +0 -9
- package/dist/templates/next-app/src/core/bootstraps/BootstrapClient.ts +0 -76
- package/dist/templates/next-app/src/core/bootstraps/BootstrapServer.ts +0 -125
- package/dist/templates/next-app/src/core/bootstraps/BootstrapsRegistry.ts +0 -50
- package/dist/templates/next-app/src/core/bootstraps/IocIdentifierTest.ts +0 -26
- package/dist/templates/next-app/src/core/bootstraps/PrintBootstrap.ts +0 -18
- package/dist/templates/next-app/src/core/clientIoc/ClientIOC.ts +0 -68
- package/dist/templates/next-app/src/core/clientIoc/ClientIOCRegister.ts +0 -100
- package/dist/templates/next-app/src/core/globals.ts +0 -28
- package/dist/templates/next-app/src/core/serverIoc/ServerIOC.ts +0 -80
- package/dist/templates/next-app/src/core/serverIoc/ServerIOCRegister.ts +0 -66
- package/dist/templates/next-app/src/i18n/loadMessages.ts +0 -103
- package/dist/templates/next-app/src/i18n/request.ts +0 -31
- package/dist/templates/next-app/src/i18n/routing.ts +0 -35
- package/dist/templates/next-app/src/lib/supabase/client.ts +0 -8
- package/dist/templates/next-app/src/lib/supabase/conts.ts +0 -2
- package/dist/templates/next-app/src/lib/supabase/proxy.ts +0 -84
- package/dist/templates/next-app/src/lib/supabase/server.ts +0 -38
- package/dist/templates/next-app/src/pages/[locale]/about.tsx +0 -61
- package/dist/templates/next-app/src/pages/_app.tsx +0 -50
- package/dist/templates/next-app/src/pages/_document.tsx +0 -13
- package/dist/templates/next-app/src/proxy.ts +0 -33
- package/dist/templates/next-app/src/server/AppErrorApi.ts +0 -10
- package/dist/templates/next-app/src/server/AppPageRouteParams.ts +0 -110
- package/dist/templates/next-app/src/server/AppSuccessApi.ts +0 -7
- package/dist/templates/next-app/src/server/NextApiServer.ts +0 -61
- package/dist/templates/next-app/src/server/PagesRouteParams.ts +0 -145
- package/dist/templates/next-app/src/server/PasswordEncrypt.ts +0 -18
- package/dist/templates/next-app/src/server/ServerAuth.ts +0 -81
- package/dist/templates/next-app/src/server/SupabaseBridge.ts +0 -262
- package/dist/templates/next-app/src/server/UserCredentialToken.ts +0 -53
- package/dist/templates/next-app/src/server/controllers/AdminLocalesController.ts +0 -86
- package/dist/templates/next-app/src/server/controllers/AdminUserController.ts +0 -42
- package/dist/templates/next-app/src/server/controllers/LocalesController.ts +0 -36
- package/dist/templates/next-app/src/server/controllers/UserController.ts +0 -91
- package/dist/templates/next-app/src/server/port/AIControllerInterface.ts +0 -8
- package/dist/templates/next-app/src/server/port/AdminLocalesControllerInterface.ts +0 -21
- package/dist/templates/next-app/src/server/port/AdminUserControllerInterface.ts +0 -11
- package/dist/templates/next-app/src/server/port/CrentialTokenInterface.ts +0 -5
- package/dist/templates/next-app/src/server/port/DBBridgeInterface.ts +0 -36
- package/dist/templates/next-app/src/server/port/DBTableInterface.ts +0 -12
- package/dist/templates/next-app/src/server/port/LocalesControllerInterface.ts +0 -10
- package/dist/templates/next-app/src/server/port/LocalesRepositoryInterface.ts +0 -43
- package/dist/templates/next-app/src/server/port/PaginationInterface.ts +0 -6
- package/dist/templates/next-app/src/server/port/RouteParamsnHandlerInterface.ts +0 -18
- package/dist/templates/next-app/src/server/port/ServerAuthInterface.ts +0 -15
- package/dist/templates/next-app/src/server/port/ServerInterface.ts +0 -23
- package/dist/templates/next-app/src/server/port/UserRepositoryInterface.ts +0 -15
- package/dist/templates/next-app/src/server/port/UserServiceInterface.ts +0 -14
- package/dist/templates/next-app/src/server/port/ValidatorInterface.ts +0 -23
- package/dist/templates/next-app/src/server/repositorys/LocalesRepository.ts +0 -216
- package/dist/templates/next-app/src/server/repositorys/UserRepository.ts +0 -102
- package/dist/templates/next-app/src/server/services/AIService.ts +0 -45
- package/dist/templates/next-app/src/server/services/AdminAuthPlugin.ts +0 -21
- package/dist/templates/next-app/src/server/services/AdminLocalesService.ts +0 -20
- package/dist/templates/next-app/src/server/services/ApiLocaleService.ts +0 -137
- package/dist/templates/next-app/src/server/services/ApiUserService.ts +0 -29
- package/dist/templates/next-app/src/server/services/UserService.ts +0 -134
- package/dist/templates/next-app/src/server/validators/ExtendedExecutorError.ts +0 -6
- package/dist/templates/next-app/src/server/validators/LocalesValidator.ts +0 -145
- package/dist/templates/next-app/src/server/validators/LoginValidator.ts +0 -82
- package/dist/templates/next-app/src/server/validators/PaginationValidator.ts +0 -70
- package/dist/templates/next-app/src/server/validators/SignupVerifyValidator.ts +0 -68
- package/dist/templates/next-app/src/styles/css/antd-themes/_common/_default.css +0 -280
- package/dist/templates/next-app/src/styles/css/antd-themes/_common/dark.css +0 -233
- package/dist/templates/next-app/src/styles/css/antd-themes/_common/index.css +0 -3
- package/dist/templates/next-app/src/styles/css/antd-themes/_common/pink.css +0 -246
- package/dist/templates/next-app/src/styles/css/antd-themes/index.css +0 -4
- package/dist/templates/next-app/src/styles/css/antd-themes/menu/_default.css +0 -108
- package/dist/templates/next-app/src/styles/css/antd-themes/menu/dark.css +0 -67
- package/dist/templates/next-app/src/styles/css/antd-themes/menu/index.css +0 -3
- package/dist/templates/next-app/src/styles/css/antd-themes/menu/pink.css +0 -67
- package/dist/templates/next-app/src/styles/css/antd-themes/no-context.css +0 -34
- package/dist/templates/next-app/src/styles/css/antd-themes/pagination/_default.css +0 -34
- package/dist/templates/next-app/src/styles/css/antd-themes/pagination/dark.css +0 -31
- package/dist/templates/next-app/src/styles/css/antd-themes/pagination/index.css +0 -3
- package/dist/templates/next-app/src/styles/css/antd-themes/pagination/pink.css +0 -36
- package/dist/templates/next-app/src/styles/css/antd-themes/table/_default.css +0 -44
- package/dist/templates/next-app/src/styles/css/antd-themes/table/dark.css +0 -43
- package/dist/templates/next-app/src/styles/css/antd-themes/table/index.css +0 -3
- package/dist/templates/next-app/src/styles/css/antd-themes/table/pink.css +0 -43
- package/dist/templates/next-app/src/styles/css/index.css +0 -6
- package/dist/templates/next-app/src/styles/css/page.css +0 -20
- package/dist/templates/next-app/src/styles/css/scrollbar.css +0 -34
- package/dist/templates/next-app/src/styles/css/tailwind.css +0 -5
- package/dist/templates/next-app/src/styles/css/themes/_default.css +0 -30
- package/dist/templates/next-app/src/styles/css/themes/dark.css +0 -30
- package/dist/templates/next-app/src/styles/css/themes/index.css +0 -3
- package/dist/templates/next-app/src/styles/css/themes/pink.css +0 -30
- package/dist/templates/next-app/src/styles/css/zIndex.css +0 -9
- package/dist/templates/next-app/src/uikit/components/AdminLayout.tsx +0 -150
- package/dist/templates/next-app/src/uikit/components/BootstrapsProvider.tsx +0 -39
- package/dist/templates/next-app/src/uikit/components/ClientRootProvider.tsx +0 -64
- package/dist/templates/next-app/src/uikit/components/ClientSeo.tsx +0 -36
- package/dist/templates/next-app/src/uikit/components/ClinetRenderProvider.tsx +0 -42
- package/dist/templates/next-app/src/uikit/components/EditableCell.tsx +0 -118
- package/dist/templates/next-app/src/uikit/components/FeatureItem.tsx +0 -13
- package/dist/templates/next-app/src/uikit/components/IOCProvider.tsx +0 -34
- package/dist/templates/next-app/src/uikit/components/LocaleLink.tsx +0 -50
- package/dist/templates/next-app/src/uikit/components/With.tsx +0 -17
- package/dist/templates/next-app/src/uikit/components/localesImportButton/LocalesImportButton.tsx +0 -62
- package/dist/templates/next-app/src/uikit/components/localesImportButton/LocalesImportEvent.ts +0 -28
- package/dist/templates/next-app/src/uikit/components/localesImportButton/import.module.css +0 -6
- package/dist/templates/next-app/src/uikit/components-app/AdminButton.tsx +0 -29
- package/dist/templates/next-app/src/uikit/components-app/AppBridge.tsx +0 -17
- package/dist/templates/next-app/src/uikit/components-app/AppRoutePage.tsx +0 -105
- package/dist/templates/next-app/src/uikit/components-app/AuthButton.tsx +0 -20
- package/dist/templates/next-app/src/uikit/components-app/LanguageSwitcher.tsx +0 -75
- package/dist/templates/next-app/src/uikit/components-app/LogoutButton.tsx +0 -45
- package/dist/templates/next-app/src/uikit/components-app/ThemeSwitcher.tsx +0 -144
- package/dist/templates/next-app/src/uikit/components-pages/LanguageSwitcher.tsx +0 -98
- package/dist/templates/next-app/src/uikit/components-pages/PagesRoutePage.tsx +0 -93
- package/dist/templates/next-app/src/uikit/context/IOCContext.ts +0 -25
- package/dist/templates/next-app/src/uikit/hook/useI18nInterface.ts +0 -22
- package/dist/templates/next-app/src/uikit/hook/useIOC.ts +0 -37
- package/dist/templates/next-app/src/uikit/hook/useStrictEffect.ts +0 -32
- package/dist/templates/next-app/src/uikit/hook/useWarnTranslations.ts +0 -11
- package/dist/templates/next-app/src/uikit/utils/getHashParams.ts +0 -8
- package/dist/templates/next-app/src/uikit/utils/getHashVerifyEmailParams.ts +0 -42
- package/dist/templates/next-app/tailwind.config.ts +0 -8
- package/dist/templates/next-app/tsconfig.json +0 -39
- package/dist/templates/node-lib/__tests__/readJson.test.ts +0 -26
- package/dist/templates/node-lib/bin/test.js +0 -30
- package/dist/templates/node-lib/package.json +0 -66
- package/dist/templates/node-lib/rollup.config.js +0 -79
- package/dist/templates/node-lib/src/index.ts +0 -7
- package/dist/templates/node-lib/src/readJson.ts +0 -12
- package/dist/templates/node-lib/tsconfig.json +0 -23
- package/dist/templates/pack-app/README.md +0 -108
- package/dist/templates/pack-app/eslint.config.js +0 -97
- package/dist/templates/pack-app/fe-config.json +0 -35
- package/dist/templates/pack-app/package.json +0 -86
- package/dist/templates/pack-app/pnpm-workspace.yaml +0 -2
- package/dist/templates/pack-app/tsconfig.json +0 -9
- package/dist/templates/pack-app/tsconfig.test.json +0 -10
- package/dist/templates/pack-app/vite.config.ts +0 -14
- package/dist/templates/react-app/.env.template +0 -22
- package/dist/templates/react-app/.prettierignore +0 -17
- package/dist/templates/react-app/README.en.md +0 -274
- package/dist/templates/react-app/README.md +0 -273
- package/dist/templates/react-app/__tests__/__mocks__/BootstrapTest.ts +0 -16
- package/dist/templates/react-app/__tests__/__mocks__/MockAppConfig.ts +0 -48
- package/dist/templates/react-app/__tests__/__mocks__/MockDialogHandler.ts +0 -17
- package/dist/templates/react-app/__tests__/__mocks__/MockLogger.ts +0 -14
- package/dist/templates/react-app/__tests__/__mocks__/components/TestApp.tsx +0 -38
- package/dist/templates/react-app/__tests__/__mocks__/components/TestBootstrapsProvider.tsx +0 -53
- package/dist/templates/react-app/__tests__/__mocks__/components/TestRouter.tsx +0 -46
- package/dist/templates/react-app/__tests__/__mocks__/components/index.ts +0 -12
- package/dist/templates/react-app/__tests__/__mocks__/createMockGlobals.ts +0 -96
- package/dist/templates/react-app/__tests__/__mocks__/i18nextHttpBackend.ts +0 -110
- package/dist/templates/react-app/__tests__/__mocks__/testIOC/TestIOC.ts +0 -55
- package/dist/templates/react-app/__tests__/__mocks__/testIOC/TestIOCRegister.ts +0 -74
- package/dist/templates/react-app/__tests__/setup/index.ts +0 -1
- package/dist/templates/react-app/__tests__/setup/setupGlobal.ts +0 -64
- package/dist/templates/react-app/__tests__/src/App.structure.test.tsx +0 -115
- package/dist/templates/react-app/__tests__/src/base/cases/AppConfig.test.ts +0 -288
- package/dist/templates/react-app/__tests__/src/base/cases/DialogHandler.test.ts +0 -226
- package/dist/templates/react-app/__tests__/src/base/cases/I18nKeyErrorPlugin.test.ts +0 -178
- package/dist/templates/react-app/__tests__/src/base/cases/InversifyContainer.test.ts +0 -181
- package/dist/templates/react-app/__tests__/src/base/cases/PublicAssetsPath.test.ts +0 -61
- package/dist/templates/react-app/__tests__/src/base/cases/RequestLogger.test.ts +0 -177
- package/dist/templates/react-app/__tests__/src/base/cases/RequestStatusCatcher.test.ts +0 -191
- package/dist/templates/react-app/__tests__/src/base/cases/RouterLoader.test.ts +0 -245
- package/dist/templates/react-app/__tests__/src/base/services/I18nService.test.ts +0 -240
- package/dist/templates/react-app/__tests__/src/core/IOC.test.ts +0 -242
- package/dist/templates/react-app/__tests__/src/core/bootstraps/BootstrapClient.test.ts +0 -135
- package/dist/templates/react-app/__tests__/src/core/bootstraps/BootstrapsApp.test.ts +0 -74
- package/dist/templates/react-app/__tests__/src/main.test.tsx +0 -46
- package/dist/templates/react-app/__tests__/src/uikit/components/BaseHeader.test.tsx +0 -97
- package/dist/templates/react-app/__tests__/src/uikit/components/chatMessage/ChatRoot.test.tsx +0 -274
- package/dist/templates/react-app/config/IOCIdentifier.ts +0 -91
- package/dist/templates/react-app/config/Identifier/common/common.error.ts +0 -34
- package/dist/templates/react-app/config/Identifier/common/common.ts +0 -62
- package/dist/templates/react-app/config/Identifier/common/index.ts +0 -2
- package/dist/templates/react-app/config/Identifier/components/component.chatMessage.ts +0 -56
- package/dist/templates/react-app/config/Identifier/components/component.messageBaseList.ts +0 -103
- package/dist/templates/react-app/config/Identifier/index.ts +0 -2
- package/dist/templates/react-app/config/Identifier/pages/index.ts +0 -9
- package/dist/templates/react-app/config/Identifier/pages/page.about.ts +0 -189
- package/dist/templates/react-app/config/Identifier/pages/page.executor.ts +0 -275
- package/dist/templates/react-app/config/Identifier/pages/page.home.ts +0 -71
- package/dist/templates/react-app/config/Identifier/pages/page.identifiter.ts +0 -102
- package/dist/templates/react-app/config/Identifier/pages/page.jsonStorage.ts +0 -77
- package/dist/templates/react-app/config/Identifier/pages/page.login.ts +0 -162
- package/dist/templates/react-app/config/Identifier/pages/page.message.ts +0 -20
- package/dist/templates/react-app/config/Identifier/pages/page.register.ts +0 -159
- package/dist/templates/react-app/config/Identifier/pages/page.request.ts +0 -169
- package/dist/templates/react-app/config/app.router.ts +0 -338
- package/dist/templates/react-app/config/common.ts +0 -85
- package/dist/templates/react-app/config/feapi.mock.json +0 -34
- package/dist/templates/react-app/config/i18n/PageI18nInterface.ts +0 -53
- package/dist/templates/react-app/config/i18n/aboutI18n.ts +0 -42
- package/dist/templates/react-app/config/i18n/chatMessageI18n.ts +0 -17
- package/dist/templates/react-app/config/i18n/executorI18n.ts +0 -51
- package/dist/templates/react-app/config/i18n/homeI18n.ts +0 -24
- package/dist/templates/react-app/config/i18n/i18nConfig.ts +0 -30
- package/dist/templates/react-app/config/i18n/identifiter18n.ts +0 -30
- package/dist/templates/react-app/config/i18n/jsonStorage18n.ts +0 -27
- package/dist/templates/react-app/config/i18n/login18n.ts +0 -42
- package/dist/templates/react-app/config/i18n/messageBaseListI18n.ts +0 -22
- package/dist/templates/react-app/config/i18n/messageI18n.ts +0 -14
- package/dist/templates/react-app/config/i18n/notFoundI18n.ts +0 -34
- package/dist/templates/react-app/config/i18n/register18n.ts +0 -40
- package/dist/templates/react-app/config/i18n/request18n.ts +0 -41
- package/dist/templates/react-app/config/theme.ts +0 -21
- package/dist/templates/react-app/docs/en/bootstrap.md +0 -1891
- package/dist/templates/react-app/docs/en/components/chat-message-component.md +0 -320
- package/dist/templates/react-app/docs/en/components/chat-message-refactor.md +0 -283
- package/dist/templates/react-app/docs/en/components/message-base-list-component.md +0 -171
- package/dist/templates/react-app/docs/en/development-guide.md +0 -1199
- package/dist/templates/react-app/docs/en/env.md +0 -1336
- package/dist/templates/react-app/docs/en/global.md +0 -509
- package/dist/templates/react-app/docs/en/i18n.md +0 -979
- package/dist/templates/react-app/docs/en/index.md +0 -802
- package/dist/templates/react-app/docs/en/ioc.md +0 -1365
- package/dist/templates/react-app/docs/en/playwright/e2e-tests.md +0 -321
- package/dist/templates/react-app/docs/en/playwright/index.md +0 -19
- package/dist/templates/react-app/docs/en/playwright/installation-summary.md +0 -332
- package/dist/templates/react-app/docs/en/playwright/overview.md +0 -222
- package/dist/templates/react-app/docs/en/playwright/quickstart.md +0 -325
- package/dist/templates/react-app/docs/en/playwright/reorganization-notes.md +0 -340
- package/dist/templates/react-app/docs/en/playwright/setup-complete.md +0 -290
- package/dist/templates/react-app/docs/en/playwright/testing-guide.md +0 -565
- package/dist/templates/react-app/docs/en/request.md +0 -423
- package/dist/templates/react-app/docs/en/router.md +0 -404
- package/dist/templates/react-app/docs/en/store.md +0 -1331
- package/dist/templates/react-app/docs/en/test-guide.md +0 -976
- package/dist/templates/react-app/docs/en/theme.md +0 -424
- package/dist/templates/react-app/docs/en/typescript-guide.md +0 -473
- package/dist/templates/react-app/docs/en/why-no-globals.md +0 -797
- package/dist/templates/react-app/docs/zh/bootstrap.md +0 -1891
- package/dist/templates/react-app/docs/zh/components/chat-message-component.md +0 -320
- package/dist/templates/react-app/docs/zh/components/chat-message-refactor.md +0 -283
- package/dist/templates/react-app/docs/zh/components/message-base-list-component.md +0 -171
- package/dist/templates/react-app/docs/zh/development-guide.md +0 -1199
- package/dist/templates/react-app/docs/zh/env.md +0 -1336
- package/dist/templates/react-app/docs/zh/global.md +0 -511
- package/dist/templates/react-app/docs/zh/i18n.md +0 -979
- package/dist/templates/react-app/docs/zh/index.md +0 -786
- package/dist/templates/react-app/docs/zh/ioc.md +0 -1364
- package/dist/templates/react-app/docs/zh/playwright/e2e-tests.md +0 -321
- package/dist/templates/react-app/docs/zh/playwright/index.md +0 -19
- package/dist/templates/react-app/docs/zh/playwright/installation-summary.md +0 -332
- package/dist/templates/react-app/docs/zh/playwright/overview.md +0 -222
- package/dist/templates/react-app/docs/zh/playwright/quickstart.md +0 -325
- package/dist/templates/react-app/docs/zh/playwright/reorganization-notes.md +0 -340
- package/dist/templates/react-app/docs/zh/playwright/setup-complete.md +0 -290
- package/dist/templates/react-app/docs/zh/playwright/testing-guide.md +0 -565
- package/dist/templates/react-app/docs/zh/request.md +0 -427
- package/dist/templates/react-app/docs/zh/router.md +0 -408
- package/dist/templates/react-app/docs/zh/store.md +0 -1329
- package/dist/templates/react-app/docs/zh/test-guide.md +0 -976
- package/dist/templates/react-app/docs/zh/theme.md +0 -424
- package/dist/templates/react-app/docs/zh/typescript-guide.md +0 -473
- package/dist/templates/react-app/docs/zh/why-no-globals.md +0 -797
- package/dist/templates/react-app/e2e/App.spec.ts +0 -319
- package/dist/templates/react-app/e2e/fixtures/base.fixture.ts +0 -40
- package/dist/templates/react-app/e2e/main.spec.ts +0 -20
- package/dist/templates/react-app/e2e/utils/test-helpers.ts +0 -19
- package/dist/templates/react-app/eslint.config.mjs +0 -325
- package/dist/templates/react-app/index.html +0 -13
- package/dist/templates/react-app/makes/generateTs2LocalesOptions.ts +0 -26
- package/dist/templates/react-app/package.json +0 -125
- package/dist/templates/react-app/playwright.config.ts +0 -79
- package/dist/templates/react-app/postcss.config.js +0 -5
- package/dist/templates/react-app/public/locales/en/common.json +0 -235
- package/dist/templates/react-app/public/locales/zh/common.json +0 -235
- package/dist/templates/react-app/public/logo.svg +0 -1
- package/dist/templates/react-app/public/router-root/logo.svg +0 -1
- package/dist/templates/react-app/src/App.tsx +0 -35
- package/dist/templates/react-app/src/assets/react.svg +0 -1
- package/dist/templates/react-app/src/base/apis/AiApi.ts +0 -68
- package/dist/templates/react-app/src/base/apis/feApi/FeApi.ts +0 -29
- package/dist/templates/react-app/src/base/apis/feApi/FeApiAdapter.ts +0 -14
- package/dist/templates/react-app/src/base/apis/feApi/FeApiBootstarp.ts +0 -86
- package/dist/templates/react-app/src/base/apis/feApi/FeApiType.ts +0 -21
- package/dist/templates/react-app/src/base/apis/userApi/UserApi.ts +0 -142
- package/dist/templates/react-app/src/base/apis/userApi/UserApiAdapter.ts +0 -14
- package/dist/templates/react-app/src/base/apis/userApi/UserApiBootstarp.ts +0 -86
- package/dist/templates/react-app/src/base/apis/userApi/UserApiType.ts +0 -70
- package/dist/templates/react-app/src/base/cases/AppConfig.ts +0 -123
- package/dist/templates/react-app/src/base/cases/DialogHandler.ts +0 -115
- package/dist/templates/react-app/src/base/cases/I18nKeyErrorPlugin.ts +0 -64
- package/dist/templates/react-app/src/base/cases/InversifyContainer.ts +0 -45
- package/dist/templates/react-app/src/base/cases/PublicAssetsPath.ts +0 -23
- package/dist/templates/react-app/src/base/cases/RequestLanguages.ts +0 -55
- package/dist/templates/react-app/src/base/cases/RequestLogger.ts +0 -80
- package/dist/templates/react-app/src/base/cases/RequestStatusCatcher.ts +0 -40
- package/dist/templates/react-app/src/base/cases/ResourceState.ts +0 -23
- package/dist/templates/react-app/src/base/cases/RouterLoader.ts +0 -173
- package/dist/templates/react-app/src/base/cases/TranslateI18nInterface.ts +0 -26
- package/dist/templates/react-app/src/base/port/ExecutorPageBridgeInterface.ts +0 -23
- package/dist/templates/react-app/src/base/port/I18nServiceInterface.ts +0 -10
- package/dist/templates/react-app/src/base/port/IOCInterface.ts +0 -38
- package/dist/templates/react-app/src/base/port/JSONStoragePageBridgeInterface.ts +0 -21
- package/dist/templates/react-app/src/base/port/RequestPageBridgeInterface.ts +0 -23
- package/dist/templates/react-app/src/base/port/RequestStatusInterface.ts +0 -5
- package/dist/templates/react-app/src/base/port/RouteServiceInterface.ts +0 -29
- package/dist/templates/react-app/src/base/port/UserServiceInterface.ts +0 -20
- package/dist/templates/react-app/src/base/services/BaseLayoutService.ts +0 -61
- package/dist/templates/react-app/src/base/services/I18nService.ts +0 -146
- package/dist/templates/react-app/src/base/services/IdentifierService.ts +0 -162
- package/dist/templates/react-app/src/base/services/RouteService.ts +0 -115
- package/dist/templates/react-app/src/base/services/UserBootstrap.ts +0 -45
- package/dist/templates/react-app/src/base/services/UserService.ts +0 -88
- package/dist/templates/react-app/src/base/types/Page.ts +0 -47
- package/dist/templates/react-app/src/base/types/deprecated-antd.d.ts +0 -60
- package/dist/templates/react-app/src/base/types/global.d.ts +0 -8
- package/dist/templates/react-app/src/core/IOC.ts +0 -28
- package/dist/templates/react-app/src/core/bootstraps/BootstrapClient.ts +0 -108
- package/dist/templates/react-app/src/core/bootstraps/BootstrapsRegistry.ts +0 -54
- package/dist/templates/react-app/src/core/bootstraps/IocIdentifierTest.ts +0 -26
- package/dist/templates/react-app/src/core/bootstraps/PrintBootstrap.ts +0 -14
- package/dist/templates/react-app/src/core/bootstraps/SaveAppInfo.ts +0 -28
- package/dist/templates/react-app/src/core/clientIoc/ClientIOC.ts +0 -47
- package/dist/templates/react-app/src/core/clientIoc/ClientIOCRegister.ts +0 -142
- package/dist/templates/react-app/src/core/globals.ts +0 -47
- package/dist/templates/react-app/src/main.tsx +0 -19
- package/dist/templates/react-app/src/pages/404.tsx +0 -19
- package/dist/templates/react-app/src/pages/500.tsx +0 -18
- package/dist/templates/react-app/src/pages/NoRouteFound.tsx +0 -5
- package/dist/templates/react-app/src/pages/auth/Layout.tsx +0 -27
- package/dist/templates/react-app/src/pages/auth/LoginPage.tsx +0 -166
- package/dist/templates/react-app/src/pages/auth/RegisterPage.tsx +0 -226
- package/dist/templates/react-app/src/pages/base/AboutPage.tsx +0 -123
- package/dist/templates/react-app/src/pages/base/ExecutorPage.tsx +0 -467
- package/dist/templates/react-app/src/pages/base/HomePage.tsx +0 -81
- package/dist/templates/react-app/src/pages/base/IdentifierPage.tsx +0 -117
- package/dist/templates/react-app/src/pages/base/JSONStoragePage.tsx +0 -132
- package/dist/templates/react-app/src/pages/base/Layout.tsx +0 -20
- package/dist/templates/react-app/src/pages/base/MessagePage.tsx +0 -71
- package/dist/templates/react-app/src/pages/base/RedirectPathname.tsx +0 -18
- package/dist/templates/react-app/src/pages/base/RequestPage.tsx +0 -193
- package/dist/templates/react-app/src/styles/css/antd-themes/_common/_default.css +0 -280
- package/dist/templates/react-app/src/styles/css/antd-themes/_common/dark.css +0 -233
- package/dist/templates/react-app/src/styles/css/antd-themes/_common/index.css +0 -3
- package/dist/templates/react-app/src/styles/css/antd-themes/_common/pink.css +0 -246
- package/dist/templates/react-app/src/styles/css/antd-themes/index.css +0 -4
- package/dist/templates/react-app/src/styles/css/antd-themes/menu/_default.css +0 -108
- package/dist/templates/react-app/src/styles/css/antd-themes/menu/dark.css +0 -67
- package/dist/templates/react-app/src/styles/css/antd-themes/menu/index.css +0 -3
- package/dist/templates/react-app/src/styles/css/antd-themes/menu/pink.css +0 -67
- package/dist/templates/react-app/src/styles/css/antd-themes/no-context.css +0 -34
- package/dist/templates/react-app/src/styles/css/antd-themes/pagination/_default.css +0 -34
- package/dist/templates/react-app/src/styles/css/antd-themes/pagination/dark.css +0 -31
- package/dist/templates/react-app/src/styles/css/antd-themes/pagination/index.css +0 -3
- package/dist/templates/react-app/src/styles/css/antd-themes/pagination/pink.css +0 -36
- package/dist/templates/react-app/src/styles/css/antd-themes/table/_default.css +0 -44
- package/dist/templates/react-app/src/styles/css/antd-themes/table/dark.css +0 -43
- package/dist/templates/react-app/src/styles/css/antd-themes/table/index.css +0 -3
- package/dist/templates/react-app/src/styles/css/antd-themes/table/pink.css +0 -43
- package/dist/templates/react-app/src/styles/css/index.css +0 -6
- package/dist/templates/react-app/src/styles/css/page.css +0 -20
- package/dist/templates/react-app/src/styles/css/scrollbar.css +0 -34
- package/dist/templates/react-app/src/styles/css/tailwind.css +0 -5
- package/dist/templates/react-app/src/styles/css/themes/_default.css +0 -30
- package/dist/templates/react-app/src/styles/css/themes/dark.css +0 -30
- package/dist/templates/react-app/src/styles/css/themes/index.css +0 -3
- package/dist/templates/react-app/src/styles/css/themes/pink.css +0 -30
- package/dist/templates/react-app/src/styles/css/zIndex.css +0 -9
- package/dist/templates/react-app/src/uikit/bridges/ExecutorPageBridge.ts +0 -72
- package/dist/templates/react-app/src/uikit/bridges/JSONStoragePageBridge.ts +0 -41
- package/dist/templates/react-app/src/uikit/bridges/NavigateBridge.ts +0 -22
- package/dist/templates/react-app/src/uikit/bridges/RequestPageBridge.ts +0 -136
- package/dist/templates/react-app/src/uikit/components/AppRouterProvider.tsx +0 -35
- package/dist/templates/react-app/src/uikit/components/BaseHeader.tsx +0 -51
- package/dist/templates/react-app/src/uikit/components/BaseLayoutProvider.tsx +0 -44
- package/dist/templates/react-app/src/uikit/components/BaseRouteProvider.tsx +0 -21
- package/dist/templates/react-app/src/uikit/components/BaseRouteSeo.tsx +0 -18
- package/dist/templates/react-app/src/uikit/components/BootstrapsProvider.tsx +0 -11
- package/dist/templates/react-app/src/uikit/components/ClientSeo.tsx +0 -62
- package/dist/templates/react-app/src/uikit/components/ComboProvider.tsx +0 -38
- package/dist/templates/react-app/src/uikit/components/LanguageSwitcher.tsx +0 -78
- package/dist/templates/react-app/src/uikit/components/Loading.tsx +0 -49
- package/dist/templates/react-app/src/uikit/components/LocaleLink.tsx +0 -43
- package/dist/templates/react-app/src/uikit/components/LogoutButton.tsx +0 -58
- package/dist/templates/react-app/src/uikit/components/MessageBaseList.tsx +0 -258
- package/dist/templates/react-app/src/uikit/components/RouterRenderComponent.tsx +0 -19
- package/dist/templates/react-app/src/uikit/components/ThemeSwitcher.tsx +0 -137
- package/dist/templates/react-app/src/uikit/components/With.tsx +0 -17
- package/dist/templates/react-app/src/uikit/components/chatMessage/ChatMessageBridge.ts +0 -205
- package/dist/templates/react-app/src/uikit/components/chatMessage/ChatRoot.tsx +0 -21
- package/dist/templates/react-app/src/uikit/components/chatMessage/FocusBar.tsx +0 -108
- package/dist/templates/react-app/src/uikit/components/chatMessage/MessageApi.ts +0 -282
- package/dist/templates/react-app/src/uikit/components/chatMessage/MessageItem.tsx +0 -102
- package/dist/templates/react-app/src/uikit/components/chatMessage/MessagesList.tsx +0 -86
- package/dist/templates/react-app/src/uikit/contexts/BaseRouteContext.ts +0 -42
- package/dist/templates/react-app/src/uikit/contexts/IOCContext.ts +0 -13
- package/dist/templates/react-app/src/uikit/hooks/useAppTranslation.ts +0 -26
- package/dist/templates/react-app/src/uikit/hooks/useI18nInterface.ts +0 -25
- package/dist/templates/react-app/src/uikit/hooks/useIOC.ts +0 -35
- package/dist/templates/react-app/src/uikit/hooks/useNavigateBridge.ts +0 -21
- package/dist/templates/react-app/src/uikit/hooks/useRouterI18nGuard.ts +0 -25
- package/dist/templates/react-app/src/uikit/hooks/useStrictEffect.ts +0 -31
- package/dist/templates/react-app/src/vite-env.d.ts +0 -1
- package/dist/templates/react-app/tailwind.config.js +0 -4
- package/dist/templates/react-app/tsconfig.app.json +0 -36
- package/dist/templates/react-app/tsconfig.e2e.json +0 -24
- package/dist/templates/react-app/tsconfig.json +0 -22
- package/dist/templates/react-app/tsconfig.node.json +0 -27
- package/dist/templates/react-app/tsconfig.test.json +0 -18
- package/dist/templates/react-app/vite.config.ts +0 -144
|
@@ -1,979 +0,0 @@
|
|
|
1
|
-
# 国际化 (i18n)
|
|
2
|
-
|
|
3
|
-
## 📋 目录
|
|
4
|
-
|
|
5
|
-
- [核心理念](#-核心理念) - 绝不硬编码,只用变量
|
|
6
|
-
- [什么是 i18n Key](#-什么是-i18n-key) - 唯一标识符
|
|
7
|
-
- [为什么使用 i18n Key](#-为什么使用-i18n-key) - 减少思考负担
|
|
8
|
-
- [项目中的实现](#-项目中的实现) - Bootstrap 插件
|
|
9
|
-
- [使用方式](#-使用方式) - 实战指南
|
|
10
|
-
- [最佳实践](#-最佳实践) - 7 条核心实践
|
|
11
|
-
- [常见问题](#-常见问题) - FAQ
|
|
12
|
-
|
|
13
|
-
---
|
|
14
|
-
|
|
15
|
-
## 🎯 核心理念
|
|
16
|
-
|
|
17
|
-
> **🚨 重要原则:项目中所有需要显示的文字、词语、句子,都必须使用 i18n Key,绝不允许硬编码文本!**
|
|
18
|
-
|
|
19
|
-
> **⭐ 核心优势:开发者不需要记住复杂的翻译字符串,只需要知道变量名,IDE 会提供自动补全!**
|
|
20
|
-
|
|
21
|
-
### 核心概念
|
|
22
|
-
|
|
23
|
-
```
|
|
24
|
-
❌ 硬编码文本 ✅ i18n Key(变量)
|
|
25
|
-
"登录" → BUTTON_LOGIN (不需要记住 'common:button.login')
|
|
26
|
-
"欢迎回来" → MESSAGE_WELCOME (不需要记住 'common:message.welcome')
|
|
27
|
-
"确定要删除吗?" → CONFIRM_DELETE (不需要记住 'common:confirm.delete')
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
**每个文本都有唯一的标识符:**
|
|
31
|
-
|
|
32
|
-
```typescript
|
|
33
|
-
// ❌ 错误:直接写文字或字符串
|
|
34
|
-
<button>登录</button>
|
|
35
|
-
<h1>欢迎回来</h1>
|
|
36
|
-
<p>{t('common:confirm.delete')}</p> // 也不要写字符串
|
|
37
|
-
|
|
38
|
-
// ✅ 正确:使用 i18n Key 变量
|
|
39
|
-
<button>{t(BUTTON_LOGIN)}</button> // 只需记住变量名
|
|
40
|
-
<h1>{t(MESSAGE_WELCOME)}</h1> // IDE 会自动补全
|
|
41
|
-
<p>{t(CONFIRM_DELETE)}</p> // TypeScript 会检查拼写
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
---
|
|
45
|
-
|
|
46
|
-
## 🔑 什么是 i18n Key
|
|
47
|
-
|
|
48
|
-
i18n Key 是**每个文本的唯一标识符**,就像每个人都有身份证号一样。
|
|
49
|
-
|
|
50
|
-
### 基本概念
|
|
51
|
-
|
|
52
|
-
```typescript
|
|
53
|
-
// i18n Key 定义(在 config/Identifier/ 目录下)
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @description Login button text
|
|
57
|
-
* @localZh 登录
|
|
58
|
-
* @localEn Login
|
|
59
|
-
*/
|
|
60
|
-
export const BUTTON_LOGIN = 'common:button.login';
|
|
61
|
-
// ↑ 常量名 ↑ 标识符字符串
|
|
62
|
-
// 用于代码中引用 存储在翻译文件中的键名
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* @description Welcome message
|
|
66
|
-
* @localZh 欢迎回来,{{name}}!
|
|
67
|
-
* @localEn Welcome back, {{name}}!
|
|
68
|
-
*/
|
|
69
|
-
export const MESSAGE_WELCOME = 'common:message.welcome';
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
**结构说明:**
|
|
73
|
-
|
|
74
|
-
```
|
|
75
|
-
'common:button.login'
|
|
76
|
-
↑ ↑ ↑
|
|
77
|
-
命名空间 分类 具体标识
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
### 命名规范
|
|
81
|
-
|
|
82
|
-
```typescript
|
|
83
|
-
// ✅ 好的命名:清晰、结构化
|
|
84
|
-
export const BUTTON_LOGIN = 'common:button.login'; // 按钮文本
|
|
85
|
-
export const BUTTON_SUBMIT = 'common:button.submit'; // 按钮文本
|
|
86
|
-
export const MESSAGE_WELCOME = 'common:message.welcome'; // 消息文本
|
|
87
|
-
export const CONFIRM_DELETE = 'common:confirm.delete'; // 确认对话框
|
|
88
|
-
export const ERROR_NETWORK = 'common:error.network'; // 错误信息
|
|
89
|
-
export const PAGE_HOME_TITLE = 'common:page.home.title'; // 页面标题
|
|
90
|
-
|
|
91
|
-
// ❌ 不好的命名:模糊、无结构
|
|
92
|
-
export const TEXT1 = 'login';
|
|
93
|
-
export const MSG = 'welcome';
|
|
94
|
-
export const DELETE_CONFIRM = 'delete_confirm';
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
---
|
|
98
|
-
|
|
99
|
-
## 🤔 为什么使用 i18n Key
|
|
100
|
-
|
|
101
|
-
### 问题:硬编码文本的痛点
|
|
102
|
-
|
|
103
|
-
```typescript
|
|
104
|
-
// ❌ 传统方式:文本硬编码
|
|
105
|
-
|
|
106
|
-
function LoginPage() {
|
|
107
|
-
return (
|
|
108
|
-
<div>
|
|
109
|
-
<h1>登录</h1>
|
|
110
|
-
<button>登录</button>
|
|
111
|
-
<p>欢迎回来!</p>
|
|
112
|
-
<span>忘记密码?</span>
|
|
113
|
-
</div>
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// 😰 问题 1:国际化困难
|
|
118
|
-
// 如果要支持英文,需要修改大量代码
|
|
119
|
-
|
|
120
|
-
// 😰 问题 2:维护困难
|
|
121
|
-
// 同样的文本可能在多个地方使用,修改时容易遗漏
|
|
122
|
-
|
|
123
|
-
// 😰 问题 3:难以统一管理
|
|
124
|
-
// 无法统计项目中有多少个文本,哪些需要翻译
|
|
125
|
-
|
|
126
|
-
// 😰 问题 4:搜索困难
|
|
127
|
-
// 想找到某个文本在哪里使用,只能全局搜索字符串
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
### 解决方案:使用 i18n Key
|
|
131
|
-
|
|
132
|
-
```typescript
|
|
133
|
-
// ✅ 使用 i18n Key
|
|
134
|
-
|
|
135
|
-
import { useAppTranslation } from '@/uikit/hooks/useAppTranslation';
|
|
136
|
-
import {
|
|
137
|
-
PAGE_LOGIN_TITLE,
|
|
138
|
-
BUTTON_LOGIN,
|
|
139
|
-
MESSAGE_WELCOME,
|
|
140
|
-
LINK_FORGOT_PASSWORD
|
|
141
|
-
} from '@config/Identifier';
|
|
142
|
-
|
|
143
|
-
function LoginPage() {
|
|
144
|
-
const { t } = useAppTranslation();
|
|
145
|
-
|
|
146
|
-
return (
|
|
147
|
-
<div>
|
|
148
|
-
<h1>{t(PAGE_LOGIN_TITLE)}</h1>
|
|
149
|
-
<button>{t(BUTTON_LOGIN)}</button>
|
|
150
|
-
<p>{t(MESSAGE_WELCOME)}</p>
|
|
151
|
-
<span>{t(LINK_FORGOT_PASSWORD)}</span>
|
|
152
|
-
</div>
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// ✅ 优势 1:自动国际化
|
|
157
|
-
// 根据用户语言自动显示对应文本
|
|
158
|
-
|
|
159
|
-
// ✅ 优势 2:集中管理
|
|
160
|
-
// 所有文本在 Identifier 文件中统一管理
|
|
161
|
-
|
|
162
|
-
// ✅ 优势 3:易于维护
|
|
163
|
-
// 修改文本只需修改一处
|
|
164
|
-
|
|
165
|
-
// ✅ 优势 4:类型安全
|
|
166
|
-
// TypeScript 会检查 Key 是否存在
|
|
167
|
-
|
|
168
|
-
// ✅ 优势 5:减少思考 ⭐
|
|
169
|
-
// 开发者只需要知道 BUTTON_LOGIN 是一个变量
|
|
170
|
-
// 不需要记住 'common:button.login' 这个字符串
|
|
171
|
-
// IDE 会提供自动补全,写错了会立即报错
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
### 对比总结
|
|
175
|
-
|
|
176
|
-
| 特性 | 硬编码文本 | i18n Key |
|
|
177
|
-
| ------------ | --------------------- | ----------------------- |
|
|
178
|
-
| **国际化** | ❌ 困难(需要改代码) | ✅ 自动(切换语言即可) |
|
|
179
|
-
| **维护性** | ❌ 差(分散各处) | ✅ 好(集中管理) |
|
|
180
|
-
| **重用性** | ❌ 差(复制粘贴) | ✅ 好(引用 Key) |
|
|
181
|
-
| **搜索性** | ❌ 困难(字符串搜索) | ✅ 简单(搜索常量) |
|
|
182
|
-
| **统计性** | ❌ 不可能 | ✅ 容易(统计 Key) |
|
|
183
|
-
| **类型安全** | ❌ 没有 | ✅ 有(TypeScript) |
|
|
184
|
-
| **心智负担** | ❌ 高(需记住文字) | ✅ 低(只需记变量名) |
|
|
185
|
-
| **IDE 支持** | ❌ 无自动补全 | ✅ 完整自动补全 |
|
|
186
|
-
|
|
187
|
-
### ⭐ 核心优势:减少开发者思考
|
|
188
|
-
|
|
189
|
-
**最重要的优势:开发者不需要关心具体的 i18n 字符串是什么!**
|
|
190
|
-
|
|
191
|
-
```typescript
|
|
192
|
-
// ❌ 传统方式:需要记住复杂的字符串
|
|
193
|
-
function LoginPage() {
|
|
194
|
-
const { t } = useTranslation();
|
|
195
|
-
|
|
196
|
-
return (
|
|
197
|
-
<div>
|
|
198
|
-
{/* 😰 需要记住 'common:button.login' */}
|
|
199
|
-
<button>{t('common:button.login')}</button>
|
|
200
|
-
|
|
201
|
-
{/* 😰 需要记住 'page.login.title' */}
|
|
202
|
-
<h1>{t('page.login.title')}</h1>
|
|
203
|
-
|
|
204
|
-
{/* 😰 写错了也不会报错 */}
|
|
205
|
-
<p>{t('page.login.welcom')}</p> {/* welcom → welcome,拼写错误 */}
|
|
206
|
-
</div>
|
|
207
|
-
);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// ✅ i18n Key 方式:只需要知道变量名
|
|
211
|
-
import { BUTTON_LOGIN, PAGE_LOGIN_TITLE, PAGE_LOGIN_WELCOME } from '@config/Identifier';
|
|
212
|
-
|
|
213
|
-
function LoginPage() {
|
|
214
|
-
const { t } = useAppTranslation();
|
|
215
|
-
|
|
216
|
-
return (
|
|
217
|
-
<div>
|
|
218
|
-
{/* ✅ 只需要记住 BUTTON_LOGIN,IDE 会自动补全 */}
|
|
219
|
-
<button>{t(BUTTON_LOGIN)}</button>
|
|
220
|
-
|
|
221
|
-
{/* ✅ 输入 PAGE_ 后,IDE 会提示所有可用的 Key */}
|
|
222
|
-
<h1>{t(PAGE_LOGIN_TITLE)}</h1>
|
|
223
|
-
|
|
224
|
-
{/* ✅ 写错了 TypeScript 会立即报错 */}
|
|
225
|
-
<p>{t(PAGE_LOGIN_WELCOM)}</p> {/* ❌ TS 错误:找不到变量 */}
|
|
226
|
-
</div>
|
|
227
|
-
);
|
|
228
|
-
}
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
**开发体验对比:**
|
|
232
|
-
|
|
233
|
-
```typescript
|
|
234
|
-
// ❌ 使用字符串的开发流程:
|
|
235
|
-
// 1. 😰 查看翻译文件,找到对应的 key
|
|
236
|
-
// 2. 😰 记住 key 的完整路径(如 'common:page.login.title')
|
|
237
|
-
// 3. 😰 在代码中手动输入字符串
|
|
238
|
-
// 4. 😰 可能输入错误,但编译器不会报错
|
|
239
|
-
// 5. 😰 运行时才发现翻译没有生效
|
|
240
|
-
|
|
241
|
-
// ✅ 使用 i18n Key 的开发流程:
|
|
242
|
-
// 1. 😊 输入变量名前缀(如 BUTTON_)
|
|
243
|
-
// 2. 😊 IDE 自动提示所有可用的 Key
|
|
244
|
-
// 3. 😊 选择需要的 Key,IDE 自动补全
|
|
245
|
-
// 4. 😊 如果输入错误,TypeScript 立即报错
|
|
246
|
-
// 5. 😊 编译期就能确保 Key 的正确性
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
**认知负担对比:**
|
|
250
|
-
|
|
251
|
-
| 开发者需要记住的内容 | 字符串方式 | i18n Key 方式 |
|
|
252
|
-
| -------------------- | --------------------------------------- | --------------- |
|
|
253
|
-
| **翻译文件结构** | ✅ 必须记住(如 `common:button.login`) | ❌ 不需要记 |
|
|
254
|
-
| **命名空间** | ✅ 必须记住(如 `common:`, `page:`) | ❌ 不需要记 |
|
|
255
|
-
| **字符串路径** | ✅ 必须记住完整路径 | ❌ 不需要记 |
|
|
256
|
-
| **变量名** | ❌ 没有变量 | ✅ 只需记变量名 |
|
|
257
|
-
| **IDE 提示** | ❌ 没有 | ✅ 完整提示 |
|
|
258
|
-
|
|
259
|
-
**实际开发场景:**
|
|
260
|
-
|
|
261
|
-
```typescript
|
|
262
|
-
// 场景 1:新写一个按钮
|
|
263
|
-
|
|
264
|
-
// ❌ 字符串方式(需要思考很多)
|
|
265
|
-
// 1. 这个按钮的翻译 key 应该叫什么?
|
|
266
|
-
// 2. 应该放在哪个命名空间?common? page?
|
|
267
|
-
// 3. 路径应该是什么?button.submit? submit.button?
|
|
268
|
-
// 4. 最终写出:t('common:button.submit')
|
|
269
|
-
<button>{t('common:button.submit')}</button>
|
|
270
|
-
|
|
271
|
-
// ✅ i18n Key 方式(只需要搜索)
|
|
272
|
-
// 1. 输入 BUTTON_
|
|
273
|
-
// 2. IDE 显示所有可用的按钮 Key
|
|
274
|
-
// 3. 看到 BUTTON_SUBMIT,选择它
|
|
275
|
-
// 4. 完成!
|
|
276
|
-
<button>{t(BUTTON_SUBMIT)}</button>
|
|
277
|
-
|
|
278
|
-
// 场景 2:要改另一个页面的文字
|
|
279
|
-
|
|
280
|
-
// ❌ 字符串方式
|
|
281
|
-
// 1. 查看那个页面的代码
|
|
282
|
-
// 2. 找到 t('page.home.welcome')
|
|
283
|
-
// 3. 记住这个字符串
|
|
284
|
-
// 4. 在翻译文件中搜索并修改
|
|
285
|
-
|
|
286
|
-
// ✅ i18n Key 方式
|
|
287
|
-
// 1. 看到代码中的 PAGE_HOME_WELCOME
|
|
288
|
-
// 2. 直接跳转到定义(IDE 的 Go to Definition)
|
|
289
|
-
// 3. 修改注释中的翻译
|
|
290
|
-
// 4. 自动生成新的翻译文件
|
|
291
|
-
```
|
|
292
|
-
|
|
293
|
-
**总结:**
|
|
294
|
-
|
|
295
|
-
使用 i18n Key 让开发者:
|
|
296
|
-
|
|
297
|
-
- ✅ **不需要记住** 复杂的字符串路径
|
|
298
|
-
- ✅ **不需要记住** 翻译文件的结构
|
|
299
|
-
- ✅ **不需要关心** 具体的翻译键名
|
|
300
|
-
- ✅ **只需要知道** 这是一个变量
|
|
301
|
-
- ✅ **依靠 IDE** 提供的自动补全和类型检查
|
|
302
|
-
- ✅ **专注于** 业务逻辑,而不是翻译细节
|
|
303
|
-
|
|
304
|
-
> 💡 **核心理念:让开发者把精力用在业务逻辑上,而不是记忆翻译字符串!**
|
|
305
|
-
|
|
306
|
-
---
|
|
307
|
-
|
|
308
|
-
## 🛠️ 项目中的实现
|
|
309
|
-
|
|
310
|
-
### 1. 文件结构
|
|
311
|
-
|
|
312
|
-
```
|
|
313
|
-
config/
|
|
314
|
-
└── Identifier/ # i18n Key 定义目录
|
|
315
|
-
├── index.ts # 导出所有 Key
|
|
316
|
-
├── common/ # 公共文本
|
|
317
|
-
│ ├── index.ts
|
|
318
|
-
│ ├── common.ts # 通用文本(按钮、消息等)
|
|
319
|
-
│ └── common.error.ts # 错误信息
|
|
320
|
-
└── pages/ # 页面文本
|
|
321
|
-
├── index.ts
|
|
322
|
-
├── page.home.ts # 首页文本
|
|
323
|
-
├── page.login.ts # 登录页文本
|
|
324
|
-
└── page.about.ts # 关于页文本
|
|
325
|
-
|
|
326
|
-
public/
|
|
327
|
-
└── locales/ # 生成的翻译文件
|
|
328
|
-
├── zh/
|
|
329
|
-
│ └── common.json # 中文翻译
|
|
330
|
-
└── en/
|
|
331
|
-
└── common.json # 英文翻译
|
|
332
|
-
|
|
333
|
-
src/
|
|
334
|
-
└── uikit/
|
|
335
|
-
└── hooks/
|
|
336
|
-
└── useAppTranslation.ts # 翻译 Hook
|
|
337
|
-
```
|
|
338
|
-
|
|
339
|
-
### 2. i18n 配置
|
|
340
|
-
|
|
341
|
-
```typescript
|
|
342
|
-
// config/i18n/i18nConfig.ts
|
|
343
|
-
export const i18nConfig = {
|
|
344
|
-
fallbackLng: 'en', // 默认语言
|
|
345
|
-
debug: false,
|
|
346
|
-
interpolation: {
|
|
347
|
-
escapeValue: false
|
|
348
|
-
},
|
|
349
|
-
ns: ['common'], // 命名空间
|
|
350
|
-
defaultNS: 'common',
|
|
351
|
-
backend: {
|
|
352
|
-
loadPath: '/locales/{{lng}}/{{ns}}.json' // 翻译文件路径
|
|
353
|
-
},
|
|
354
|
-
supportedLngs: ['en', 'zh'] // 支持的语言
|
|
355
|
-
} as const;
|
|
356
|
-
```
|
|
357
|
-
|
|
358
|
-
### 3. I18nService(Bootstrap 插件)
|
|
359
|
-
|
|
360
|
-
```typescript
|
|
361
|
-
// src/base/services/I18nService.ts
|
|
362
|
-
export class I18nService implements BootstrapExecutorPlugin {
|
|
363
|
-
readonly pluginName = 'I18nService';
|
|
364
|
-
|
|
365
|
-
constructor(protected pathname: string) {
|
|
366
|
-
super(() => new I18nServiceState(i18n.language));
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
/**
|
|
370
|
-
* 在 Bootstrap 启动时初始化 i18n
|
|
371
|
-
*/
|
|
372
|
-
onBefore(): void {
|
|
373
|
-
i18n
|
|
374
|
-
.use(HttpApi) // 加载翻译文件
|
|
375
|
-
.use(LanguageDetector) // 语言检测
|
|
376
|
-
.use(initReactI18next) // React 集成
|
|
377
|
-
.init(i18nConfig); // 初始化配置
|
|
378
|
-
|
|
379
|
-
// 添加自定义语言检测器(从 URL 路径检测)
|
|
380
|
-
const pathLanguageDetector = {
|
|
381
|
-
name: 'pathLanguageDetector',
|
|
382
|
-
lookup: () => {
|
|
383
|
-
const paths = this.pathname.split('/');
|
|
384
|
-
for (const path of paths) {
|
|
385
|
-
if (this.isValidLanguage(path)) {
|
|
386
|
-
return path; // 从 URL 中提取语言(如 /zh/home)
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
return fallbackLng;
|
|
390
|
-
},
|
|
391
|
-
cacheUserLanguage(lng: string) {
|
|
392
|
-
localStorage.setItem('i18nextLng', lng);
|
|
393
|
-
}
|
|
394
|
-
};
|
|
395
|
-
|
|
396
|
-
i18n.services.languageDetector.addDetector(pathLanguageDetector);
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* 切换语言
|
|
401
|
-
*/
|
|
402
|
-
async changeLanguage(language: string): Promise<void> {
|
|
403
|
-
await i18n.changeLanguage(language);
|
|
404
|
-
localStorage.setItem('i18nextLng', language);
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
/**
|
|
408
|
-
* 翻译文本
|
|
409
|
-
*/
|
|
410
|
-
t(key: string, params?: Record<string, unknown>): string {
|
|
411
|
-
return i18n.t(key, { lng: i18n.language, ...params });
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
```
|
|
415
|
-
|
|
416
|
-
### 4. 自动生成翻译文件
|
|
417
|
-
|
|
418
|
-
```typescript
|
|
419
|
-
// vite.config.ts
|
|
420
|
-
import ts2Locales from '@brain-toolkit/ts2locales/vite';
|
|
421
|
-
|
|
422
|
-
export default defineConfig({
|
|
423
|
-
plugins: [
|
|
424
|
-
// ✅ ts2locales 插件:自动从 TypeScript 注释生成翻译文件
|
|
425
|
-
ts2Locales({
|
|
426
|
-
locales: ['en', 'zh'],
|
|
427
|
-
options: generateTs2LocalesOptions() // 配置生成规则
|
|
428
|
-
})
|
|
429
|
-
]
|
|
430
|
-
});
|
|
431
|
-
```
|
|
432
|
-
|
|
433
|
-
**工作原理:**
|
|
434
|
-
|
|
435
|
-
```typescript
|
|
436
|
-
// 1. 在 Identifier 文件中定义 Key
|
|
437
|
-
/**
|
|
438
|
-
* @description Login button text
|
|
439
|
-
* @localZh 登录
|
|
440
|
-
* @localEn Login
|
|
441
|
-
*/
|
|
442
|
-
export const BUTTON_LOGIN = 'common:button.login';
|
|
443
|
-
|
|
444
|
-
// 2. ts2locales 插件自动生成翻译文件
|
|
445
|
-
|
|
446
|
-
// public/locales/zh/common.json
|
|
447
|
-
{
|
|
448
|
-
"button": {
|
|
449
|
-
"login": "登录"
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
// public/locales/en/common.json
|
|
454
|
-
{
|
|
455
|
-
"button": {
|
|
456
|
-
"login": "Login"
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
```
|
|
460
|
-
|
|
461
|
-
---
|
|
462
|
-
|
|
463
|
-
## 📝 使用方式
|
|
464
|
-
|
|
465
|
-
### 1. 定义 i18n Key
|
|
466
|
-
|
|
467
|
-
```typescript
|
|
468
|
-
// config/Identifier/pages/page.home.ts
|
|
469
|
-
|
|
470
|
-
/**
|
|
471
|
-
* @description Home page title
|
|
472
|
-
* @localZh 首页
|
|
473
|
-
* @localEn Home
|
|
474
|
-
*/
|
|
475
|
-
export const PAGE_HOME_TITLE = 'common:page.home.title';
|
|
476
|
-
|
|
477
|
-
/**
|
|
478
|
-
* @description Home page welcome message
|
|
479
|
-
* @localZh 欢迎来到我们的应用!
|
|
480
|
-
* @localEn Welcome to our application!
|
|
481
|
-
*/
|
|
482
|
-
export const PAGE_HOME_WELCOME = 'common:page.home.welcome';
|
|
483
|
-
|
|
484
|
-
/**
|
|
485
|
-
* @description Home page description with user name
|
|
486
|
-
* @localZh 你好,{{name}}!今天是个好日子。
|
|
487
|
-
* @localEn Hello, {{name}}! Have a great day.
|
|
488
|
-
*/
|
|
489
|
-
export const PAGE_HOME_GREETING = 'common:page.home.greeting';
|
|
490
|
-
```
|
|
491
|
-
|
|
492
|
-
**注释规范:**
|
|
493
|
-
|
|
494
|
-
- `@description`:说明文本用途(英文)
|
|
495
|
-
- `@localZh`:中文翻译
|
|
496
|
-
- `@localEn`:英文翻译
|
|
497
|
-
|
|
498
|
-
### 2. 在 UI 组件中使用
|
|
499
|
-
|
|
500
|
-
```typescript
|
|
501
|
-
// src/pages/base/HomePage.tsx
|
|
502
|
-
import { useAppTranslation } from '@/uikit/hooks/useAppTranslation';
|
|
503
|
-
import {
|
|
504
|
-
PAGE_HOME_TITLE,
|
|
505
|
-
PAGE_HOME_WELCOME,
|
|
506
|
-
PAGE_HOME_GREETING
|
|
507
|
-
} from '@config/Identifier';
|
|
508
|
-
|
|
509
|
-
function HomePage() {
|
|
510
|
-
const { t } = useAppTranslation();
|
|
511
|
-
const userName = 'John';
|
|
512
|
-
|
|
513
|
-
return (
|
|
514
|
-
<div>
|
|
515
|
-
{/* ✅ 简单文本 */}
|
|
516
|
-
<h1>{t(PAGE_HOME_TITLE)}</h1>
|
|
517
|
-
<p>{t(PAGE_HOME_WELCOME)}</p>
|
|
518
|
-
|
|
519
|
-
{/* ✅ 带参数的文本 */}
|
|
520
|
-
<p>{t(PAGE_HOME_GREETING, { name: userName })}</p>
|
|
521
|
-
</div>
|
|
522
|
-
);
|
|
523
|
-
}
|
|
524
|
-
```
|
|
525
|
-
|
|
526
|
-
### 3. 在服务中使用
|
|
527
|
-
|
|
528
|
-
```typescript
|
|
529
|
-
// src/base/services/UserService.ts
|
|
530
|
-
import { injectable, inject } from 'inversify';
|
|
531
|
-
import { I18nServiceInterface } from '@/base/port/I18nServiceInterface';
|
|
532
|
-
import { ERROR_USER_NOT_FOUND } from '@config/Identifier';
|
|
533
|
-
|
|
534
|
-
@injectable()
|
|
535
|
-
export class UserService {
|
|
536
|
-
constructor(
|
|
537
|
-
@inject(IOCIdentifier.I18nServiceInterface)
|
|
538
|
-
private i18n: I18nServiceInterface
|
|
539
|
-
) {}
|
|
540
|
-
|
|
541
|
-
async getUser(id: string) {
|
|
542
|
-
const user = await this.api.getUserById(id);
|
|
543
|
-
|
|
544
|
-
if (!user) {
|
|
545
|
-
// ✅ 在服务中使用 i18n
|
|
546
|
-
throw new Error(this.i18n.t(ERROR_USER_NOT_FOUND, { id }));
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
return user;
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
```
|
|
553
|
-
|
|
554
|
-
### 4. 切换语言
|
|
555
|
-
|
|
556
|
-
```typescript
|
|
557
|
-
// src/uikit/components/LanguageSwitcher.tsx
|
|
558
|
-
import { useIOC } from '@/uikit/hooks/useIOC';
|
|
559
|
-
|
|
560
|
-
function LanguageSwitcher() {
|
|
561
|
-
const i18nService = useIOC('I18nServiceInterface');
|
|
562
|
-
const currentLang = i18nService.getCurrentLanguage();
|
|
563
|
-
|
|
564
|
-
const handleChange = (lang: 'zh' | 'en') => {
|
|
565
|
-
// ✅ 切换语言
|
|
566
|
-
i18nService.changeLanguage(lang);
|
|
567
|
-
};
|
|
568
|
-
|
|
569
|
-
return (
|
|
570
|
-
<select value={currentLang} onChange={(e) => handleChange(e.target.value)}>
|
|
571
|
-
<option value="zh">中文</option>
|
|
572
|
-
<option value="en">English</option>
|
|
573
|
-
</select>
|
|
574
|
-
);
|
|
575
|
-
}
|
|
576
|
-
```
|
|
577
|
-
|
|
578
|
-
### 5. 复杂场景
|
|
579
|
-
|
|
580
|
-
#### 场景 1:动态文本
|
|
581
|
-
|
|
582
|
-
```typescript
|
|
583
|
-
/**
|
|
584
|
-
* @description Upload progress message
|
|
585
|
-
* @localZh 已上传 {{current}} / {{total}} 个文件
|
|
586
|
-
* @localEn Uploaded {{current}} / {{total}} files
|
|
587
|
-
*/
|
|
588
|
-
export const UPLOAD_PROGRESS = 'common:upload.progress';
|
|
589
|
-
|
|
590
|
-
// 使用
|
|
591
|
-
<p>{t(UPLOAD_PROGRESS, { current: 3, total: 10 })}</p>
|
|
592
|
-
// 中文:已上传 3 / 10 个文件
|
|
593
|
-
// 英文:Uploaded 3 / 10 files
|
|
594
|
-
```
|
|
595
|
-
|
|
596
|
-
#### 场景 2:复数形式
|
|
597
|
-
|
|
598
|
-
```typescript
|
|
599
|
-
/**
|
|
600
|
-
* @description Files count message
|
|
601
|
-
* @localZh {{count}} 个文件
|
|
602
|
-
* @localEn {{count}} file_plural
|
|
603
|
-
*/
|
|
604
|
-
export const FILES_COUNT = 'common:files.count';
|
|
605
|
-
|
|
606
|
-
// 使用
|
|
607
|
-
<p>{t(FILES_COUNT, { count: 1 })}</p> // 1 file
|
|
608
|
-
<p>{t(FILES_COUNT, { count: 5 })}</p> // 5 files
|
|
609
|
-
```
|
|
610
|
-
|
|
611
|
-
#### 场景 3:HTML 内容
|
|
612
|
-
|
|
613
|
-
```typescript
|
|
614
|
-
/**
|
|
615
|
-
* @description Terms of service agreement
|
|
616
|
-
* @localZh 我已阅读并同意<a href="/terms">服务条款</a>
|
|
617
|
-
* @localEn I have read and agree to the <a href="/terms">Terms of Service</a>
|
|
618
|
-
*/
|
|
619
|
-
export const TERMS_AGREEMENT = 'common:terms.agreement';
|
|
620
|
-
|
|
621
|
-
// 使用(React 中需要使用 Trans 组件)
|
|
622
|
-
import { Trans } from 'react-i18next';
|
|
623
|
-
|
|
624
|
-
<Trans i18nKey={TERMS_AGREEMENT}>
|
|
625
|
-
I have read and agree to the <a href="/terms">Terms of Service</a>
|
|
626
|
-
</Trans>
|
|
627
|
-
```
|
|
628
|
-
|
|
629
|
-
---
|
|
630
|
-
|
|
631
|
-
## 💎 最佳实践
|
|
632
|
-
|
|
633
|
-
### 1. ✅ 所有文本都使用 i18n Key
|
|
634
|
-
|
|
635
|
-
```typescript
|
|
636
|
-
// ❌ 错误:混用硬编码和 i18n Key
|
|
637
|
-
function LoginForm() {
|
|
638
|
-
return (
|
|
639
|
-
<form>
|
|
640
|
-
<h1>{t(PAGE_LOGIN_TITLE)}</h1>
|
|
641
|
-
<button>登录</button> {/* ❌ 硬编码 */}
|
|
642
|
-
<a href="/forgot">忘记密码?</a> {/* ❌ 硬编码 */}
|
|
643
|
-
</form>
|
|
644
|
-
);
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
// ✅ 正确:所有文本都使用 i18n Key
|
|
648
|
-
function LoginForm() {
|
|
649
|
-
return (
|
|
650
|
-
<form>
|
|
651
|
-
<h1>{t(PAGE_LOGIN_TITLE)}</h1>
|
|
652
|
-
<button>{t(BUTTON_LOGIN)}</button> {/* ✅ 使用 Key */}
|
|
653
|
-
<a href="/forgot">{t(LINK_FORGOT_PASSWORD)}</a> {/* ✅ 使用 Key */}
|
|
654
|
-
</form>
|
|
655
|
-
);
|
|
656
|
-
}
|
|
657
|
-
```
|
|
658
|
-
|
|
659
|
-
### 2. ✅ 合理组织 i18n Key
|
|
660
|
-
|
|
661
|
-
```typescript
|
|
662
|
-
// ✅ 好的组织:按功能模块分类
|
|
663
|
-
config/Identifier/
|
|
664
|
-
├── common/
|
|
665
|
-
│ ├── common.ts # 通用文本(按钮、标签等)
|
|
666
|
-
│ └── common.error.ts # 错误信息
|
|
667
|
-
├── pages/
|
|
668
|
-
│ ├── page.home.ts # 首页文本
|
|
669
|
-
│ ├── page.login.ts # 登录页文本
|
|
670
|
-
│ └── page.about.ts # 关于页文本
|
|
671
|
-
└── components/
|
|
672
|
-
├── component.header.ts # 头部组件文本
|
|
673
|
-
└── component.footer.ts # 底部组件文本
|
|
674
|
-
```
|
|
675
|
-
|
|
676
|
-
### 3. ✅ 使用语义化的命名
|
|
677
|
-
|
|
678
|
-
```typescript
|
|
679
|
-
// ✅ 好的命名:清晰表达含义
|
|
680
|
-
export const BUTTON_SUBMIT = 'common:button.submit';
|
|
681
|
-
export const BUTTON_CANCEL = 'common:button.cancel';
|
|
682
|
-
export const ERROR_NETWORK = 'common:error.network';
|
|
683
|
-
export const MESSAGE_SUCCESS = 'common:message.success';
|
|
684
|
-
|
|
685
|
-
// ❌ 不好的命名:含义不清
|
|
686
|
-
export const BTN1 = 'btn1';
|
|
687
|
-
export const TEXT = 'text';
|
|
688
|
-
export const MSG = 'msg';
|
|
689
|
-
```
|
|
690
|
-
|
|
691
|
-
### 4. ✅ 为 i18n Key 添加清晰的注释
|
|
692
|
-
|
|
693
|
-
```typescript
|
|
694
|
-
// ✅ 好的注释:清晰说明用途和上下文
|
|
695
|
-
/**
|
|
696
|
-
* @description Confirmation message when user tries to delete an item
|
|
697
|
-
* @localZh 确定要删除 {{itemName}} 吗?此操作不可撤销。
|
|
698
|
-
* @localEn Are you sure you want to delete {{itemName}}? This action cannot be undone.
|
|
699
|
-
*/
|
|
700
|
-
export const CONFIRM_DELETE_ITEM = 'common:confirm.delete.item';
|
|
701
|
-
|
|
702
|
-
// ❌ 不好的注释:没有上下文信息
|
|
703
|
-
/**
|
|
704
|
-
* @description Delete
|
|
705
|
-
* @localZh 删除
|
|
706
|
-
* @localEn Delete
|
|
707
|
-
*/
|
|
708
|
-
export const DELETE = 'delete';
|
|
709
|
-
```
|
|
710
|
-
|
|
711
|
-
### 5. ✅ 统一命名规范
|
|
712
|
-
|
|
713
|
-
```typescript
|
|
714
|
-
// ✅ 推荐的命名规范:
|
|
715
|
-
|
|
716
|
-
// 按钮文本
|
|
717
|
-
export const BUTTON_LOGIN = 'common:button.login';
|
|
718
|
-
export const BUTTON_SUBMIT = 'common:button.submit';
|
|
719
|
-
export const BUTTON_CANCEL = 'common:button.cancel';
|
|
720
|
-
|
|
721
|
-
// 页面标题
|
|
722
|
-
export const PAGE_HOME_TITLE = 'common:page.home.title';
|
|
723
|
-
export const PAGE_ABOUT_TITLE = 'common:page.about.title';
|
|
724
|
-
|
|
725
|
-
// 错误信息
|
|
726
|
-
export const ERROR_NETWORK = 'common:error.network';
|
|
727
|
-
export const ERROR_INVALID_INPUT = 'common:error.invalid.input';
|
|
728
|
-
|
|
729
|
-
// 消息提示
|
|
730
|
-
export const MESSAGE_SUCCESS = 'common:message.success';
|
|
731
|
-
export const MESSAGE_WARNING = 'common:message.warning';
|
|
732
|
-
|
|
733
|
-
// 确认对话框
|
|
734
|
-
export const CONFIRM_DELETE = 'common:confirm.delete';
|
|
735
|
-
export const CONFIRM_LOGOUT = 'common:confirm.logout';
|
|
736
|
-
|
|
737
|
-
// 链接文本
|
|
738
|
-
export const LINK_FORGOT_PASSWORD = 'common:link.forgot.password';
|
|
739
|
-
export const LINK_PRIVACY_POLICY = 'common:link.privacy.policy';
|
|
740
|
-
```
|
|
741
|
-
|
|
742
|
-
### 6. ✅ 避免重复定义
|
|
743
|
-
|
|
744
|
-
```typescript
|
|
745
|
-
// ❌ 错误:重复定义相同的文本
|
|
746
|
-
// config/Identifier/common/common.ts
|
|
747
|
-
export const BUTTON_OK_1 = 'common:button.ok1';
|
|
748
|
-
|
|
749
|
-
// config/Identifier/pages/page.home.ts
|
|
750
|
-
export const BUTTON_OK_2 = 'common:button.ok2';
|
|
751
|
-
|
|
752
|
-
// ✅ 正确:在公共文件中定义一次,多处引用
|
|
753
|
-
// config/Identifier/common/common.ts
|
|
754
|
-
export const BUTTON_OK = 'common:button.ok';
|
|
755
|
-
|
|
756
|
-
// 在各个页面中引用
|
|
757
|
-
import { BUTTON_OK } from '@config/Identifier';
|
|
758
|
-
```
|
|
759
|
-
|
|
760
|
-
### 7. ✅ 使用参数化文本
|
|
761
|
-
|
|
762
|
-
```typescript
|
|
763
|
-
// ❌ 不好:为每种情况定义不同的 Key
|
|
764
|
-
export const WELCOME_USER_JOHN = 'common:welcome.john';
|
|
765
|
-
export const WELCOME_USER_MARY = 'common:welcome.mary';
|
|
766
|
-
|
|
767
|
-
// ✅ 好:使用参数
|
|
768
|
-
/**
|
|
769
|
-
* @description Welcome message with user name
|
|
770
|
-
* @localZh 欢迎,{{name}}!
|
|
771
|
-
* @localEn Welcome, {{name}}!
|
|
772
|
-
*/
|
|
773
|
-
export const WELCOME_USER = 'common:welcome.user';
|
|
774
|
-
|
|
775
|
-
// 使用
|
|
776
|
-
<h1>{t(WELCOME_USER, { name: 'John' })}</h1>
|
|
777
|
-
<h1>{t(WELCOME_USER, { name: 'Mary' })}</h1>
|
|
778
|
-
```
|
|
779
|
-
|
|
780
|
-
---
|
|
781
|
-
|
|
782
|
-
## ❓ 常见问题
|
|
783
|
-
|
|
784
|
-
### Q1: 为什么不能直接写文字?
|
|
785
|
-
|
|
786
|
-
**A:**
|
|
787
|
-
|
|
788
|
-
```typescript
|
|
789
|
-
// ❌ 直接写文字的问题
|
|
790
|
-
<button>登录</button>
|
|
791
|
-
|
|
792
|
-
// 问题 1:无法国际化
|
|
793
|
-
// 如果用户切换到英文,文字不会改变
|
|
794
|
-
|
|
795
|
-
// 问题 2:难以维护
|
|
796
|
-
// 如果要把"登录"改成"立即登录",需要找到所有地方修改
|
|
797
|
-
|
|
798
|
-
// 问题 3:无法统一管理
|
|
799
|
-
// 无法知道项目中有多少个"登录"按钮
|
|
800
|
-
|
|
801
|
-
// ✅ 使用 i18n Key 的优势
|
|
802
|
-
<button>{t(BUTTON_LOGIN)}</button>
|
|
803
|
-
|
|
804
|
-
// ✅ 自动国际化:切换语言自动显示对应文本
|
|
805
|
-
// ✅ 易于维护:只需修改一处定义
|
|
806
|
-
// ✅ 统一管理:所有文本集中在 Identifier 文件中
|
|
807
|
-
```
|
|
808
|
-
|
|
809
|
-
### Q2: 如何知道某个 Key 在哪里使用?
|
|
810
|
-
|
|
811
|
-
**A:**
|
|
812
|
-
|
|
813
|
-
```bash
|
|
814
|
-
# 搜索常量名
|
|
815
|
-
grep -r "BUTTON_LOGIN" src/
|
|
816
|
-
|
|
817
|
-
# 或者在 IDE 中使用 "Find Usages" 功能
|
|
818
|
-
# 可以快速找到所有使用该 Key 的地方
|
|
819
|
-
```
|
|
820
|
-
|
|
821
|
-
### Q3: 忘记定义 i18n Key 怎么办?
|
|
822
|
-
|
|
823
|
-
**A:** TypeScript 会在编译时报错:
|
|
824
|
-
|
|
825
|
-
```typescript
|
|
826
|
-
// ❌ 使用未定义的 Key
|
|
827
|
-
<button>{t(BUTTON_NOT_EXIST)}</button>
|
|
828
|
-
// TypeScript 错误:Cannot find name 'BUTTON_NOT_EXIST'
|
|
829
|
-
|
|
830
|
-
// ✅ 正确:先定义 Key
|
|
831
|
-
export const BUTTON_NOT_EXIST = 'common:button.not.exist';
|
|
832
|
-
```
|
|
833
|
-
|
|
834
|
-
### Q4: 如何处理动态文本?
|
|
835
|
-
|
|
836
|
-
**A:** 使用参数:
|
|
837
|
-
|
|
838
|
-
```typescript
|
|
839
|
-
/**
|
|
840
|
-
* @description User greeting with name and time
|
|
841
|
-
* @localZh {{name}},{{time}}好!
|
|
842
|
-
* @localEn Good {{time}}, {{name}}!
|
|
843
|
-
*/
|
|
844
|
-
export const GREETING_WITH_TIME = 'common:greeting.with.time';
|
|
845
|
-
|
|
846
|
-
// 使用
|
|
847
|
-
<h1>{t(GREETING_WITH_TIME, { name: 'John', time: 'morning' })}</h1>
|
|
848
|
-
// 中文:John,morning好!
|
|
849
|
-
// 英文:Good morning, John!
|
|
850
|
-
```
|
|
851
|
-
|
|
852
|
-
### Q5: 翻译文件是如何生成的?
|
|
853
|
-
|
|
854
|
-
**A:**
|
|
855
|
-
|
|
856
|
-
```
|
|
857
|
-
1. 开发者在 Identifier 文件中定义 Key 和翻译注释
|
|
858
|
-
↓
|
|
859
|
-
2. ts2locales 插件扫描 TypeScript 文件
|
|
860
|
-
↓
|
|
861
|
-
3. 从注释中提取 @localZh 和 @localEn
|
|
862
|
-
↓
|
|
863
|
-
4. 自动生成 public/locales/{lng}/common.json
|
|
864
|
-
↓
|
|
865
|
-
5. i18next 在运行时加载这些 JSON 文件
|
|
866
|
-
```
|
|
867
|
-
|
|
868
|
-
### Q6: 可以在代码中直接使用翻译后的文本吗?
|
|
869
|
-
|
|
870
|
-
**A:**
|
|
871
|
-
|
|
872
|
-
```typescript
|
|
873
|
-
// ❌ 不推荐:跳过 i18n 系统
|
|
874
|
-
const loginText = '登录'; // 硬编码
|
|
875
|
-
<button>{loginText}</button>
|
|
876
|
-
|
|
877
|
-
// ✅ 推荐:始终使用 i18n Key
|
|
878
|
-
const loginText = t(BUTTON_LOGIN); // 通过 i18n 系统
|
|
879
|
-
<button>{loginText}</button>
|
|
880
|
-
```
|
|
881
|
-
|
|
882
|
-
### Q7: 如何测试 i18n?
|
|
883
|
-
|
|
884
|
-
**A:**
|
|
885
|
-
|
|
886
|
-
```typescript
|
|
887
|
-
// __tests__/i18n.test.ts
|
|
888
|
-
import { I18nService } from '@/base/services/I18nService';
|
|
889
|
-
import { BUTTON_LOGIN } from '@config/Identifier';
|
|
890
|
-
|
|
891
|
-
describe('I18n', () => {
|
|
892
|
-
let i18nService: I18nService;
|
|
893
|
-
|
|
894
|
-
beforeEach(() => {
|
|
895
|
-
i18nService = new I18nService('/zh/home');
|
|
896
|
-
i18nService.onBefore();
|
|
897
|
-
});
|
|
898
|
-
|
|
899
|
-
it('should translate to Chinese', () => {
|
|
900
|
-
i18nService.changeLanguage('zh');
|
|
901
|
-
expect(i18nService.t(BUTTON_LOGIN)).toBe('登录');
|
|
902
|
-
});
|
|
903
|
-
|
|
904
|
-
it('should translate to English', () => {
|
|
905
|
-
i18nService.changeLanguage('en');
|
|
906
|
-
expect(i18nService.t(BUTTON_LOGIN)).toBe('Login');
|
|
907
|
-
});
|
|
908
|
-
|
|
909
|
-
it('should support parameters', () => {
|
|
910
|
-
const result = i18nService.t(WELCOME_USER, { name: 'John' });
|
|
911
|
-
expect(result).toContain('John');
|
|
912
|
-
});
|
|
913
|
-
});
|
|
914
|
-
```
|
|
915
|
-
|
|
916
|
-
---
|
|
917
|
-
|
|
918
|
-
## 📚 相关文档
|
|
919
|
-
|
|
920
|
-
- [项目架构设计](./index.md) - 了解整体架构
|
|
921
|
-
- [Bootstrap 启动器](./bootstrap.md) - I18nService 是 Bootstrap 插件之一
|
|
922
|
-
- [配置驱动开发](./#配置驱动开发) - i18n Key 是配置驱动的实践
|
|
923
|
-
|
|
924
|
-
---
|
|
925
|
-
|
|
926
|
-
## 🎉 总结
|
|
927
|
-
|
|
928
|
-
国际化系统的核心理念:
|
|
929
|
-
|
|
930
|
-
1. **唯一标识符** 🔑 - 每个文本都有唯一的 i18n Key
|
|
931
|
-
2. **绝不硬编码** 🚫 - 项目中所有文本都必须使用 i18n Key
|
|
932
|
-
3. **减少思考** 🧠 - 开发者只需知道变量名,不需要记住字符串路径
|
|
933
|
-
4. **集中管理** 📦 - 所有 Key 定义在 Identifier 目录中
|
|
934
|
-
5. **自动生成** ⚡ - 翻译文件从注释自动生成
|
|
935
|
-
6. **类型安全** 🔒 - TypeScript 提供完整的类型检查
|
|
936
|
-
7. **易于维护** 🛠️ - 修改翻译只需修改一处
|
|
937
|
-
8. **IDE 友好** 💻 - 完整的自动补全和类型提示
|
|
938
|
-
|
|
939
|
-
**记住两个核心原则:**
|
|
940
|
-
|
|
941
|
-
1. **永远不要在代码中直接写文字!**
|
|
942
|
-
2. **开发者不需要记住翻译字符串,只需要知道变量名!**
|
|
943
|
-
|
|
944
|
-
```typescript
|
|
945
|
-
// ❌ 永远不要这样做
|
|
946
|
-
<button>登录</button>
|
|
947
|
-
<h1>欢迎回来</h1>
|
|
948
|
-
<p>{t('common:confirm.delete')}</p> // 也不要直接写字符串
|
|
949
|
-
|
|
950
|
-
// ✅ 始终这样做
|
|
951
|
-
<button>{t(BUTTON_LOGIN)}</button>
|
|
952
|
-
<h1>{t(MESSAGE_WELCOME)}</h1>
|
|
953
|
-
<p>{t(CONFIRM_DELETE)}</p> // 使用变量,IDE 会自动补全
|
|
954
|
-
```
|
|
955
|
-
|
|
956
|
-
**开发流程:**
|
|
957
|
-
|
|
958
|
-
```typescript
|
|
959
|
-
// 1. 输入变量名前缀
|
|
960
|
-
t(BUTTON_
|
|
961
|
-
|
|
962
|
-
// 2. IDE 自动提示所有可用的 Key
|
|
963
|
-
// BUTTON_LOGIN
|
|
964
|
-
// BUTTON_SUBMIT
|
|
965
|
-
// BUTTON_CANCEL
|
|
966
|
-
// ...
|
|
967
|
-
|
|
968
|
-
// 3. 选择需要的 Key,完成!
|
|
969
|
-
t(BUTTON_LOGIN)
|
|
970
|
-
|
|
971
|
-
// 不需要记住 'common:button.login'
|
|
972
|
-
// 不需要关心翻译文件的结构
|
|
973
|
-
// 不需要担心拼写错误(TypeScript 会检查)
|
|
974
|
-
```
|
|
975
|
-
|
|
976
|
-
---
|
|
977
|
-
|
|
978
|
-
**问题反馈:**
|
|
979
|
-
如果你对国际化系统有任何疑问或建议,请在团队频道中讨论或提交 Issue。
|