tang-ui-x 1.3.1 → 1.3.3
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 +180 -158
- package/components/TActionSheet/index.uvue +54 -38
- package/components/TAvatar/index.uvue +53 -39
- package/components/TBadge/index.uvue +4 -4
- package/components/TButton/index.uvue +121 -121
- package/components/TCard/index.uvue +48 -48
- package/components/TCheckbox/index.uvue +61 -53
- package/components/TCheckboxGroup/index.uvue +33 -21
- package/components/TCol/index.uvue +8 -8
- package/components/TCollapse/index.uvue +21 -18
- package/components/TCollapseItem/index.uvue +53 -57
- package/components/TDateTimePicker/README.md +31 -31
- package/components/TDateTimePicker/index.uvue +708 -388
- package/components/TDialog/index.uvue +66 -54
- package/components/TEmpty/index.uvue +56 -41
- package/components/TErrorState/index.uvue +38 -38
- package/components/TForm/index.uvue +182 -119
- package/components/TForm/type.uts +6 -9
- package/components/TGrid/index.uvue +2 -7
- package/components/TGridItem/index.uvue +75 -51
- package/components/TIcon/index.uvue +6 -4
- package/components/TImage/index.uvue +53 -49
- package/components/TInput/index.uvue +178 -147
- package/components/TList/index.uvue +30 -26
- package/components/TListItem/index.uvue +49 -29
- package/components/TLoading/index.uvue +31 -27
- package/components/TNavBar/index.uvue +37 -27
- package/components/TNoticeBar/index.uvue +19 -11
- package/components/TNumberInput/index.uvue +16 -7
- package/components/TPicker/index.uvue +61 -54
- package/components/TPopup/index.uvue +10 -10
- package/components/TRadioButton/index.uvue +100 -64
- package/components/TRadioGroup/index.uvue +35 -22
- package/components/TRate/index.uvue +33 -21
- package/components/TSearchBar/index.uvue +54 -54
- package/components/TSelect/index.uvue +94 -73
- package/components/TSlider/index.uvue +15 -15
- package/components/TSwiper/index.uvue +71 -71
- package/components/TSwitch/index.uvue +157 -149
- package/components/TSwitch/type.uts +6 -0
- package/components/TTextarea/index.uvue +57 -45
- package/components/Tabs/index.uvue +21 -7
- package/components/Tags/index.uvue +30 -19
- package/composables/i18n/manager.uts +68 -126
- package/composables/i18n/types.uts +6 -10
- package/composables/useI18n.uts +3 -3
- package/composables/useModal.uts +16 -16
- package/composables/useTheme.uts +105 -124
- package/composables/useToast.uts +24 -8
- package/index.uts +2 -2
- package/package.json +37 -7
- package/static/tailwind.css +172 -396
- package/types/index.d.ts +251 -0
- package/composables/i18n/manager-demo.uts +0 -104
- package/composables/i18n/manager.test.uts +0 -182
- package/composables/i18n/register-demo.uts +0 -125
- package/composables/i18n/task22-verification.uts +0 -198
- package/composables/i18n/task23-verification.uts +0 -343
- package/composables/i18n/task8-demo.uts +0 -93
- package/composables/i18n/task8-verification.uts +0 -98
- package/composables/i18n/test-task23.uts +0 -9
- package/composables/i18n/useI18n-verification.uts +0 -105
- package/composables/i18n/validation-demo.uts +0 -45
- package/composables/i18n/validation-test.uts +0 -106
- package/locales/cross-platform-verification.uts +0 -510
- package/locales/init-verification.uts +0 -101
- package/locales/run-verification.uts +0 -16
package/README.md
CHANGED
|
@@ -65,8 +65,8 @@ pnpm add tang-ui-x
|
|
|
65
65
|
</template>
|
|
66
66
|
|
|
67
67
|
<script module="TButton" lang="uts">
|
|
68
|
-
|
|
69
|
-
|
|
68
|
+
import TButton from 'tang-ui-x/components/TButton/index.uvue'
|
|
69
|
+
export default TButton
|
|
70
70
|
</script>
|
|
71
71
|
```
|
|
72
72
|
|
|
@@ -178,37 +178,37 @@ const languages = getAvailableLocales() // ['zh-CN', 'en-US', 'zh-TW']
|
|
|
178
178
|
添加组件库未内置的语言(如日语、韩语等):
|
|
179
179
|
|
|
180
180
|
```typescript
|
|
181
|
-
import { registerLocale, setLanguage } from
|
|
181
|
+
import { registerLocale, setLanguage } from "tang-ui-x";
|
|
182
182
|
|
|
183
183
|
// 注册日语语言包
|
|
184
|
-
registerLocale(
|
|
184
|
+
registerLocale("ja-JP", {
|
|
185
185
|
common: {
|
|
186
|
-
confirm:
|
|
187
|
-
cancel:
|
|
188
|
-
ok:
|
|
189
|
-
close:
|
|
190
|
-
loading:
|
|
191
|
-
noData:
|
|
186
|
+
confirm: "確認",
|
|
187
|
+
cancel: "キャンセル",
|
|
188
|
+
ok: "OK",
|
|
189
|
+
close: "閉じる",
|
|
190
|
+
loading: "読み込み中...",
|
|
191
|
+
noData: "データなし",
|
|
192
192
|
},
|
|
193
193
|
dialog: {
|
|
194
|
-
title:
|
|
195
|
-
confirmText:
|
|
196
|
-
cancelText:
|
|
194
|
+
title: "ヒント",
|
|
195
|
+
confirmText: "確認",
|
|
196
|
+
cancelText: "キャンセル",
|
|
197
197
|
},
|
|
198
198
|
empty: {
|
|
199
|
-
title:
|
|
200
|
-
description:
|
|
201
|
-
actionText:
|
|
199
|
+
title: "データなし",
|
|
200
|
+
description: "関連コンテンツがありません",
|
|
201
|
+
actionText: "更新",
|
|
202
202
|
},
|
|
203
203
|
searchBar: {
|
|
204
|
-
placeholder:
|
|
205
|
-
cancelText:
|
|
206
|
-
}
|
|
204
|
+
placeholder: "検索キーワードを入力",
|
|
205
|
+
cancelText: "キャンセル",
|
|
206
|
+
},
|
|
207
207
|
// ... 其他组件模块
|
|
208
|
-
})
|
|
208
|
+
});
|
|
209
209
|
|
|
210
210
|
// 切换到日语
|
|
211
|
-
setLanguage(
|
|
211
|
+
setLanguage("ja-JP");
|
|
212
212
|
```
|
|
213
213
|
|
|
214
214
|
#### 方式二:覆盖默认翻译
|
|
@@ -216,17 +216,17 @@ setLanguage('ja-JP')
|
|
|
216
216
|
只覆盖部分翻译文本,其他保持默认:
|
|
217
217
|
|
|
218
218
|
```typescript
|
|
219
|
-
import { registerLocale } from
|
|
219
|
+
import { registerLocale } from "tang-ui-x";
|
|
220
220
|
|
|
221
221
|
// 自定义简体中文的部分翻译
|
|
222
|
-
registerLocale(
|
|
222
|
+
registerLocale("zh-CN", {
|
|
223
223
|
common: {
|
|
224
|
-
confirm:
|
|
225
|
-
cancel:
|
|
224
|
+
confirm: "好的", // 覆盖默认的"确定"
|
|
225
|
+
cancel: "算了", // 覆盖默认的"取消"
|
|
226
226
|
// 其他键(ok, close, loading 等)保持默认值
|
|
227
|
-
}
|
|
227
|
+
},
|
|
228
228
|
// 其他模块(dialog, empty 等)保持默认翻译
|
|
229
|
-
})
|
|
229
|
+
});
|
|
230
230
|
```
|
|
231
231
|
|
|
232
232
|
### 语言包注册模式
|
|
@@ -238,6 +238,7 @@ Tang UI X 提供了两种语言包注册模式,以满足不同的使用场景
|
|
|
238
238
|
合并模式会将新语言包与现有语言包合并,新内容覆盖旧内容,但未覆盖的内容会被保留。这是 `registerLocale` 函数的默认行为。
|
|
239
239
|
|
|
240
240
|
**使用场景:**
|
|
241
|
+
|
|
241
242
|
- 只想覆盖部分翻译文本
|
|
242
243
|
- 添加新的翻译键而不影响现有翻译
|
|
243
244
|
- 逐步自定义组件库的翻译
|
|
@@ -245,17 +246,17 @@ Tang UI X 提供了两种语言包注册模式,以满足不同的使用场景
|
|
|
245
246
|
**示例 1:覆盖部分通用文本**
|
|
246
247
|
|
|
247
248
|
```typescript
|
|
248
|
-
import { registerLocale } from
|
|
249
|
+
import { registerLocale } from "tang-ui-x";
|
|
249
250
|
|
|
250
251
|
// 只覆盖 common 模块的部分键
|
|
251
|
-
registerLocale(
|
|
252
|
+
registerLocale("zh-CN", {
|
|
252
253
|
common: {
|
|
253
|
-
confirm:
|
|
254
|
-
cancel:
|
|
254
|
+
confirm: "好的", // 覆盖默认的"确定"
|
|
255
|
+
cancel: "算了", // 覆盖默认的"取消"
|
|
255
256
|
// ok, close, loading, noData 等其他键保持默认值
|
|
256
|
-
}
|
|
257
|
+
},
|
|
257
258
|
// dialog, empty, searchBar 等其他模块保持默认翻译
|
|
258
|
-
})
|
|
259
|
+
});
|
|
259
260
|
|
|
260
261
|
// 结果:
|
|
261
262
|
// - common.confirm → "好的"(已覆盖)
|
|
@@ -269,21 +270,21 @@ registerLocale('zh-CN', {
|
|
|
269
270
|
**示例 2:添加新的翻译键**
|
|
270
271
|
|
|
271
272
|
```typescript
|
|
272
|
-
import { registerLocale } from
|
|
273
|
+
import { registerLocale } from "tang-ui-x";
|
|
273
274
|
|
|
274
275
|
// 为现有语言添加新的翻译键
|
|
275
|
-
registerLocale(
|
|
276
|
+
registerLocale("zh-CN", {
|
|
276
277
|
common: {
|
|
277
|
-
save:
|
|
278
|
-
delete:
|
|
279
|
-
edit:
|
|
278
|
+
save: "保存", // 新增键
|
|
279
|
+
delete: "删除", // 新增键
|
|
280
|
+
edit: "编辑", // 新增键
|
|
280
281
|
},
|
|
281
282
|
// 添加新的自定义模块
|
|
282
283
|
myComponent: {
|
|
283
|
-
title:
|
|
284
|
-
description:
|
|
285
|
-
}
|
|
286
|
-
})
|
|
284
|
+
title: "我的组件",
|
|
285
|
+
description: "这是自定义组件",
|
|
286
|
+
},
|
|
287
|
+
});
|
|
287
288
|
|
|
288
289
|
// 原有的所有翻译都会保留,只是新增了这些键
|
|
289
290
|
```
|
|
@@ -291,14 +292,18 @@ registerLocale('zh-CN', {
|
|
|
291
292
|
**示例 3:使用 registerLocaleWithMode 显式指定合并模式**
|
|
292
293
|
|
|
293
294
|
```typescript
|
|
294
|
-
import { registerLocaleWithMode } from
|
|
295
|
+
import { registerLocaleWithMode } from "tang-ui-x";
|
|
295
296
|
|
|
296
297
|
// 显式指定合并模式(与 registerLocale 效果相同)
|
|
297
|
-
registerLocaleWithMode(
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
298
|
+
registerLocaleWithMode(
|
|
299
|
+
"zh-CN",
|
|
300
|
+
{
|
|
301
|
+
common: {
|
|
302
|
+
confirm: "好的",
|
|
303
|
+
},
|
|
304
|
+
},
|
|
305
|
+
"merge",
|
|
306
|
+
);
|
|
302
307
|
```
|
|
303
308
|
|
|
304
309
|
#### 替换模式
|
|
@@ -306,6 +311,7 @@ registerLocaleWithMode('zh-CN', {
|
|
|
306
311
|
替换模式会完全替换现有语言包,不保留任何原有内容。使用此模式时,您需要提供完整的语言包,否则缺失的模块和键将无法使用。
|
|
307
312
|
|
|
308
313
|
**使用场景:**
|
|
314
|
+
|
|
309
315
|
- 完全自定义某个语言的所有翻译
|
|
310
316
|
- 从外部 JSON 文件加载完整的语言包
|
|
311
317
|
- 确保语言包的完整性和一致性
|
|
@@ -313,34 +319,34 @@ registerLocaleWithMode('zh-CN', {
|
|
|
313
319
|
**方式 1:使用 replaceLocale 函数**
|
|
314
320
|
|
|
315
321
|
```typescript
|
|
316
|
-
import { replaceLocale } from
|
|
322
|
+
import { replaceLocale } from "tang-ui-x";
|
|
317
323
|
|
|
318
324
|
// 完全替换简体中文语言包
|
|
319
|
-
replaceLocale(
|
|
325
|
+
replaceLocale("zh-CN", {
|
|
320
326
|
common: {
|
|
321
|
-
confirm:
|
|
322
|
-
cancel:
|
|
323
|
-
ok:
|
|
324
|
-
close:
|
|
325
|
-
loading:
|
|
326
|
-
noData:
|
|
327
|
+
confirm: "好的",
|
|
328
|
+
cancel: "取消",
|
|
329
|
+
ok: "OK",
|
|
330
|
+
close: "关闭",
|
|
331
|
+
loading: "加载中...",
|
|
332
|
+
noData: "暂无数据",
|
|
327
333
|
},
|
|
328
334
|
dialog: {
|
|
329
|
-
title:
|
|
330
|
-
confirmText:
|
|
331
|
-
cancelText:
|
|
335
|
+
title: "提示",
|
|
336
|
+
confirmText: "好的",
|
|
337
|
+
cancelText: "取消",
|
|
332
338
|
},
|
|
333
339
|
empty: {
|
|
334
|
-
title:
|
|
335
|
-
description:
|
|
336
|
-
actionText:
|
|
340
|
+
title: "暂无数据",
|
|
341
|
+
description: "当前没有相关内容",
|
|
342
|
+
actionText: "刷新试试",
|
|
337
343
|
},
|
|
338
344
|
searchBar: {
|
|
339
|
-
placeholder:
|
|
340
|
-
cancelText:
|
|
341
|
-
}
|
|
345
|
+
placeholder: "请输入搜索关键词",
|
|
346
|
+
cancelText: "取消",
|
|
347
|
+
},
|
|
342
348
|
// 必须提供所有组件模块,否则缺失的模块将无法使用
|
|
343
|
-
})
|
|
349
|
+
});
|
|
344
350
|
|
|
345
351
|
// 注意:原有的所有翻译都被清除,只保留新提供的内容
|
|
346
352
|
```
|
|
@@ -348,21 +354,25 @@ replaceLocale('zh-CN', {
|
|
|
348
354
|
**方式 2:使用 registerLocaleWithMode 指定替换模式**
|
|
349
355
|
|
|
350
356
|
```typescript
|
|
351
|
-
import { registerLocaleWithMode } from
|
|
357
|
+
import { registerLocaleWithMode } from "tang-ui-x";
|
|
352
358
|
|
|
353
359
|
// 使用替换模式注册语言包
|
|
354
|
-
registerLocaleWithMode(
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
360
|
+
registerLocaleWithMode(
|
|
361
|
+
"zh-CN",
|
|
362
|
+
{
|
|
363
|
+
common: {
|
|
364
|
+
confirm: "好的",
|
|
365
|
+
cancel: "取消",
|
|
366
|
+
// ... 其他键
|
|
367
|
+
},
|
|
368
|
+
dialog: {
|
|
369
|
+
title: "提示",
|
|
370
|
+
// ... 其他键
|
|
371
|
+
},
|
|
372
|
+
// ... 其他模块
|
|
359
373
|
},
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
// ... 其他键
|
|
363
|
-
}
|
|
364
|
-
// ... 其他模块
|
|
365
|
-
}, 'replace')
|
|
374
|
+
"replace",
|
|
375
|
+
);
|
|
366
376
|
```
|
|
367
377
|
|
|
368
378
|
#### 从 JSON 文件注册语言包
|
|
@@ -402,63 +412,65 @@ registerLocaleWithMode('zh-CN', {
|
|
|
402
412
|
**使用合并模式加载 JSON 文件:**
|
|
403
413
|
|
|
404
414
|
```typescript
|
|
405
|
-
import { registerLocale } from
|
|
406
|
-
import customZhCN from
|
|
415
|
+
import { registerLocale } from "tang-ui-x";
|
|
416
|
+
import customZhCN from "./locales/custom-zh-CN.json";
|
|
407
417
|
|
|
408
418
|
// 合并模式:保留未覆盖的默认翻译
|
|
409
|
-
registerLocale(
|
|
419
|
+
registerLocale("zh-CN", customZhCN);
|
|
410
420
|
```
|
|
411
421
|
|
|
412
422
|
**使用替换模式加载 JSON 文件:**
|
|
413
423
|
|
|
414
424
|
```typescript
|
|
415
|
-
import { replaceLocale } from
|
|
416
|
-
import completeZhCN from
|
|
425
|
+
import { replaceLocale } from "tang-ui-x";
|
|
426
|
+
import completeZhCN from "./locales/complete-zh-CN.json";
|
|
417
427
|
|
|
418
428
|
// 替换模式:完全替换,不保留默认翻译
|
|
419
|
-
replaceLocale(
|
|
429
|
+
replaceLocale("zh-CN", completeZhCN);
|
|
420
430
|
```
|
|
421
431
|
|
|
422
432
|
**从多个 JSON 文件组合语言包:**
|
|
423
433
|
|
|
424
434
|
```typescript
|
|
425
|
-
import { registerLocale } from
|
|
426
|
-
import commonTranslations from
|
|
427
|
-
import dialogTranslations from
|
|
428
|
-
import emptyTranslations from
|
|
435
|
+
import { registerLocale } from "tang-ui-x";
|
|
436
|
+
import commonTranslations from "./locales/common.json";
|
|
437
|
+
import dialogTranslations from "./locales/dialog.json";
|
|
438
|
+
import emptyTranslations from "./locales/empty.json";
|
|
429
439
|
|
|
430
440
|
// 组合多个 JSON 文件
|
|
431
441
|
const customLocale = {
|
|
432
442
|
common: commonTranslations,
|
|
433
443
|
dialog: dialogTranslations,
|
|
434
|
-
empty: emptyTranslations
|
|
435
|
-
}
|
|
444
|
+
empty: emptyTranslations,
|
|
445
|
+
};
|
|
436
446
|
|
|
437
447
|
// 注册组合后的语言包
|
|
438
|
-
registerLocale(
|
|
448
|
+
registerLocale("zh-CN", customLocale);
|
|
439
449
|
```
|
|
440
450
|
|
|
441
451
|
#### 两种模式的对比
|
|
442
452
|
|
|
443
|
-
| 特性
|
|
444
|
-
|
|
445
|
-
| **默认行为**
|
|
446
|
-
| **保留原有内容**
|
|
447
|
-
| **覆盖指定键**
|
|
448
|
-
| **需要完整语言包** | ✗ 否
|
|
449
|
-
| **适合部分自定义** | ✓ 是
|
|
450
|
-
| **适合完全自定义** | ✗ 否
|
|
451
|
-
| **使用函数**
|
|
453
|
+
| 特性 | 合并模式(merge) | 替换模式(replace) |
|
|
454
|
+
| ------------------ | ------------------------------------------------------ | ------------------------------------------------------- |
|
|
455
|
+
| **默认行为** | ✓ 是(registerLocale) | ✗ 否(需显式使用) |
|
|
456
|
+
| **保留原有内容** | ✓ 是 | ✗ 否 |
|
|
457
|
+
| **覆盖指定键** | ✓ 是 | ✓ 是 |
|
|
458
|
+
| **需要完整语言包** | ✗ 否 | ✓ 是 |
|
|
459
|
+
| **适合部分自定义** | ✓ 是 | ✗ 否 |
|
|
460
|
+
| **适合完全自定义** | ✗ 否 | ✓ 是 |
|
|
461
|
+
| **使用函数** | registerLocale<br>registerLocaleWithMode(..., 'merge') | replaceLocale<br>registerLocaleWithMode(..., 'replace') |
|
|
452
462
|
|
|
453
463
|
#### 选择合适的模式
|
|
454
464
|
|
|
455
465
|
**使用合并模式的情况:**
|
|
466
|
+
|
|
456
467
|
- ✓ 只想修改少数几个翻译文本
|
|
457
468
|
- ✓ 想要添加新的翻译键
|
|
458
469
|
- ✓ 希望保留组件库的默认翻译作为后备
|
|
459
470
|
- ✓ 逐步自定义翻译,不想一次性提供所有内容
|
|
460
471
|
|
|
461
472
|
**使用替换模式的情况:**
|
|
473
|
+
|
|
462
474
|
- ✓ 需要完全控制语言包内容
|
|
463
475
|
- ✓ 从外部系统或 CMS 加载完整的翻译
|
|
464
476
|
- ✓ 确保翻译的完整性和一致性
|
|
@@ -509,13 +521,13 @@ async function loadFromJSON() {
|
|
|
509
521
|
// 假设从服务器获取语言包
|
|
510
522
|
const response = await fetch('/api/locales/zh-CN.json')
|
|
511
523
|
const localeData = await response.json()
|
|
512
|
-
|
|
524
|
+
|
|
513
525
|
// 使用合并模式
|
|
514
526
|
registerLocale('zh-CN', localeData)
|
|
515
|
-
|
|
527
|
+
|
|
516
528
|
// 或使用替换模式
|
|
517
529
|
// replaceLocale('zh-CN', localeData)
|
|
518
|
-
|
|
530
|
+
|
|
519
531
|
console.log('已从 JSON 加载语言包')
|
|
520
532
|
} catch (error) {
|
|
521
533
|
console.error('加载语言包失败:', error)
|
|
@@ -528,7 +540,7 @@ function explicitMode() {
|
|
|
528
540
|
registerLocaleWithMode('zh-CN', {
|
|
529
541
|
common: { confirm: '好的' }
|
|
530
542
|
}, 'merge')
|
|
531
|
-
|
|
543
|
+
|
|
532
544
|
// 替换模式
|
|
533
545
|
registerLocaleWithMode('en-US', {
|
|
534
546
|
common: {
|
|
@@ -575,27 +587,27 @@ function explicitMode() {
|
|
|
575
587
|
loading: '加载中...',
|
|
576
588
|
noData: '暂无数据'
|
|
577
589
|
},
|
|
578
|
-
|
|
590
|
+
|
|
579
591
|
// 对话框模块
|
|
580
592
|
dialog: {
|
|
581
593
|
title: '提示',
|
|
582
594
|
confirmText: '确定',
|
|
583
595
|
cancelText: '取消'
|
|
584
596
|
},
|
|
585
|
-
|
|
597
|
+
|
|
586
598
|
// 空状态模块
|
|
587
599
|
empty: {
|
|
588
600
|
title: '暂无数据',
|
|
589
601
|
description: '当前没有相关内容',
|
|
590
602
|
actionText: '刷新试试'
|
|
591
603
|
},
|
|
592
|
-
|
|
604
|
+
|
|
593
605
|
// 搜索框模块
|
|
594
606
|
searchBar: {
|
|
595
607
|
placeholder: '请输入搜索关键词',
|
|
596
608
|
cancelText: '取消'
|
|
597
609
|
}
|
|
598
|
-
|
|
610
|
+
|
|
599
611
|
// ... 其他组件模块
|
|
600
612
|
}
|
|
601
613
|
```
|
|
@@ -618,17 +630,17 @@ function explicitMode() {
|
|
|
618
630
|
<view>
|
|
619
631
|
<!-- 使用默认翻译 -->
|
|
620
632
|
<TEmpty />
|
|
621
|
-
|
|
633
|
+
|
|
622
634
|
<!-- 使用自定义文本(优先级高于翻译) -->
|
|
623
|
-
<TEmpty
|
|
624
|
-
title="没有找到结果"
|
|
635
|
+
<TEmpty
|
|
636
|
+
title="没有找到结果"
|
|
625
637
|
description="请尝试其他搜索条件"
|
|
626
638
|
actionText="重新搜索"
|
|
627
639
|
/>
|
|
628
|
-
|
|
640
|
+
|
|
629
641
|
<!-- 使用默认翻译,但会随语言切换自动更新 -->
|
|
630
642
|
<TSearchBar />
|
|
631
|
-
|
|
643
|
+
|
|
632
644
|
<!-- 使用自定义占位符 -->
|
|
633
645
|
<TSearchBar placeholder="搜索商品" />
|
|
634
646
|
</view>
|
|
@@ -641,24 +653,24 @@ function explicitMode() {
|
|
|
641
653
|
|
|
642
654
|
```typescript
|
|
643
655
|
// 定义带占位符的翻译
|
|
644
|
-
registerLocale(
|
|
656
|
+
registerLocale("zh-CN", {
|
|
645
657
|
common: {
|
|
646
|
-
hello:
|
|
647
|
-
itemCount:
|
|
648
|
-
welcome:
|
|
649
|
-
}
|
|
650
|
-
})
|
|
658
|
+
hello: "你好,{name}",
|
|
659
|
+
itemCount: "共 {count} 个项目",
|
|
660
|
+
welcome: "{user},欢迎回来!",
|
|
661
|
+
},
|
|
662
|
+
});
|
|
651
663
|
|
|
652
664
|
// 使用参数
|
|
653
|
-
const { $t } = useI18n()
|
|
665
|
+
const { $t } = useI18n();
|
|
654
666
|
|
|
655
|
-
const greeting = $t(
|
|
667
|
+
const greeting = $t("common.hello", { name: "张三" });
|
|
656
668
|
// 结果: "你好,张三"
|
|
657
669
|
|
|
658
|
-
const count = $t(
|
|
670
|
+
const count = $t("common.itemCount", { count: 5 });
|
|
659
671
|
// 结果: "共 5 个项目"
|
|
660
672
|
|
|
661
|
-
const welcome = $t(
|
|
673
|
+
const welcome = $t("common.welcome", { user: "Admin" });
|
|
662
674
|
// 结果: "Admin,欢迎回来!"
|
|
663
675
|
```
|
|
664
676
|
|
|
@@ -683,11 +695,11 @@ const description = computed(() => $t('empty.description'))
|
|
|
683
695
|
<text>{{ title }}</text>
|
|
684
696
|
<text>{{ description }}</text>
|
|
685
697
|
<text>当前语言: {{ locale }}</text>
|
|
686
|
-
|
|
698
|
+
|
|
687
699
|
<!-- 组件内的文本也会自动更新 -->
|
|
688
700
|
<TEmpty />
|
|
689
701
|
<TSearchBar />
|
|
690
|
-
|
|
702
|
+
|
|
691
703
|
<button @click="setLocale('en-US')">English</button>
|
|
692
704
|
<button @click="setLocale('zh-CN')">中文</button>
|
|
693
705
|
</view>
|
|
@@ -726,10 +738,10 @@ function switchLanguage(code: string) {
|
|
|
726
738
|
<template>
|
|
727
739
|
<view class="language-switcher">
|
|
728
740
|
<text class="title">选择语言 / Select Language</text>
|
|
729
|
-
|
|
741
|
+
|
|
730
742
|
<view class="language-list">
|
|
731
|
-
<view
|
|
732
|
-
v-for="lang in availableLocales"
|
|
743
|
+
<view
|
|
744
|
+
v-for="lang in availableLocales"
|
|
733
745
|
:key="lang"
|
|
734
746
|
class="language-item"
|
|
735
747
|
:class="{ active: locale === lang }"
|
|
@@ -739,7 +751,7 @@ function switchLanguage(code: string) {
|
|
|
739
751
|
<text v-if="locale === lang" class="check">✓</text>
|
|
740
752
|
</view>
|
|
741
753
|
</view>
|
|
742
|
-
|
|
754
|
+
|
|
743
755
|
<text class="current">当前语言: {{ languageNames[locale] }}</text>
|
|
744
756
|
</view>
|
|
745
757
|
</template>
|
|
@@ -798,39 +810,41 @@ function switchLanguage(code: string) {
|
|
|
798
810
|
#### 步骤 1: 导入 useI18n
|
|
799
811
|
|
|
800
812
|
```typescript
|
|
801
|
-
import { useI18n } from
|
|
813
|
+
import { useI18n } from "tang-ui-x";
|
|
802
814
|
```
|
|
803
815
|
|
|
804
816
|
#### 步骤 2: 在 setup 中获取翻译函数
|
|
805
817
|
|
|
806
818
|
```typescript
|
|
807
|
-
const { $t } = useI18n()
|
|
819
|
+
const { $t } = useI18n();
|
|
808
820
|
```
|
|
809
821
|
|
|
810
822
|
#### 步骤 3: 替换硬编码文本
|
|
811
823
|
|
|
812
824
|
```typescript
|
|
813
825
|
// 之前
|
|
814
|
-
const confirmText =
|
|
826
|
+
const confirmText = "确定";
|
|
815
827
|
|
|
816
828
|
// 之后
|
|
817
|
-
const confirmText = $t(
|
|
829
|
+
const confirmText = $t("common.confirm");
|
|
818
830
|
```
|
|
819
831
|
|
|
820
832
|
#### 步骤 4: 支持用户覆盖(推荐)
|
|
821
833
|
|
|
822
834
|
```typescript
|
|
823
835
|
interface Props {
|
|
824
|
-
title?: string
|
|
825
|
-
description?: string
|
|
836
|
+
title?: string;
|
|
837
|
+
description?: string;
|
|
826
838
|
}
|
|
827
839
|
|
|
828
|
-
const props = defineProps<Props>()
|
|
829
|
-
const { $t } = useI18n()
|
|
840
|
+
const props = defineProps<Props>();
|
|
841
|
+
const { $t } = useI18n();
|
|
830
842
|
|
|
831
843
|
// 优先使用用户传入的值,否则使用翻译
|
|
832
|
-
const displayTitle = computed(() => props.title || $t(
|
|
833
|
-
const displayDescription = computed(
|
|
844
|
+
const displayTitle = computed(() => props.title || $t("empty.title"));
|
|
845
|
+
const displayDescription = computed(
|
|
846
|
+
() => props.description || $t("empty.description"),
|
|
847
|
+
);
|
|
834
848
|
```
|
|
835
849
|
|
|
836
850
|
#### 步骤 5: 更新模板
|
|
@@ -890,10 +904,10 @@ function handleAction() {
|
|
|
890
904
|
|
|
891
905
|
```typescript
|
|
892
906
|
{
|
|
893
|
-
$t: (key: string, params?: Record<string, any>) => string
|
|
894
|
-
locale: ComputedRef
|
|
895
|
-
availableLocales: ComputedRef<string[]
|
|
896
|
-
setLocale: (locale: string) => boolean
|
|
907
|
+
$t: (key: string, params?: Record<string, any>) => string;
|
|
908
|
+
locale: ComputedRef;
|
|
909
|
+
availableLocales: ComputedRef<string[]>;
|
|
910
|
+
setLocale: (locale: string) => boolean;
|
|
897
911
|
}
|
|
898
912
|
```
|
|
899
913
|
|
|
@@ -934,8 +948,10 @@ function handleAction() {
|
|
|
934
948
|
A: 使用 `registerLocale` 函数注册新语言包,然后使用 `setLanguage` 切换。
|
|
935
949
|
|
|
936
950
|
```typescript
|
|
937
|
-
registerLocale(
|
|
938
|
-
|
|
951
|
+
registerLocale("ja-JP", {
|
|
952
|
+
/* 日语翻译 */
|
|
953
|
+
});
|
|
954
|
+
setLanguage("ja-JP");
|
|
939
955
|
```
|
|
940
956
|
|
|
941
957
|
#### Q: 如何覆盖默认翻译?
|
|
@@ -943,11 +959,11 @@ setLanguage('ja-JP')
|
|
|
943
959
|
A: 使用 `registerLocale` 注册同语言代码的语言包,只需包含要覆盖的键。
|
|
944
960
|
|
|
945
961
|
```typescript
|
|
946
|
-
registerLocale(
|
|
962
|
+
registerLocale("zh-CN", {
|
|
947
963
|
common: {
|
|
948
|
-
confirm:
|
|
949
|
-
}
|
|
950
|
-
})
|
|
964
|
+
confirm: "好的", // 只覆盖这一个键
|
|
965
|
+
},
|
|
966
|
+
});
|
|
951
967
|
```
|
|
952
968
|
|
|
953
969
|
#### Q: 组件的文本不随语言切换更新?
|
|
@@ -956,10 +972,10 @@ A: 确保使用 `computed` 包装翻译文本:
|
|
|
956
972
|
|
|
957
973
|
```typescript
|
|
958
974
|
// ❌ 错误:不会更新
|
|
959
|
-
const title = $t(
|
|
975
|
+
const title = $t("common.title");
|
|
960
976
|
|
|
961
977
|
// ✓ 正确:会自动更新
|
|
962
|
-
const title = computed(() => $t(
|
|
978
|
+
const title = computed(() => $t("common.title"));
|
|
963
979
|
```
|
|
964
980
|
|
|
965
981
|
#### Q: 如何在组件外使用翻译?
|
|
@@ -967,9 +983,9 @@ const title = computed(() => $t('common.title'))
|
|
|
967
983
|
A: 使用全局函数或直接访问 I18nManager:
|
|
968
984
|
|
|
969
985
|
```typescript
|
|
970
|
-
import { getCurrentLocale } from
|
|
986
|
+
import { getCurrentLocale } from "tang-ui-x";
|
|
971
987
|
|
|
972
|
-
const locale = getCurrentLocale()
|
|
988
|
+
const locale = getCurrentLocale();
|
|
973
989
|
```
|
|
974
990
|
|
|
975
991
|
#### Q: 支持哪些语言?
|
|
@@ -999,8 +1015,8 @@ i18n 系统已在以下平台测试并验证:
|
|
|
999
1015
|
|
|
1000
1016
|
```typescript
|
|
1001
1017
|
// ✓ 使用静态导入(跨平台兼容)
|
|
1002
|
-
import zhCNCommon from
|
|
1003
|
-
import enUSCommon from
|
|
1018
|
+
import zhCNCommon from "../locales/zh-CN/common.json";
|
|
1019
|
+
import enUSCommon from "../locales/en-US/common.json";
|
|
1004
1020
|
|
|
1005
1021
|
// ✗ 避免动态导入(小程序不支持)
|
|
1006
1022
|
// const module = await import(`../locales/${locale}/common.json`)
|
|
@@ -1016,8 +1032,8 @@ i18n 系统使用同步初始化方式,避免异步操作可能带来的平台
|
|
|
1016
1032
|
|
|
1017
1033
|
```typescript
|
|
1018
1034
|
// 同步初始化,所有平台都支持
|
|
1019
|
-
import { useI18n } from
|
|
1020
|
-
const { $t } = useI18n()
|
|
1035
|
+
import { useI18n } from "tang-ui-x";
|
|
1036
|
+
const { $t } = useI18n();
|
|
1021
1037
|
```
|
|
1022
1038
|
|
|
1023
1039
|
**4. JSON 文件处理**
|
|
@@ -1076,7 +1092,7 @@ function switchLanguage(lang: string) {
|
|
|
1076
1092
|
<button @click="switchLanguage('zh-CN')">中文</button>
|
|
1077
1093
|
<button @click="switchLanguage('en-US')">English</button>
|
|
1078
1094
|
<button @click="switchLanguage('zh-TW')">繁體中文</button>
|
|
1079
|
-
|
|
1095
|
+
|
|
1080
1096
|
<text>当前语言: {{ locale }}</text>
|
|
1081
1097
|
</view>
|
|
1082
1098
|
</template>
|
|
@@ -1094,6 +1110,7 @@ function switchLanguage(lang: string) {
|
|
|
1094
1110
|
## 📚 组件列表
|
|
1095
1111
|
|
|
1096
1112
|
### 基础组件 (5个)
|
|
1113
|
+
|
|
1097
1114
|
- TButton - 按钮
|
|
1098
1115
|
- TIcon - 图标
|
|
1099
1116
|
- TText - 文本
|
|
@@ -1101,6 +1118,7 @@ function switchLanguage(lang: string) {
|
|
|
1101
1118
|
- TDivider - 分割线
|
|
1102
1119
|
|
|
1103
1120
|
### 布局组件 (8个)
|
|
1121
|
+
|
|
1104
1122
|
- TCard - 卡片
|
|
1105
1123
|
- TList - 列表
|
|
1106
1124
|
- TListItem - 列表项
|
|
@@ -1111,6 +1129,7 @@ function switchLanguage(lang: string) {
|
|
|
1111
1129
|
- TCol - 栅格列
|
|
1112
1130
|
|
|
1113
1131
|
### 表单组件 (13个)
|
|
1132
|
+
|
|
1114
1133
|
- TInput - 输入框
|
|
1115
1134
|
- TNumberInput - 数字输入
|
|
1116
1135
|
- TTextarea - 多行文本输入
|
|
@@ -1126,6 +1145,7 @@ function switchLanguage(lang: string) {
|
|
|
1126
1145
|
- TPicker - 选择器
|
|
1127
1146
|
|
|
1128
1147
|
### 数据展示 (9个)
|
|
1148
|
+
|
|
1129
1149
|
- Tags - 标签
|
|
1130
1150
|
- TBadge - 徽标
|
|
1131
1151
|
- TAvatar - 头像
|
|
@@ -1138,6 +1158,7 @@ function switchLanguage(lang: string) {
|
|
|
1138
1158
|
- TSwiper - 轮播图
|
|
1139
1159
|
|
|
1140
1160
|
### 反馈组件 (5个)
|
|
1161
|
+
|
|
1141
1162
|
- TLoading - 加载
|
|
1142
1163
|
- TToast - 轻提示
|
|
1143
1164
|
- TDialog - 对话框
|
|
@@ -1145,6 +1166,7 @@ function switchLanguage(lang: string) {
|
|
|
1145
1166
|
- TActionSheet - 动作面板
|
|
1146
1167
|
|
|
1147
1168
|
### 导航组件 (2个)
|
|
1169
|
+
|
|
1148
1170
|
- Tabs - 标签页
|
|
1149
1171
|
- TNavBar - 导航栏
|
|
1150
1172
|
|