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.
Files changed (99) hide show
  1. package/README.md +1 -1
  2. package/generators/micro-react/index.js +34 -10
  3. package/generators/micro-react/templates/.commitlintrc.js +1 -0
  4. package/generators/micro-react/templates/.cursor/rules/coding-conventions.mdc +1 -1
  5. package/generators/micro-react/templates/.cursor/rules/development-guide.mdc +1 -1
  6. package/generators/micro-react/templates/.cursor/rules/layout-app.mdc +2 -5
  7. package/generators/micro-react/templates/.cursor/rules/project-overview.mdc +2 -2
  8. package/generators/micro-react/templates/.cursor/rules/theme-system.mdc +10 -12
  9. package/generators/micro-react/templates/AGENTS.md +2 -2
  10. package/generators/micro-react/templates/CLAUDE.md +1 -2
  11. package/generators/micro-react/templates/README.md +2 -2
  12. package/generators/micro-react/templates/_npmrc +3 -0
  13. package/generators/micro-react/templates/apps/layout/config/config.dev.ts +15 -0
  14. package/generators/micro-react/templates/apps/layout/config/config.ts +4 -3
  15. package/generators/micro-react/templates/apps/layout/docs/common-intl.md +370 -0
  16. package/generators/micro-react/templates/apps/layout/docs/feature-404/351/241/265/351/235/242.md +2 -2
  17. 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
  18. 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
  19. 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
  20. package/generators/micro-react/templates/apps/layout/docs/utils-timezone.md +322 -0
  21. package/generators/micro-react/templates/apps/layout/mock/menus.ts +17 -1
  22. package/generators/micro-react/templates/apps/layout/package.json +5 -2
  23. package/generators/micro-react/templates/apps/layout/src/app.tsx +22 -10
  24. package/generators/micro-react/templates/apps/layout/src/common/auth/index.ts +3 -0
  25. package/generators/micro-react/templates/apps/layout/src/common/helpers.ts +177 -0
  26. package/generators/micro-react/templates/apps/layout/src/common/locale.ts +56 -4
  27. package/generators/micro-react/templates/apps/layout/src/common/menu/parser.ts +84 -5
  28. package/generators/micro-react/templates/apps/layout/src/common/menu/types.ts +13 -1
  29. package/generators/micro-react/templates/apps/layout/src/common/request/interceptors.ts +1 -1
  30. package/generators/micro-react/templates/apps/layout/src/common/request/sso.ts +7 -1
  31. package/generators/micro-react/templates/apps/layout/src/common/theme.ts +1 -1
  32. package/generators/micro-react/templates/apps/layout/src/common/upload/oss.ts +2 -3
  33. package/generators/micro-react/templates/apps/layout/src/components/AppTabs/index.less +8 -2
  34. package/generators/micro-react/templates/apps/layout/src/components/AppTabs/index.tsx +23 -6
  35. package/generators/micro-react/templates/apps/layout/src/components/HeaderDropdown/index.tsx +22 -0
  36. package/generators/micro-react/templates/apps/layout/src/components/IconFont/index.tsx +1 -1
  37. package/generators/micro-react/templates/apps/layout/src/components/MicroAppLoader/index.tsx +1 -1
  38. package/generators/micro-react/templates/apps/layout/src/components/RightContent/AvatarDropdown.tsx +388 -0
  39. package/generators/micro-react/templates/apps/layout/src/components/RightContent/avatar-dropdown.less +35 -0
  40. package/generators/micro-react/templates/apps/layout/src/components/RightContent/index.ts +2 -0
  41. package/generators/micro-react/templates/apps/layout/src/constants/index.ts +2 -0
  42. package/generators/micro-react/templates/apps/layout/src/global.less +3 -6
  43. package/generators/micro-react/templates/apps/layout/src/layouts/components/header/index.less +3 -1
  44. package/generators/micro-react/templates/apps/layout/src/layouts/components/header/index.tsx +9 -6
  45. package/generators/micro-react/templates/apps/layout/src/layouts/components/menu/index.less +3 -1
  46. package/generators/micro-react/templates/apps/layout/src/layouts/components/menu/index.tsx +4 -4
  47. package/generators/micro-react/templates/apps/layout/src/layouts/index.less +83 -13
  48. package/generators/micro-react/templates/apps/layout/src/layouts/index.tsx +40 -25
  49. package/generators/micro-react/templates/apps/layout/src/locales/en-US.ts +9 -0
  50. package/generators/micro-react/templates/apps/layout/src/locales/zh-CN.ts +9 -0
  51. package/generators/micro-react/templates/apps/layout/src/pages/403/index.tsx +2 -1
  52. package/generators/micro-react/templates/apps/layout/src/pages/404/index.tsx +1 -1
  53. package/generators/micro-react/templates/apps/layout/src/pages/Home/index.less +3 -0
  54. package/generators/micro-react/templates/apps/layout/src/pages/User/Login/index.less +1 -1
  55. package/generators/micro-react/templates/apps/layout/src/pages/User/Login/index.tsx +3 -3
  56. package/generators/micro-react/templates/apps/layout/src/requestErrorConfig.ts +1 -1
  57. package/generators/micro-react/templates/apps/layout/src/services/config/index.ts +63 -0
  58. package/generators/micro-react/templates/apps/layout/src/services/config/type.ts +32 -0
  59. package/generators/micro-react/templates/apps/layout/src/services/user.ts +23 -1
  60. package/generators/micro-react/templates/package.json +4 -1
  61. package/generators/micro-react/templates/packages/common-intl/.turbo/turbo-build.log +13 -0
  62. package/generators/micro-react/templates/packages/common-intl/README.md +428 -0
  63. package/generators/micro-react/templates/packages/common-intl/dist/index.d.ts +3 -0
  64. package/generators/micro-react/templates/packages/common-intl/dist/index.js +4388 -0
  65. package/generators/micro-react/templates/packages/common-intl/dist/indexedDBUtils.d.ts +13 -0
  66. package/generators/micro-react/templates/packages/common-intl/dist/intl.d.ts +1022 -0
  67. package/generators/micro-react/templates/packages/common-intl/dist/utils.d.ts +122 -0
  68. package/generators/micro-react/templates/packages/common-intl/package.json +34 -0
  69. package/generators/micro-react/templates/packages/common-intl/src/index.ts +7 -0
  70. package/generators/micro-react/templates/packages/common-intl/src/indexedDBUtils.ts +51 -0
  71. package/generators/micro-react/templates/packages/common-intl/src/intl.ts +5709 -0
  72. package/generators/micro-react/templates/packages/common-intl/src/utils.ts +481 -0
  73. package/generators/micro-react/templates/packages/common-intl/tsconfig.json +22 -0
  74. package/generators/micro-react/templates/packages/common-intl/vite.config.ts +25 -0
  75. package/generators/micro-react/templates/turbo.json +3 -0
  76. package/generators/subapp-react/index.js +13 -18
  77. package/generators/subapp-react/templates/homepage/README.md +3 -3
  78. package/generators/subapp-react/templates/homepage/config/config.dev.ts +8 -7
  79. package/generators/subapp-react/templates/homepage/config/config.prod.development.ts +2 -3
  80. package/generators/subapp-react/templates/homepage/config/config.prod.testing.ts +2 -3
  81. package/generators/subapp-react/templates/homepage/config/config.prod.ts +4 -5
  82. package/generators/subapp-react/templates/homepage/package.json +3 -2
  83. package/generators/subapp-react/templates/homepage/src/global.less +3 -3
  84. package/generators/subapp-react/templates/homepage/src/pages/index.less +2 -2
  85. package/generators/subapp-react/templates/homepage/src/pages/index.tsx +72 -33
  86. package/generators/subapp-react/templates/homepage/src/styles/theme.less +1 -1
  87. package/lib/utils.js +43 -0
  88. package/package.json +8 -11
  89. package/generators/micro-react/templates/packages/shared-styles/README.md +0 -124
  90. package/generators/micro-react/templates/packages/shared-styles/arco-design-mobile-override.less +0 -91
  91. package/generators/micro-react/templates/packages/shared-styles/arco-override.less +0 -119
  92. package/generators/micro-react/templates/packages/shared-styles/index.d.ts +0 -44
  93. package/generators/micro-react/templates/packages/shared-styles/index.less +0 -13
  94. package/generators/micro-react/templates/packages/shared-styles/package.json +0 -30
  95. package/generators/micro-react/templates/packages/shared-styles/theme-inject.less +0 -10
  96. package/generators/micro-react/templates/packages/shared-styles/themes/dark/custom-var.less +0 -290
  97. package/generators/micro-react/templates/packages/shared-styles/themes/normal/custom-var.less +0 -269
  98. package/generators/micro-react/templates/packages/shared-styles/variables-only.less +0 -433
  99. package/generators/micro-react/templates/packages/shared-styles/variables.less +0 -452
@@ -0,0 +1,428 @@
1
+ # @portal-web/common-intl
2
+
3
+ 通用国际化(i18n)库,用于 Portal 多应用间的多语言文案管理。
4
+
5
+ ## 功能特性
6
+
7
+ - **跨端兼容**:不内置 UI 提示能力,通过外部注入 `messageInstance` 实现跨平台兼容
8
+ - **缓存机制**:支持 IndexedDB 持久化存储 + 内存缓存双重保障
9
+ - **插值替换**:支持 `%AA%`、`%BB%` 等占位符的动态替换
10
+ - **降级处理**:当翻译 key 不存在时,自动返回兜底文案
11
+ - **环境感知**:自动根据 `NODE_ENV` 切换开发/生产 API 地址
12
+ - **微前端共享**:主应用获取的文案数据自动共享给所有子应用
13
+
14
+ ## 安装
15
+
16
+ 在 monorepo 项目中,通过 workspace 引用:
17
+
18
+ ```json
19
+ {
20
+ "dependencies": {
21
+ "@portal-web/common-intl": "workspace:*"
22
+ }
23
+ }
24
+ ```
25
+
26
+ ## 两种使用模式
27
+
28
+ 根据应用架构的不同,`@portal-web/common-intl` 支持两种使用模式:
29
+
30
+ | 模式 | 适用场景 | 示例应用 |
31
+ | ------------ | ------------------------------------------------ | ------------------------------------------------------------ |
32
+ | 微前端模式 | qiankun 主子应用架构,主应用获取文案后子应用共享 | layout、conversation-v2、session、workorder、faq、permission |
33
+ | 独立应用模式 | 独立运行的应用,自己获取和管理文案 | mico-cs-mobile |
34
+
35
+ ### 微前端模式
36
+
37
+ 在 qiankun 微前端架构中,**主应用负责获取文案**,子应用直接使用共享的文案对象,无需重复初始化。
38
+
39
+ **工作原理**:
40
+
41
+ - 主应用调用 `initIntl()` 获取远程文案
42
+ - 文案数据存储到 `window.__MICO_COMMON_INTL_TRANSLATIONS__`
43
+ - 子应用直接使用 `intl` 对象,自动从 window 读取共享数据
44
+
45
+ **主应用配置**(参考 `apps/layout/src/app.tsx`):
46
+
47
+ ```typescript
48
+ import {
49
+ getCurrentLocale,
50
+ initIntl,
51
+ type ILang,
52
+ } from "@portal-web/common-intl";
53
+ import { Message } from "@arco-design/web-react";
54
+ import { request } from "@umijs/max";
55
+
56
+ // 初始化国际化模块
57
+ const fetchMultilingualData = initIntl({
58
+ requestInstance: request,
59
+ messageInstance: {
60
+ error: (msg: string) => Message.error(msg),
61
+ warning: (msg: string) => Message.warning(msg),
62
+ },
63
+ fetchMultilingualDataParams: {
64
+ tag: "cs_fe_pc",
65
+ lang: getCurrentLocale() as ILang,
66
+ app_name: "middle",
67
+ },
68
+ indexedDBParams: {
69
+ dbName: "mico_cs_web_i18n_db",
70
+ },
71
+ });
72
+
73
+ // 在应用渲染前加载多语言数据
74
+ export function render(oldRender: () => void): void {
75
+ fetchMultilingualData()
76
+ .then(oldRender)
77
+ .catch((error: Error) => {
78
+ console.error("获取多语言文案失败", error);
79
+ oldRender(); // 失败时继续渲染,使用兜底文案
80
+ });
81
+ }
82
+ ```
83
+
84
+ **子应用使用**(无需初始化):
85
+
86
+ ```typescript
87
+ // apps/conversation-v2、apps/session、apps/workorder 等子应用
88
+ import { intl } from "@portal-web/common-intl";
89
+
90
+ // 直接使用,文案来自主应用
91
+ <h1>{intl.cs_web_workbench_conversation_record()}</h1>;
92
+ ```
93
+
94
+ ### 独立应用模式
95
+
96
+ 对于非 qiankun 架构的独立应用(如 H5 应用),需要自己调用 `initIntl()` 获取文案,并维护独立的文案文件。
97
+
98
+ **独立应用配置**(参考 `apps/mico-cs-mobile/src/app.ts`):
99
+
100
+ ```typescript
101
+ import { initIntl } from "@portal-web/common-intl";
102
+ import { Toast } from "@arco-design/mobile-react";
103
+ import { request } from "@umijs/max";
104
+ import { convertLocaleToLangParam } from "./locales/utils";
105
+ import { getSearchParams } from "@/common/jsbridge/jsbridge";
106
+
107
+ // 初始化国际化模块
108
+ const fetchMultilingualData = initIntl({
109
+ // 请求实例(必填)
110
+ requestInstance: request,
111
+ // 消息提示实例(必填)
112
+ messageInstance: {
113
+ error: (msg: string) => Toast.error(msg),
114
+ warning: (msg: string) => Toast.warn(msg),
115
+ },
116
+ // 多语言中台接口参数(必填)
117
+ fetchMultilingualDataParams: {
118
+ tag: 'cs_fe_mobile', // 多语言标签,参考翻译中台-文案管理列表页-Tags 表格列 https://lang-test.micoplatform.com/lang/#/langs
119
+ lang: convertLocaleToLangParam(getSearchParams().language as string),
120
+ app_name: 'middle', // 应用名称,参考翻译中台左上角 https://lang-test.micoplatform.com/lang/#/langs
121
+ },
122
+ // IndexedDB 参数(可选)
123
+ indexedDBParams: {
124
+ dbName: 'mico_cs_mobile_i18n_db',
125
+ },
126
+ });
127
+
128
+ // 在应用渲染前加载多语言数据
129
+ // 如果是基于 umi 的项目,可以在 app.tsx 的 render 函数中调用加载函数 https://umijs.org/docs/api/runtime-config#render
130
+ export function render(oldRender: () => void): void {
131
+ fetchMultilingualData()
132
+ .then(oldRender)
133
+ .catch((error: Error) => {
134
+ console.error('获取多语言文案失败', error);
135
+ // 即使失败也继续渲染,使用兜底文案
136
+ oldRender();
137
+ });
138
+ }
139
+ ```
140
+
141
+ **独立应用的文案文件**(`apps/mico-cs-mobile/src/locales/index.ts`):集中管理所有国际化文案
142
+
143
+ ```typescript
144
+ import { i18n } from "@portal-web/common-intl";
145
+
146
+ const intl = {
147
+ // 无插值示例
148
+ sdk_h5_ticket_record: () =>
149
+ i18n({
150
+ key: 'sdk_h5_ticket_record',
151
+ defaultMessage: '工单记录',
152
+ }),
153
+
154
+ // 单个插值示例
155
+ sdk_h5_common_request_failed_aa: (code: string | number) =>
156
+ i18n({
157
+ key: 'sdk_h5_common_request_failed_aa',
158
+ interpolations: [code],
159
+ defaultMessage: `请求失败 (${code})`,
160
+ }),
161
+
162
+ // 多个插值示例
163
+ sdk_h5_common_upload_failed_status_aa_bb: (
164
+ statusCode: string | number,
165
+ responseText: string,
166
+ ) =>
167
+ i18n({
168
+ key: 'sdk_h5_common_upload_failed_status_aa_bb',
169
+ interpolations: [statusCode, responseText],
170
+ defaultMessage: `上传失败,状态码 ${statusCode}${responseText}`,
171
+ }),
172
+ };
173
+
174
+ export default intl;
175
+ ```
176
+
177
+ **在组件中使用**:
178
+
179
+ ```tsx
180
+ import intl from '@/locales';
181
+
182
+ function MyComponent() {
183
+ return (
184
+ <div>
185
+ <h1>{intl.sdk_h5_ticket_record()}</h1>
186
+ <p>{intl.sdk_h5_common_request_failed_aa(404)}</p>
187
+ </div>
188
+ );
189
+ }
190
+ ```
191
+
192
+ ## API 参考
193
+
194
+ ### `initIntl(params: IInitIntlParams)`
195
+
196
+ 初始化国际化模块,返回 `fetchMultilingualData` 函数。
197
+
198
+ #### 参数
199
+
200
+ | 参数 | 类型 | 必填 | 说明 |
201
+ | -------------------------------------- | ------------------ | ---- | -------------------------------------------- |
202
+ | `requestInstance` | `Function` | ✅ | HTTP 请求函数,用于请求多语言中台接口 |
203
+ | `messageInstance` | `IMessageInstance` | ✅ | 消息提示实例,包含 `error` 和 `warning` 方法 |
204
+ | `fetchMultilingualDataParams` | `Object` | ✅ | 多语言接口参数 |
205
+ | `fetchMultilingualDataParams.tag` | `string` | ✅ | 多语言标签 |
206
+ | `fetchMultilingualDataParams.lang` | `ILang` | ✅ | 语言类型 |
207
+ | `fetchMultilingualDataParams.app_name` | `string` | ✅ | 应用名称 |
208
+ | `indexedDBParams` | `Object` | ❌ | IndexedDB 配置参数 |
209
+
210
+ #### 返回值
211
+
212
+ 返回 `fetchMultilingualData(): Promise<Record<string, string>>` 函数,调用后会:
213
+
214
+ 1. 从多语言中台拉取翻译数据
215
+ 2. 将数据存储到 IndexedDB 和全局变量缓存(`window.__MICO_COMMON_INTL_TRANSLATIONS__`)
216
+ 3. 返回翻译数据对象
217
+
218
+ ### `i18n(params: II18nParams)`
219
+
220
+ 国际化翻译函数,同步返回翻译后的文案。
221
+
222
+ #### 参数
223
+
224
+ | 参数 | 类型 | 必填 | 说明 |
225
+ | ---------------- | ------------------------- | ---- | -------------------------------------------- |
226
+ | `key` | `string` | ✅ | 翻译文案的 key |
227
+ | `interpolations` | `Array<string \| number>` | ❌ | 插值数组,按顺序替换 `%AA%`、`%BB%` 等占位符 |
228
+ | `defaultMessage` | `string` | ✅ | 兜底文案,当找不到翻译时返回 |
229
+
230
+ #### 返回值
231
+
232
+ 返回翻译后的字符串。
233
+
234
+ ### `getCurrentLocale()`
235
+
236
+ 获取当前语言环境,按优先级从高到低:
237
+
238
+ 1. URL 参数 `lang`
239
+ 2. localStorage 中的 `umi_locale`
240
+ 3. 浏览器默认语言
241
+
242
+ #### 返回值
243
+
244
+ 返回当前语言类型 `ILang`。
245
+
246
+ ### `setLocaleToStorage(locale: ILang)`
247
+
248
+ 将语言设置保存到 localStorage。
249
+
250
+ ### `LANG`
251
+
252
+ 支持的语言常量对象:
253
+
254
+ ```typescript
255
+ const LANG = {
256
+ ZH_CN: "zh_CN", // 简体中文
257
+ EN: "en", // 英语
258
+ AR: "ar", // 阿拉伯语
259
+ TR: "tr", // 土耳其语
260
+ } as const;
261
+ ```
262
+
263
+ ### `LOCALE`
264
+
265
+ `LANG` 的别名,用于兼容现有代码。
266
+
267
+ ### `SUPPORTED_LOCALES`
268
+
269
+ 支持的语言列表数组:`['zh_CN', 'en', 'ar', 'tr']`
270
+
271
+ ### 类型定义
272
+
273
+ ```typescript
274
+ // 语言类型
275
+ type ILang = "zh_CN" | "en" | "ar" | "tr";
276
+
277
+ // TLocale 是 ILang 的别名,用于兼容
278
+ type TLocale = ILang;
279
+
280
+ // 消息提示实例接口
281
+ interface IMessageInstance {
282
+ error: (message: string) => unknown;
283
+ warning: (message: string) => unknown;
284
+ }
285
+
286
+ // 初始化参数接口
287
+ interface IInitIntlParams {
288
+ requestInstance: (
289
+ url: string,
290
+ options: { method: string; params: Record<string, string> }
291
+ ) => Promise<{
292
+ code: number;
293
+ data?: Record<string, string>;
294
+ errorMessage?: string;
295
+ }>;
296
+ messageInstance: IMessageInstance;
297
+ fetchMultilingualDataParams: {
298
+ tag: string;
299
+ lang: ILang;
300
+ app_name: string;
301
+ };
302
+ indexedDBParams?: {
303
+ dbName?: string;
304
+ dbVersion?: number;
305
+ storeName?: string;
306
+ keyPathKey?: string;
307
+ };
308
+ }
309
+
310
+ // i18n 函数参数接口
311
+ interface II18nParams {
312
+ key: string;
313
+ interpolations?: Array<string | number | undefined>;
314
+ defaultMessage: string;
315
+ }
316
+ ```
317
+
318
+ ## 导出清单
319
+
320
+ | 导出 | 类型 | 说明 |
321
+ | -------------------- | ---- | --------------------------------------------------------- |
322
+ | `initIntl` | 函数 | 初始化国际化模块,返回 `fetchMultilingualData` 函数 |
323
+ | `i18n` | 函数 | 翻译函数,用于获取单条翻译文案 |
324
+ | `intl` | 对象 | PC 端主子应用共享的文案对象(仅供 layout 及其子应用使用) |
325
+ | `LANG` | 常量 | 支持的语言常量:`ZH_CN`、`EN`、`AR`、`TR` |
326
+ | `LOCALE` | 常量 | `LANG` 的别名,用于兼容 |
327
+ | `SUPPORTED_LOCALES` | 常量 | 支持的语言列表数组 |
328
+ | `getCurrentLocale` | 函数 | 获取当前语言环境(按优先级:URL > localStorage > 浏览器) |
329
+ | `setLocaleToStorage` | 函数 | 设置语言到 localStorage |
330
+ | `ILang` | 类型 | 语言类型定义 |
331
+ | `TLocale` | 类型 | `ILang` 的别名,用于兼容 |
332
+
333
+ ## 占位符规则
334
+
335
+ 翻译文案中的占位符使用大写字母对表示:
336
+
337
+ | 占位符 | 对应 interpolations 索引 |
338
+ | ------ | ------------------------ |
339
+ | `%AA%` | `interpolations[0]` |
340
+ | `%BB%` | `interpolations[1]` |
341
+ | `%CC%` | `interpolations[2]` |
342
+ | ... | ... |
343
+ | `%ZZ%` | `interpolations[25]` |
344
+
345
+ ### 示例
346
+
347
+ ```typescript
348
+ // 翻译文案: "共 %AA% 条记录,第 %BB% 页"
349
+ i18n({
350
+ key: "pagination_info",
351
+ interpolations: [100, 1],
352
+ defaultMessage: `共 100 条记录,第 1 页`,
353
+ });
354
+ // 输出: "共 100 条记录,第 1 页"
355
+ ```
356
+
357
+ ## 缓存策略
358
+
359
+ 1. **内存缓存**:翻译数据首先存储在 `window.__MICO_COMMON_INTL_TRANSLATIONS__` 全局变量中,保证 `i18n()` 函数的同步调用,同时支持微前端主子应用间的数据共享
360
+ 2. **IndexedDB 缓存**:同时持久化到 IndexedDB,支持离线访问和页面刷新后的快速加载
361
+ 3. **降级处理**:当 IndexedDB 不可用时(如某些 WebView 环境),仅使用内存缓存
362
+
363
+ ## 添加新翻译
364
+
365
+ 1. 在 `src/intl.ts` 中添加新的 key:
366
+
367
+ ```typescript
368
+ const intl = {
369
+ // ... 其他 keys ...
370
+
371
+ // 新增的 key(无参数)
372
+ cs_web_your_new_key: () =>
373
+ i18n({
374
+ key: "cs_web_your_new_key",
375
+ defaultMessage: "你的新文案",
376
+ }),
377
+
378
+ // 带参数的 key(单个参数)
379
+ cs_web_your_new_key_aa: (param: string | number) =>
380
+ i18n({
381
+ key: "cs_web_your_new_key_aa",
382
+ interpolations: [param],
383
+ defaultMessage: `你的新文案 ${param}`,
384
+ }),
385
+
386
+ // 带参数的 key(多个参数)
387
+ cs_web_your_new_key_multi: (a: string | number, b: string | number) =>
388
+ i18n({
389
+ key: "cs_web_your_new_key_multi",
390
+ interpolations: [a, b],
391
+ defaultMessage: `参数A: ${a},参数B: ${b}`,
392
+ }),
393
+ };
394
+ ```
395
+
396
+ 2. 在业务代码中使用:
397
+
398
+ ```typescript
399
+ import { intl } from "@portal-web/common-intl";
400
+
401
+ // 使用新 key
402
+ intl.cs_web_your_new_key();
403
+ intl.cs_web_your_new_key_aa("参数值");
404
+ intl.cs_web_your_new_key_multi("值A", "值B");
405
+ ```
406
+
407
+ 3. 把 key 和兜底文案 defaultMessage 添加到 https://micoworld.feishu.cn/wiki/Ak4pwBl86ixhXyk39bKcgQbknTf?sheet=MzAybi ,并上传到翻译中台
408
+
409
+ ## 开发命令
410
+
411
+ ```bash
412
+ # 开发模式(监听文件变化)
413
+ pnpm dev
414
+
415
+ # 构建
416
+ pnpm build
417
+ ```
418
+
419
+ ## 注意事项
420
+
421
+ 本项目暂时没有独立发包。各个业务应用 (/apps/\*) 通过 pnpm workspace 引入本项目,可参考 [/apps/mico-cs-mobile](../../apps/mico-cs-mobile/package.json)。原因:本项目代码量较少,暂时由各个业务应用各自把本项目打包到产物中,后续有必要时再优化。
422
+
423
+ 1. **不要直接调用 `i18n` 函数**:应通过 `intl.xxx()` 方式调用
424
+ 2. **参数类型**:插值参数支持 `string | number | undefined`。无插值时,`interpolations` 参数可以不传
425
+ 3. **默认文案**:每个 key 都需要提供 `defaultMessage` 作为后备
426
+ 4. **TypeScript 支持**:`intl` 对象有完整的类型提示
427
+ 5. **同步调用**:`i18n` 函数是同步的,从全局变量缓存读取数据,因此必须确保在 `fetchMultilingualData()` 完成后才能使用
428
+ 6. **离线支持**:即使网络请求失败,也会尝试从 IndexedDB 读取历史缓存,保证基本可用性
@@ -0,0 +1,3 @@
1
+ export * from './utils';
2
+ export { LANG as LOCALE, type ILang as TLocale } from './utils';
3
+ export { default as intl } from './intl';