generator-mico-cli 0.2.13 → 0.2.14
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/README.md +1 -1
- package/generators/micro-react/index.js +34 -10
- package/generators/micro-react/templates/.commitlintrc.js +1 -0
- package/generators/micro-react/templates/.cursor/rules/coding-conventions.mdc +1 -1
- package/generators/micro-react/templates/.cursor/rules/development-guide.mdc +1 -1
- package/generators/micro-react/templates/.cursor/rules/layout-app.mdc +2 -5
- package/generators/micro-react/templates/.cursor/rules/project-overview.mdc +2 -2
- package/generators/micro-react/templates/.cursor/rules/theme-system.mdc +10 -12
- package/generators/micro-react/templates/AGENTS.md +2 -2
- package/generators/micro-react/templates/CLAUDE.md +1 -2
- package/generators/micro-react/templates/README.md +2 -2
- package/generators/micro-react/templates/_npmrc +3 -0
- package/generators/micro-react/templates/apps/layout/config/config.dev.ts +15 -0
- package/generators/micro-react/templates/apps/layout/config/config.ts +4 -3
- package/generators/micro-react/templates/apps/layout/docs/common-intl.md +370 -0
- package/generators/micro-react/templates/apps/layout/docs/feature-404/351/241/265/351/235/242.md +2 -2
- package/generators/micro-react/templates/apps/layout/docs/feature-/344/270/273/351/242/230/350/211/262/345/210/207/346/215/242.md +21 -25
- package/generators/micro-react/templates/apps/layout/docs/feature-/345/276/256/345/211/215/347/253/257/346/250/241/345/274/217.md +15 -16
- package/generators/micro-react/templates/apps/layout/docs/feature-/350/217/234/345/215/225/346/235/203/351/231/220/346/216/247/345/210/266.md +1 -1
- package/generators/micro-react/templates/apps/layout/docs/utils-timezone.md +322 -0
- package/generators/micro-react/templates/apps/layout/mock/menus.ts +17 -1
- package/generators/micro-react/templates/apps/layout/package.json +5 -2
- package/generators/micro-react/templates/apps/layout/src/app.tsx +22 -10
- package/generators/micro-react/templates/apps/layout/src/common/auth/index.ts +3 -0
- package/generators/micro-react/templates/apps/layout/src/common/helpers.ts +177 -0
- package/generators/micro-react/templates/apps/layout/src/common/locale.ts +56 -4
- package/generators/micro-react/templates/apps/layout/src/common/menu/parser.ts +84 -5
- package/generators/micro-react/templates/apps/layout/src/common/menu/types.ts +13 -1
- package/generators/micro-react/templates/apps/layout/src/common/request/interceptors.ts +1 -1
- package/generators/micro-react/templates/apps/layout/src/common/request/sso.ts +7 -1
- package/generators/micro-react/templates/apps/layout/src/common/theme.ts +1 -1
- package/generators/micro-react/templates/apps/layout/src/common/upload/oss.ts +2 -3
- package/generators/micro-react/templates/apps/layout/src/components/AppTabs/index.less +8 -2
- package/generators/micro-react/templates/apps/layout/src/components/AppTabs/index.tsx +23 -6
- package/generators/micro-react/templates/apps/layout/src/components/HeaderDropdown/index.tsx +22 -0
- package/generators/micro-react/templates/apps/layout/src/components/IconFont/index.tsx +1 -1
- package/generators/micro-react/templates/apps/layout/src/components/MicroAppLoader/index.tsx +1 -1
- package/generators/micro-react/templates/apps/layout/src/components/RightContent/AvatarDropdown.tsx +388 -0
- package/generators/micro-react/templates/apps/layout/src/components/RightContent/avatar-dropdown.less +35 -0
- package/generators/micro-react/templates/apps/layout/src/components/RightContent/index.ts +2 -0
- package/generators/micro-react/templates/apps/layout/src/constants/index.ts +2 -0
- package/generators/micro-react/templates/apps/layout/src/global.less +3 -6
- package/generators/micro-react/templates/apps/layout/src/layouts/components/header/index.less +3 -1
- package/generators/micro-react/templates/apps/layout/src/layouts/components/header/index.tsx +9 -6
- package/generators/micro-react/templates/apps/layout/src/layouts/components/menu/index.less +3 -1
- package/generators/micro-react/templates/apps/layout/src/layouts/components/menu/index.tsx +4 -4
- package/generators/micro-react/templates/apps/layout/src/layouts/index.less +83 -13
- package/generators/micro-react/templates/apps/layout/src/layouts/index.tsx +40 -25
- package/generators/micro-react/templates/apps/layout/src/locales/en-US.ts +9 -0
- package/generators/micro-react/templates/apps/layout/src/locales/zh-CN.ts +9 -0
- package/generators/micro-react/templates/apps/layout/src/pages/403/index.tsx +2 -1
- package/generators/micro-react/templates/apps/layout/src/pages/404/index.tsx +1 -1
- package/generators/micro-react/templates/apps/layout/src/pages/Home/index.less +3 -0
- package/generators/micro-react/templates/apps/layout/src/pages/User/Login/index.less +1 -1
- package/generators/micro-react/templates/apps/layout/src/pages/User/Login/index.tsx +3 -3
- package/generators/micro-react/templates/apps/layout/src/requestErrorConfig.ts +1 -1
- package/generators/micro-react/templates/apps/layout/src/services/config/index.ts +63 -0
- package/generators/micro-react/templates/apps/layout/src/services/config/type.ts +32 -0
- package/generators/micro-react/templates/apps/layout/src/services/user.ts +23 -1
- package/generators/micro-react/templates/package.json +4 -1
- package/generators/micro-react/templates/packages/common-intl/.turbo/turbo-build.log +13 -0
- package/generators/micro-react/templates/packages/common-intl/README.md +428 -0
- package/generators/micro-react/templates/packages/common-intl/dist/index.d.ts +3 -0
- package/generators/micro-react/templates/packages/common-intl/dist/index.js +4388 -0
- package/generators/micro-react/templates/packages/common-intl/dist/indexedDBUtils.d.ts +13 -0
- package/generators/micro-react/templates/packages/common-intl/dist/intl.d.ts +1022 -0
- package/generators/micro-react/templates/packages/common-intl/dist/utils.d.ts +122 -0
- package/generators/micro-react/templates/packages/common-intl/package.json +34 -0
- package/generators/micro-react/templates/packages/common-intl/src/index.ts +7 -0
- package/generators/micro-react/templates/packages/common-intl/src/indexedDBUtils.ts +51 -0
- package/generators/micro-react/templates/packages/common-intl/src/intl.ts +5709 -0
- package/generators/micro-react/templates/packages/common-intl/src/utils.ts +481 -0
- package/generators/micro-react/templates/packages/common-intl/tsconfig.json +22 -0
- package/generators/micro-react/templates/packages/common-intl/vite.config.ts +25 -0
- package/generators/micro-react/templates/turbo.json +3 -0
- package/generators/subapp-react/index.js +13 -18
- package/generators/subapp-react/templates/homepage/README.md +3 -3
- package/generators/subapp-react/templates/homepage/config/config.dev.ts +8 -7
- package/generators/subapp-react/templates/homepage/config/config.prod.development.ts +2 -3
- package/generators/subapp-react/templates/homepage/config/config.prod.testing.ts +2 -3
- package/generators/subapp-react/templates/homepage/config/config.prod.ts +4 -5
- package/generators/subapp-react/templates/homepage/package.json +3 -2
- package/generators/subapp-react/templates/homepage/src/global.less +3 -3
- package/generators/subapp-react/templates/homepage/src/pages/index.less +2 -2
- package/generators/subapp-react/templates/homepage/src/pages/index.tsx +72 -33
- package/generators/subapp-react/templates/homepage/src/styles/theme.less +1 -1
- package/lib/utils.js +43 -0
- package/package.json +8 -11
- package/generators/micro-react/templates/packages/shared-styles/README.md +0 -124
- package/generators/micro-react/templates/packages/shared-styles/arco-design-mobile-override.less +0 -91
- package/generators/micro-react/templates/packages/shared-styles/arco-override.less +0 -119
- package/generators/micro-react/templates/packages/shared-styles/index.d.ts +0 -44
- package/generators/micro-react/templates/packages/shared-styles/index.less +0 -13
- package/generators/micro-react/templates/packages/shared-styles/package.json +0 -30
- package/generators/micro-react/templates/packages/shared-styles/theme-inject.less +0 -10
- package/generators/micro-react/templates/packages/shared-styles/themes/dark/custom-var.less +0 -290
- package/generators/micro-react/templates/packages/shared-styles/themes/normal/custom-var.less +0 -269
- package/generators/micro-react/templates/packages/shared-styles/variables-only.less +0 -433
- package/generators/micro-react/templates/packages/shared-styles/variables.less +0 -452
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
# 通用国际化库使用指南
|
|
2
|
+
|
|
3
|
+
## 概述
|
|
4
|
+
|
|
5
|
+
`@portal-web/common-intl` 是 Portal 项目的通用国际化(i18n)库,用于管理多语言文案,支持从多语言中台拉取翻译数据。
|
|
6
|
+
|
|
7
|
+
## 适用场景
|
|
8
|
+
|
|
9
|
+
- 应用需要支持多语言切换(中文、英文、阿拉伯语、土耳其语等)
|
|
10
|
+
- 需要从多语言中台动态获取翻译文案
|
|
11
|
+
- 希望统一管理国际化逻辑,减少重复代码
|
|
12
|
+
|
|
13
|
+
## 核心特性
|
|
14
|
+
|
|
15
|
+
| 特性 | 说明 |
|
|
16
|
+
| ---------- | ------------------------------------------------ |
|
|
17
|
+
| 跨端兼容 | 通过外部注入 `messageInstance`,适配不同 UI 框架 |
|
|
18
|
+
| 离线缓存 | 支持 IndexedDB + 内存双重缓存 |
|
|
19
|
+
| 插值替换 | 支持 `%AA%`、`%BB%` 等动态占位符 |
|
|
20
|
+
| 降级兜底 | 翻译缺失时自动返回默认文案 |
|
|
21
|
+
| 微前端共享 | 主应用获取文案后,子应用可直接使用 |
|
|
22
|
+
|
|
23
|
+
## 两种使用模式
|
|
24
|
+
|
|
25
|
+
根据应用架构的不同,`@portal-web/common-intl` 支持两种使用模式:
|
|
26
|
+
|
|
27
|
+
| 模式 | 适用场景 | 示例应用 |
|
|
28
|
+
| ------------ | ------------------------------------------------ | ------------------------------------------------------------ |
|
|
29
|
+
| 微前端模式 | qiankun 主子应用架构,主应用获取文案后子应用共享 | layout、conversation-v2、session、workorder、faq、permission |
|
|
30
|
+
| 独立应用模式 | 独立运行的应用,自己获取和管理文案 | mico-cs-mobile |
|
|
31
|
+
|
|
32
|
+
### 微前端模式
|
|
33
|
+
|
|
34
|
+
在 qiankun 微前端架构中,**主应用负责获取文案**,子应用直接使用共享的文案对象,无需重复初始化。
|
|
35
|
+
|
|
36
|
+
**工作原理**:
|
|
37
|
+
|
|
38
|
+
- 主应用调用 `initIntl()` 获取远程文案
|
|
39
|
+
- 文案数据存储到 `window.__MICO_COMMON_INTL_TRANSLATIONS__`
|
|
40
|
+
- 子应用直接使用 `intl` 对象,自动从 window 读取共享数据
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
44
|
+
│ apps/layout(主应用) │
|
|
45
|
+
│ ┌─────────────────────────────────────────────────────────┐│
|
|
46
|
+
│ │ initIntl() → 请求远程文案 → 存储到 window 对象 ││
|
|
47
|
+
│ └─────────────────────────────────────────────────────────┘│
|
|
48
|
+
└─────────────────────────────────────────────────────────────┘
|
|
49
|
+
↓
|
|
50
|
+
┌─────────────────────┼─────────────────────┐
|
|
51
|
+
↓ ↓ ↓
|
|
52
|
+
┌───────────────┐ ┌───────────────┐ ┌───────────────┐
|
|
53
|
+
│ conversation │ │ session │ │ workorder │
|
|
54
|
+
│ 子应用 │ │ 子应用 │ │ 子应用 │
|
|
55
|
+
│ import {intl} │ │ import {intl} │ │ import {intl} │
|
|
56
|
+
│ 直接使用 │ │ 直接使用 │ │ 直接使用 │
|
|
57
|
+
└───────────────┘ └───────────────┘ └───────────────┘
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
**主应用配置(apps/layout/src/app.tsx)**:
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import {
|
|
64
|
+
getCurrentLocale,
|
|
65
|
+
initIntl,
|
|
66
|
+
type ILang,
|
|
67
|
+
} from "@portal-web/common-intl";
|
|
68
|
+
import { Message } from "@arco-design/web-react";
|
|
69
|
+
import { request } from "@umijs/max";
|
|
70
|
+
|
|
71
|
+
// 初始化国际化模块
|
|
72
|
+
const fetchMultilingualData = initIntl({
|
|
73
|
+
requestInstance: request,
|
|
74
|
+
messageInstance: {
|
|
75
|
+
error: (msg: string) => Message.error(msg),
|
|
76
|
+
warning: (msg: string) => Message.warning(msg),
|
|
77
|
+
},
|
|
78
|
+
fetchMultilingualDataParams: {
|
|
79
|
+
tag: "cs_fe_pc",
|
|
80
|
+
lang: getCurrentLocale() as ILang,
|
|
81
|
+
app_name: "middle",
|
|
82
|
+
},
|
|
83
|
+
indexedDBParams: {
|
|
84
|
+
dbName: "mico_cs_web_i18n_db",
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// 在应用渲染前加载多语言数据
|
|
89
|
+
export function render(oldRender: () => void): void {
|
|
90
|
+
fetchMultilingualData()
|
|
91
|
+
.then(oldRender)
|
|
92
|
+
.catch((error: Error) => {
|
|
93
|
+
console.error("获取多语言文案失败", error);
|
|
94
|
+
oldRender(); // 失败时继续渲染,使用兜底文案
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
**子应用使用(无需初始化)**:
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
// apps/conversation-v2、apps/session、apps/workorder 等子应用
|
|
103
|
+
import { intl } from "@portal-web/common-intl";
|
|
104
|
+
|
|
105
|
+
// 直接使用,文案来自主应用
|
|
106
|
+
<h1>{intl.cs_web_workbench_conversation_record()}</h1>;
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 独立应用模式
|
|
110
|
+
|
|
111
|
+
对于非 qiankun 架构的独立应用(如 H5 应用),需要自己调用 `initIntl()` 获取文案,并维护独立的文案文件。
|
|
112
|
+
|
|
113
|
+
**独立应用配置(apps/mico-cs-mobile/src/app.ts)**:
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
import { initIntl } from "@portal-web/common-intl";
|
|
117
|
+
import { Toast } from "@arco-design/mobile-react";
|
|
118
|
+
import { request } from "@umijs/max";
|
|
119
|
+
import { convertLocaleToLangParam } from "./locales/utils";
|
|
120
|
+
import { getSearchParams } from "@/common/jsbridge/jsbridge";
|
|
121
|
+
|
|
122
|
+
// 初始化国际化模块
|
|
123
|
+
const fetchMultilingualData = initIntl({
|
|
124
|
+
requestInstance: request,
|
|
125
|
+
messageInstance: {
|
|
126
|
+
error: (msg: string) => Toast.error(msg),
|
|
127
|
+
warning: (msg: string) => Toast.warn(msg),
|
|
128
|
+
},
|
|
129
|
+
fetchMultilingualDataParams: {
|
|
130
|
+
tag: "cs_fe_mobile", // 独立应用使用不同的 tag
|
|
131
|
+
lang: convertLocaleToLangParam(getSearchParams().language as string),
|
|
132
|
+
app_name: "middle",
|
|
133
|
+
},
|
|
134
|
+
indexedDBParams: {
|
|
135
|
+
dbName: "mico_cs_mobile_i18n_db",
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
export function render(oldRender: () => void): void {
|
|
140
|
+
fetchMultilingualData()
|
|
141
|
+
.then(oldRender)
|
|
142
|
+
.catch((error: Error) => {
|
|
143
|
+
console.error("获取多语言文案失败", error);
|
|
144
|
+
oldRender();
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
**独立应用的文案文件(apps/mico-cs-mobile/src/locales/index.ts)**:
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
import { i18n } from "@portal-web/common-intl";
|
|
153
|
+
|
|
154
|
+
const intl = {
|
|
155
|
+
sdk_h5_ticket_record: () =>
|
|
156
|
+
i18n({
|
|
157
|
+
key: "sdk_h5_ticket_record",
|
|
158
|
+
defaultMessage: "工单记录",
|
|
159
|
+
}),
|
|
160
|
+
// ... 其他文案
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
export default intl;
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
**在组件中使用**:
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
import intl from "@/locales";
|
|
170
|
+
|
|
171
|
+
<h1>{intl.sdk_h5_ticket_record()}</h1>;
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## 导出 API
|
|
175
|
+
|
|
176
|
+
`@portal-web/common-intl` 提供以下导出:
|
|
177
|
+
|
|
178
|
+
| 导出 | 类型 | 说明 |
|
|
179
|
+
| -------------------- | ---- | --------------------------------------------------------- |
|
|
180
|
+
| `initIntl` | 函数 | 初始化国际化模块,返回 `fetchMultilingualData` 函数 |
|
|
181
|
+
| `i18n` | 函数 | 翻译函数,用于获取单条翻译文案 |
|
|
182
|
+
| `intl` | 对象 | PC 端主子应用共享的文案对象(仅供 layout 及其子应用使用) |
|
|
183
|
+
| `LANG` | 常量 | 支持的语言常量:`ZH_CN`、`EN`、`AR`、`TR` |
|
|
184
|
+
| `LOCALE` | 常量 | `LANG` 的别名,用于兼容 |
|
|
185
|
+
| `SUPPORTED_LOCALES` | 常量 | 支持的语言列表数组 |
|
|
186
|
+
| `getCurrentLocale` | 函数 | 获取当前语言环境(按优先级:URL > localStorage > 浏览器) |
|
|
187
|
+
| `setLocaleToStorage` | 函数 | 设置语言到 localStorage |
|
|
188
|
+
| `ILang` | 类型 | 语言类型定义 |
|
|
189
|
+
| `TLocale` | 类型 | `ILang` 的别名,用于兼容 |
|
|
190
|
+
|
|
191
|
+
### 导入示例
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
// 微前端子应用 - 直接使用共享的 intl 对象
|
|
195
|
+
import { intl } from "@portal-web/common-intl";
|
|
196
|
+
intl.cs_web_common_request_failed();
|
|
197
|
+
|
|
198
|
+
// 独立应用 - 使用 i18n 函数构建自己的文案对象
|
|
199
|
+
import { i18n, initIntl, LANG } from "@portal-web/common-intl";
|
|
200
|
+
|
|
201
|
+
// 获取当前语言环境
|
|
202
|
+
import { getCurrentLocale } from "@portal-web/common-intl";
|
|
203
|
+
const currentLang = getCurrentLocale(); // 'zh_CN' | 'en' | 'ar' | 'tr'
|
|
204
|
+
|
|
205
|
+
// 获取支持的语言列表
|
|
206
|
+
import { SUPPORTED_LOCALES } from "@portal-web/common-intl";
|
|
207
|
+
console.log(SUPPORTED_LOCALES); // ['zh_CN', 'en', 'ar', 'tr']
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## 文案管理规则
|
|
211
|
+
|
|
212
|
+
### PC 端主子应用
|
|
213
|
+
|
|
214
|
+
- **文案位置**:[packages/common-intl/src/intl.ts](../packages/common-intl/src/intl.ts)
|
|
215
|
+
- **使用方式**:`import { intl } from '@portal-web/common-intl'`
|
|
216
|
+
- **适用应用**:
|
|
217
|
+
- apps/layout(主应用)
|
|
218
|
+
- apps/conversation-v2(工作台子应用)
|
|
219
|
+
- apps/session(服务记录子应用)
|
|
220
|
+
- apps/workorder(工单子应用)
|
|
221
|
+
- apps/faq(FAQ 子应用)
|
|
222
|
+
- apps/permission(权限管理子应用)
|
|
223
|
+
- packages/shared(公共包)
|
|
224
|
+
|
|
225
|
+
### 独立应用(如 mico-cs-mobile)
|
|
226
|
+
|
|
227
|
+
- **文案位置**:各应用自己的 `src/locales/index.ts`
|
|
228
|
+
- **使用方式**:`import intl from '@/locales'`
|
|
229
|
+
- **原因**:独立应用有自己的多语言标签(tag),文案独立管理
|
|
230
|
+
|
|
231
|
+
### layout-crm 应用
|
|
232
|
+
|
|
233
|
+
- **角色**:另一个主应用(类似 layout)
|
|
234
|
+
- **使用方式**:使用 `getCurrentLocale`、`LOCALE` 等工具函数
|
|
235
|
+
- **说明**:layout-crm 是独立的布局应用,可按需配置国际化初始化
|
|
236
|
+
|
|
237
|
+
## 快速接入
|
|
238
|
+
|
|
239
|
+
### 1. 添加依赖
|
|
240
|
+
|
|
241
|
+
```json
|
|
242
|
+
// apps/your-app/package.json
|
|
243
|
+
{
|
|
244
|
+
"dependencies": {
|
|
245
|
+
"@portal-web/common-intl": "workspace:*"
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
### 2. 根据模式选择接入方式
|
|
251
|
+
|
|
252
|
+
**如果是 layout 的子应用**:
|
|
253
|
+
|
|
254
|
+
- 无需调用 `initIntl()`
|
|
255
|
+
- 直接使用 `import { intl } from '@portal-web/common-intl'`
|
|
256
|
+
|
|
257
|
+
**如果是独立应用**:
|
|
258
|
+
|
|
259
|
+
- 在 `app.ts` 中调用 `initIntl()` 并配置
|
|
260
|
+
- 创建自己的国际化文案集中管理文件,如 `src/locales/index.ts`
|
|
261
|
+
- 使用 `import intl from '@/locales'`
|
|
262
|
+
|
|
263
|
+
## 语言优先级
|
|
264
|
+
|
|
265
|
+
`getCurrentLocale()` 函数按以下优先级确定当前语言:
|
|
266
|
+
|
|
267
|
+
1. **URL 参数**:`?lang=zh_CN`
|
|
268
|
+
2. **localStorage**:`umi_locale` 键
|
|
269
|
+
3. **浏览器默认语言**:`navigator.language`
|
|
270
|
+
|
|
271
|
+
## 详细文档
|
|
272
|
+
|
|
273
|
+
完整的 API 参考、类型定义和占位符规则,请参阅:
|
|
274
|
+
|
|
275
|
+
- **[packages/common-intl/README.md](../packages/common-intl/README.md)**
|
|
276
|
+
|
|
277
|
+
## 常见问题
|
|
278
|
+
|
|
279
|
+
### Q: 微前端子应用需要调用 initIntl 吗?
|
|
280
|
+
|
|
281
|
+
不需要。主应用(layout)负责初始化和获取文案,子应用直接使用共享的 `intl` 对象即可。
|
|
282
|
+
|
|
283
|
+
### Q: 如何获取多语言中台的 `tag` 和 `app_name`?
|
|
284
|
+
|
|
285
|
+
请联系多语言中台管理员,根据你的应用申请对应的配置参数。
|
|
286
|
+
|
|
287
|
+
### Q: 翻译数据什么时候加载?
|
|
288
|
+
|
|
289
|
+
推荐在 UmiJS 的 `render` 钩子中调用 `fetchMultilingualData()`,确保在应用渲染前完成加载。
|
|
290
|
+
|
|
291
|
+
### Q: 如何添加新的翻译文案?
|
|
292
|
+
|
|
293
|
+
1. 在多语言中台添加新的翻译 key 和对应的各语言翻译
|
|
294
|
+
2. PC 端:在 `packages/common-intl/src/intl.ts` 中添加对应的函数
|
|
295
|
+
3. 独立应用:在应用自己的 `src/locales/index.ts` 中添加
|
|
296
|
+
4. 在代码中通过 `intl.xxx()` 调用
|
|
297
|
+
|
|
298
|
+
### Q: IndexedDB 不可用怎么办?
|
|
299
|
+
|
|
300
|
+
库会自动降级到内存缓存模式,不影响正常使用,但页面刷新后需要重新拉取数据。
|
|
301
|
+
|
|
302
|
+
### Q: 主应用获取文案失败,子应用会怎样?
|
|
303
|
+
|
|
304
|
+
会显示兜底文案(`defaultMessage`)。建议在定义文案时始终提供有意义的兜底文案。
|
|
305
|
+
|
|
306
|
+
### Q: 如何切换语言?
|
|
307
|
+
|
|
308
|
+
语言切换由主应用(layout)的 AvatarDropdown 组件处理,切换后会刷新页面重新加载对应语言的文案。
|
|
309
|
+
|
|
310
|
+
### Q: 支持哪些语言?
|
|
311
|
+
|
|
312
|
+
目前支持以下语言:
|
|
313
|
+
|
|
314
|
+
| 语言常量 | 值 | 说明 |
|
|
315
|
+
| ------------ | ------- | -------- |
|
|
316
|
+
| `LANG.ZH_CN` | `zh_CN` | 简体中文 |
|
|
317
|
+
| `LANG.EN` | `en` | 英语 |
|
|
318
|
+
| `LANG.AR` | `ar` | 阿拉伯语 |
|
|
319
|
+
| `LANG.TR` | `tr` | 土耳其语 |
|
|
320
|
+
|
|
321
|
+
### Q: 如何在代码中获取当前语言?
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
import { getCurrentLocale } from "@portal-web/common-intl";
|
|
325
|
+
|
|
326
|
+
const currentLang = getCurrentLocale();
|
|
327
|
+
if (currentLang === "ar") {
|
|
328
|
+
// 处理阿拉伯语的 RTL 布局
|
|
329
|
+
}
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
### Q: 模块级常量中使用 intl 时,为什么文案显示不正确?
|
|
333
|
+
|
|
334
|
+
当在模块顶层定义常量时,`intl.xxx()` 会在模块加载时立即执行,此时多语言文案可能还未加载完成,导致显示兜底文案或空值。
|
|
335
|
+
|
|
336
|
+
**错误示例**:
|
|
337
|
+
|
|
338
|
+
```typescript
|
|
339
|
+
// ❌ 模块加载时立即执行,此时文案可能未加载
|
|
340
|
+
export const STATUS_TEXT = {
|
|
341
|
+
pending: intl.cs_web_status_pending(),
|
|
342
|
+
completed: intl.cs_web_status_completed(),
|
|
343
|
+
};
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
**正确做法**:用函数包裹,延迟到运行时获取文案。
|
|
347
|
+
|
|
348
|
+
```typescript
|
|
349
|
+
// ✅ 函数形式,运行时调用才获取文案
|
|
350
|
+
export const getStatusText = (status: TStatus): string => {
|
|
351
|
+
const map: Record<TStatus, string> = {
|
|
352
|
+
pending: intl.cs_web_status_pending(),
|
|
353
|
+
completed: intl.cs_web_status_completed(),
|
|
354
|
+
};
|
|
355
|
+
return map[status];
|
|
356
|
+
};
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
**在组件中使用时**,如果需要构建选项列表,可用 `useMemo`:
|
|
360
|
+
|
|
361
|
+
```tsx
|
|
362
|
+
const options = useMemo(
|
|
363
|
+
() =>
|
|
364
|
+
Object.values(Status).map((status) => ({
|
|
365
|
+
value: status,
|
|
366
|
+
label: getStatusText(status),
|
|
367
|
+
})),
|
|
368
|
+
[],
|
|
369
|
+
);
|
|
370
|
+
```
|
package/generators/micro-react/templates/apps/layout/docs/feature-404/351/241/265/351/235/242.md
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
### 技术栈
|
|
12
12
|
|
|
13
13
|
- 框架:React 18 + @umijs/max
|
|
14
|
-
- UI
|
|
14
|
+
- UI 组件:@mico-platform/ui `Result`、`Button`、`Space`
|
|
15
15
|
- 路由:React Router 6 通配符路由
|
|
16
16
|
|
|
17
17
|
### 核心实现
|
|
@@ -84,7 +84,7 @@ const getFirstAvailablePath = (filterOptions: MenuFilterOptions): string;
|
|
|
84
84
|
|
|
85
85
|
| 决策点 | 选择 | 理由 |
|
|
86
86
|
| --- | --- | --- |
|
|
87
|
-
| UI 组件 |
|
|
87
|
+
| UI 组件 | @mico-platform/ui Result | 与 403 页面保持一致,复用现有组件库 |
|
|
88
88
|
| 首页跳转逻辑 | 动态获取第一个可访问路由 | 适应动态菜单配置,不硬编码路径 |
|
|
89
89
|
| 权限过滤 | 复用 filterMenuItems | 与菜单权限逻辑保持一致 |
|
|
90
90
|
|
|
@@ -11,7 +11,7 @@ Layout 应用支持亮色/暗黑主题切换,通过 CSS 变量实现运行时
|
|
|
11
11
|
### 技术栈
|
|
12
12
|
|
|
13
13
|
- 框架:React 18 + UmiJS 4
|
|
14
|
-
- UI
|
|
14
|
+
- UI 组件:@mico-platform/ui
|
|
15
15
|
- 样式:Less + CSS Variables
|
|
16
16
|
- 状态管理:React Hooks + localStorage
|
|
17
17
|
|
|
@@ -19,7 +19,7 @@ Layout 应用支持亮色/暗黑主题切换,通过 CSS 变量实现运行时
|
|
|
19
19
|
|
|
20
20
|
采用双属性主题系统:
|
|
21
21
|
|
|
22
|
-
1. `arco-theme` 属性控制
|
|
22
|
+
1. `arco-theme` 属性控制 @mico-platform/ui 组件主题(基于 Arco)
|
|
23
23
|
2. `data-theme` 属性控制自定义 CSS 变量
|
|
24
24
|
|
|
25
25
|
两个属性都设置在 `body` 元素上,确保选择器正确匹配。Less 变量引用 CSS 变量,实现编译时类型安全和运行时动态切换。
|
|
@@ -28,22 +28,19 @@ Layout 应用支持亮色/暗黑主题切换,通过 CSS 变量实现运行时
|
|
|
28
28
|
|
|
29
29
|
### 主题系统核心文件
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
主题变量由 `@mico-platform/theme` 包提供,主应用与子应用通过该包获取 CSS 变量与 Less 变量:
|
|
32
32
|
|
|
33
|
-
|
|
|
34
|
-
|
|
|
35
|
-
|
|
|
36
|
-
|
|
|
37
|
-
|
|
|
38
|
-
| `packages/shared-styles/variables.less` | Less 变量定义(引用 CSS 变量) |
|
|
39
|
-
| `packages/shared-styles/index.less` | 入口文件,导入全部(主应用用) |
|
|
40
|
-
| `packages/shared-styles/variables-only.less` | 仅 Less 变量(子应用用) |
|
|
33
|
+
| 入口/子路径 | 说明 |
|
|
34
|
+
| ------------------------------ | ------------------------------ |
|
|
35
|
+
| `@mico-platform/theme` | 主入口,CSS 变量 + Less 变量 |
|
|
36
|
+
| `@mico-platform/theme/variables` | 仅 Less 变量(子应用用) |
|
|
37
|
+
| `@mico-platform/theme/theme-inject` | 子应用独立运行时注入主题 |
|
|
41
38
|
|
|
42
39
|
### Layout 应用文件
|
|
43
40
|
|
|
44
41
|
| 文件路径 | 说明 |
|
|
45
42
|
| ---------------------------- | ----------------------------------------- |
|
|
46
|
-
| `src/global.less` | 导入
|
|
43
|
+
| `src/global.less` | 导入 `@mico-platform/theme` |
|
|
47
44
|
| `src/common/theme.ts` | 主题切换逻辑(`applyTheme`、`initTheme`) |
|
|
48
45
|
| `src/hooks/useTheme.ts` | 主题管理 Hook |
|
|
49
46
|
|
|
@@ -82,7 +79,7 @@ const { theme, isDark, toggleTheme, setTheme } = useTheme();
|
|
|
82
79
|
|
|
83
80
|
### 主要颜色变量
|
|
84
81
|
|
|
85
|
-
> 完整变量列表请参考
|
|
82
|
+
> 完整变量列表请参考 @mico-platform/theme 包文档
|
|
86
83
|
|
|
87
84
|
| 分类 | 变量 |
|
|
88
85
|
| ------ | --------------------------------------- |
|
|
@@ -151,8 +148,8 @@ export const THEME = {
|
|
|
151
148
|
|
|
152
149
|
```less
|
|
153
150
|
// apps/[子应用]/src/global.less
|
|
154
|
-
@import '
|
|
155
|
-
//
|
|
151
|
+
@import '@mico-platform/theme/variables';
|
|
152
|
+
// 主应用已注入 CSS 变量时,子应用仅需 Less 变量
|
|
156
153
|
```
|
|
157
154
|
|
|
158
155
|
#### 2. app.tsx - 开发环境条件注入
|
|
@@ -195,24 +192,24 @@ wc -c apps/[子应用]/dist/umi.css
|
|
|
195
192
|
# 期望:< 500 字节
|
|
196
193
|
```
|
|
197
194
|
|
|
198
|
-
###
|
|
195
|
+
### @mico-platform/ui 组件主题适配
|
|
199
196
|
|
|
200
|
-
子应用使用
|
|
197
|
+
子应用使用 @mico-platform/ui 组件时,组件样式会自动跟随主应用的主题切换。
|
|
201
198
|
|
|
202
199
|
#### 原理
|
|
203
200
|
|
|
204
|
-
1. 主应用加载
|
|
205
|
-
2.
|
|
201
|
+
1. 主应用加载 @mico-platform/ui 并暴露到 `window.micoUi`
|
|
202
|
+
2. 主应用的样式包含 `arco-theme` 属性选择器(基于 Arco)
|
|
206
203
|
3. 主应用切换主题时设置 `body[arco-theme="dark"]`
|
|
207
|
-
4. 子应用的
|
|
204
|
+
4. 子应用的 UI 组件(来自 window.micoUi)自动应用暗色样式
|
|
208
205
|
|
|
209
|
-
#### 子应用使用
|
|
206
|
+
#### 子应用使用 @mico-platform/ui
|
|
210
207
|
|
|
211
208
|
> 详细配置请参考 [微前端模式 - 依赖共享机制](./feature-微前端模式.md#依赖共享机制)
|
|
212
209
|
|
|
213
210
|
```tsx
|
|
214
|
-
// 子应用可以直接使用
|
|
215
|
-
import { Button, Card, Message, Tag } from '@
|
|
211
|
+
// 子应用可以直接使用 @mico-platform/ui 组件
|
|
212
|
+
import { Button, Card, Message, Tag } from '@mico-platform/ui';
|
|
216
213
|
|
|
217
214
|
export default function Page() {
|
|
218
215
|
return (
|
|
@@ -224,11 +221,10 @@ export default function Page() {
|
|
|
224
221
|
}
|
|
225
222
|
```
|
|
226
223
|
|
|
227
|
-
**注意**:子应用需要配置 `externals` 将
|
|
224
|
+
**注意**:子应用需要配置 `externals` 将 @mico-platform/ui 排除打包,详见 [微前端模式文档](./feature-微前端模式.md#依赖共享机制)。
|
|
228
225
|
|
|
229
226
|
## 相关文档
|
|
230
227
|
|
|
231
228
|
- [微前端模式](./feature-微前端模式.md) - qiankun 微前端架构与依赖共享
|
|
232
229
|
- [日志与常量](./arch-日志与常量.md) - Logger 工具与常量管理(含 THEME 常量)
|
|
233
|
-
- [shared-styles 包](../../../packages/shared-styles/README.md) - 共享样式包使用指南
|
|
234
230
|
- [提交规范](../../../docs/commit-message.md) - Git Commit 规范
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
|
|
14
14
|
- 框架:React 18 + @umijs/max
|
|
15
15
|
- 微前端:qiankun (通过 @umijs/max 内置插件)
|
|
16
|
-
- UI
|
|
16
|
+
- UI 组件:@mico-platform/ui
|
|
17
17
|
- 路由:React Router 6
|
|
18
18
|
|
|
19
19
|
### 核心实现
|
|
@@ -256,9 +256,9 @@ export const getRequest = () => mainAppProps?.request;
|
|
|
256
256
|
|
|
257
257
|
### 问题背景
|
|
258
258
|
|
|
259
|
-
如果子应用直接安装 React
|
|
259
|
+
如果子应用直接安装 React、@mico-platform/ui 等大型库,会导致:
|
|
260
260
|
|
|
261
|
-
1. **包体积重复**:React (~140KB) +
|
|
261
|
+
1. **包体积重复**:React (~140KB) + UI 库 (~800KB) 被打包多次
|
|
262
262
|
2. **样式冲突**:多份 CSS 可能互相覆盖
|
|
263
263
|
3. **React 实例问题**:可能出现 "Invalid hook call" 错误
|
|
264
264
|
|
|
@@ -270,8 +270,8 @@ export const getRequest = () => mainAppProps?.request;
|
|
|
270
270
|
┌─────────────────────────────────────────────────────────────┐
|
|
271
271
|
│ 主应用 (layout) │
|
|
272
272
|
│ ┌─────────────────────────────────────────────────────────┐│
|
|
273
|
-
│ │ 1. 打包并加载 React、ReactDOM
|
|
274
|
-
│ │ 2. 暴露到 window.React / window.ReactDOM / window.
|
|
273
|
+
│ │ 1. 打包并加载 React、ReactDOM、@mico-platform/ui ││
|
|
274
|
+
│ │ 2. 暴露到 window.React / window.ReactDOM / window.micoUi ││
|
|
275
275
|
│ └─────────────────────────────────────────────────────────┘│
|
|
276
276
|
│ ↓ 共享 │
|
|
277
277
|
│ ┌─────────────────────────────────────────────────────────┐│
|
|
@@ -289,7 +289,7 @@ export const getRequest = () => mainAppProps?.request;
|
|
|
289
289
|
|
|
290
290
|
```typescript
|
|
291
291
|
// apps/layout/src/app.tsx
|
|
292
|
-
import * as
|
|
292
|
+
import * as micoUi from '@mico-platform/ui';
|
|
293
293
|
import React from 'react';
|
|
294
294
|
import ReactDOM from 'react-dom';
|
|
295
295
|
|
|
@@ -297,7 +297,7 @@ import ReactDOM from 'react-dom';
|
|
|
297
297
|
if (typeof window !== 'undefined') {
|
|
298
298
|
(window as any).React = React;
|
|
299
299
|
(window as any).ReactDOM = ReactDOM;
|
|
300
|
-
(window as any).
|
|
300
|
+
(window as any).micoUi = micoUi;
|
|
301
301
|
}
|
|
302
302
|
```
|
|
303
303
|
|
|
@@ -334,7 +334,7 @@ const config: ReturnType<typeof defineConfig> = {
|
|
|
334
334
|
externals: {
|
|
335
335
|
react: 'window.React',
|
|
336
336
|
'react-dom': 'window.ReactDOM',
|
|
337
|
-
'@
|
|
337
|
+
'@mico-platform/ui': 'window.micoUi',
|
|
338
338
|
},
|
|
339
339
|
};
|
|
340
340
|
|
|
@@ -362,23 +362,23 @@ export default defineConfig(config);
|
|
|
362
362
|
|
|
363
363
|
#### 3. package.json - 开发依赖
|
|
364
364
|
|
|
365
|
-
将
|
|
365
|
+
将 @mico-platform/ui 放在 `devDependencies`(用于类型提示和开发),生产环境从主应用获取。版本由生成器在创建子应用时从 npm 解析为当时最新版本(不使用 `latest` 标签):
|
|
366
366
|
|
|
367
367
|
```json
|
|
368
368
|
{
|
|
369
369
|
"devDependencies": {
|
|
370
|
-
"@
|
|
370
|
+
"@mico-platform/ui": "^x.x.x"
|
|
371
371
|
}
|
|
372
372
|
}
|
|
373
373
|
```
|
|
374
374
|
|
|
375
|
-
### 子应用使用
|
|
375
|
+
### 子应用使用 @mico-platform/ui
|
|
376
376
|
|
|
377
|
-
配置完成后,子应用可以正常导入和使用
|
|
377
|
+
配置完成后,子应用可以正常导入和使用 UI 组件:
|
|
378
378
|
|
|
379
379
|
```tsx
|
|
380
380
|
// apps/[子应用]/src/pages/index.tsx
|
|
381
|
-
import { Button, Card, Message, Tag } from '@
|
|
381
|
+
import { Button, Card, Message, Tag } from '@mico-platform/ui';
|
|
382
382
|
|
|
383
383
|
export default function HomePage() {
|
|
384
384
|
return (
|
|
@@ -396,7 +396,7 @@ export default function HomePage() {
|
|
|
396
396
|
|
|
397
397
|
| 配置方式 | 子应用 JS 体积 | 说明 |
|
|
398
398
|
| ------------------ | -------------- | ------------------------ |
|
|
399
|
-
| 直接安装依赖 | ~1.2MB | 包含 React +
|
|
399
|
+
| 直接安装依赖 | ~1.2MB | 包含 React + @mico-platform/ui |
|
|
400
400
|
| **externals 共享** | **~50KB** | 仅业务代码 |
|
|
401
401
|
|
|
402
402
|
### 运行环境说明
|
|
@@ -417,14 +417,13 @@ export default function HomePage() {
|
|
|
417
417
|
- 组件会在 `entry` 或 `name` 变化时重新加载微应用
|
|
418
418
|
- 加载失败会显示错误信息,便于调试
|
|
419
419
|
- **子应用应使用主应用传递的 `request` 实例**,确保认证 token 和拦截器一致
|
|
420
|
-
- **子应用使用 externals 共享依赖**,避免重复打包 React 和
|
|
420
|
+
- **子应用使用 externals 共享依赖**,避免重复打包 React 和 @mico-platform/ui
|
|
421
421
|
|
|
422
422
|
## 相关文档
|
|
423
423
|
|
|
424
424
|
- [主题色切换](./feature-主题色切换.md) - 主题系统实现与子应用适配
|
|
425
425
|
- [请求模块架构](./arch-请求模块.md) - HTTP 请求层模块化设计
|
|
426
426
|
- [日志与常量](./arch-日志与常量.md) - Logger 工具与常量管理
|
|
427
|
-
- [shared-styles 包](../../../packages/shared-styles/README.md) - 共享样式包使用指南
|
|
428
427
|
- [提交规范](../../../docs/commit-message.md) - Git Commit 规范
|
|
429
428
|
|
|
430
429
|
## 加载类型判断逻辑
|