tang-ui-x 1.1.1 → 1.1.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 +1003 -0
- package/components/TActionSheet/index.uvue +15 -2
- package/components/TCheckboxGroup/index.uvue +30 -11
- package/components/TCheckboxGroup/type.uts +10 -0
- package/components/TCollapse/index.uvue +1 -1
- package/components/TCollapse/type.uts +3 -1
- package/components/TCollapseItem/index.uvue +22 -26
- package/components/TDialog/index.uvue +19 -4
- package/components/TEmpty/index.uvue +28 -14
- package/components/TForm/index.uvue +394 -392
- package/components/TForm/type.uts +90 -80
- package/components/TInput/index.uvue +24 -5
- package/components/TInput/type.uts +10 -0
- package/components/TPicker/index.uvue +26 -6
- package/components/TRadioGroup/index.uvue +32 -12
- package/components/TRadioGroup/type.uts +7 -0
- package/components/TSearchBar/index.uvue +19 -4
- package/composables/i18n/error.uts +82 -0
- package/composables/i18n/index.uts +188 -0
- package/composables/i18n/manager-demo.uts +104 -0
- package/composables/i18n/manager.test.uts +182 -0
- package/composables/i18n/manager.uts +531 -0
- package/composables/i18n/register-demo.uts +125 -0
- package/composables/i18n/task22-verification.uts +198 -0
- package/composables/i18n/task23-verification.uts +343 -0
- package/composables/i18n/task8-demo.uts +93 -0
- package/composables/i18n/task8-verification.uts +98 -0
- package/composables/i18n/test-task23.uts +9 -0
- package/composables/i18n/types.uts +46 -0
- package/composables/i18n/useI18n-verification.uts +105 -0
- package/composables/i18n/validation-demo.uts +45 -0
- package/composables/i18n/validation-test.uts +106 -0
- package/composables/useI18n.uts +77 -0
- package/index.uts +23 -0
- package/locales/cross-platform-verification.uts +510 -0
- package/locales/en-US/actionSheet.json +3 -0
- package/locales/en-US/common.json +17 -0
- package/locales/en-US/dialog.json +5 -0
- package/locales/en-US/empty.json +6 -0
- package/locales/en-US/errorState.json +5 -0
- package/locales/en-US/examplePages.json +1236 -0
- package/locales/en-US/examples.json +218 -0
- package/locales/en-US/form.json +18 -0
- package/locales/en-US/input.json +3 -0
- package/locales/en-US/list.json +5 -0
- package/locales/en-US/loading.json +3 -0
- package/locales/en-US/navBar.json +4 -0
- package/locales/en-US/noticeBar.json +3 -0
- package/locales/en-US/picker.json +5 -0
- package/locales/en-US/searchBar.json +4 -0
- package/locales/en-US/textarea.json +3 -0
- package/locales/en-US/toast.json +6 -0
- package/locales/index.uts +79 -0
- package/locales/init-verification.uts +101 -0
- package/locales/loader.uts +251 -0
- package/locales/run-verification.uts +16 -0
- package/locales/zh-CN/actionSheet.json +3 -0
- package/locales/zh-CN/common.json +17 -0
- package/locales/zh-CN/dialog.json +5 -0
- package/locales/zh-CN/empty.json +6 -0
- package/locales/zh-CN/errorState.json +5 -0
- package/locales/zh-CN/examplePages.json +1236 -0
- package/locales/zh-CN/examples.json +218 -0
- package/locales/zh-CN/form.json +18 -0
- package/locales/zh-CN/input.json +3 -0
- package/locales/zh-CN/list.json +5 -0
- package/locales/zh-CN/loading.json +3 -0
- package/locales/zh-CN/navBar.json +4 -0
- package/locales/zh-CN/noticeBar.json +3 -0
- package/locales/zh-CN/picker.json +5 -0
- package/locales/zh-CN/searchBar.json +4 -0
- package/locales/zh-CN/textarea.json +3 -0
- package/locales/zh-CN/toast.json +6 -0
- package/locales/zh-TW/actionSheet.json +3 -0
- package/locales/zh-TW/common.json +15 -0
- package/locales/zh-TW/dialog.json +5 -0
- package/locales/zh-TW/empty.json +6 -0
- package/locales/zh-TW/errorState.json +5 -0
- package/locales/zh-TW/examplePages.json +705 -0
- package/locales/zh-TW/examples.json +218 -0
- package/locales/zh-TW/form.json +18 -0
- package/locales/zh-TW/input.json +3 -0
- package/locales/zh-TW/list.json +5 -0
- package/locales/zh-TW/loading.json +3 -0
- package/locales/zh-TW/navBar.json +4 -0
- package/locales/zh-TW/noticeBar.json +3 -0
- package/locales/zh-TW/picker.json +5 -0
- package/locales/zh-TW/searchBar.json +4 -0
- package/locales/zh-TW/textarea.json +3 -0
- package/locales/zh-TW/toast.json +6 -0
- package/package.json +3 -2
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Task 22 验证脚本
|
|
3
|
+
* 验证语言包注册模式功能
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { I18nManager } from './manager.uts'
|
|
7
|
+
import type { ModularLocaleMessages } from './types.uts'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 测试语言包注册模式功能
|
|
11
|
+
*/
|
|
12
|
+
export function testRegistrationModes(): void {
|
|
13
|
+
console.log('=== Task 22: 语言包注册模式功能验证 ===\n')
|
|
14
|
+
|
|
15
|
+
const manager = I18nManager.getInstance()
|
|
16
|
+
|
|
17
|
+
// 测试 1: 合并模式(默认)
|
|
18
|
+
console.log('测试 1: 合并模式(默认 registerMessages)')
|
|
19
|
+
const initialMessages: ModularLocaleMessages = {
|
|
20
|
+
common: {
|
|
21
|
+
confirm: '确定',
|
|
22
|
+
cancel: '取消',
|
|
23
|
+
ok: '好的'
|
|
24
|
+
},
|
|
25
|
+
dialog: {
|
|
26
|
+
title: '提示',
|
|
27
|
+
confirmText: '确定'
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
manager.registerMessages('test-locale', initialMessages)
|
|
32
|
+
console.log('✓ 初始语言包已注册')
|
|
33
|
+
|
|
34
|
+
// 部分覆盖
|
|
35
|
+
const partialMessages: ModularLocaleMessages = {
|
|
36
|
+
common: {
|
|
37
|
+
confirm: '好的', // 覆盖
|
|
38
|
+
close: '关闭' // 新增
|
|
39
|
+
},
|
|
40
|
+
empty: { // 新模块
|
|
41
|
+
title: '暂无数据'
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
manager.registerMessages('test-locale', partialMessages)
|
|
46
|
+
console.log('✓ 部分语言包已合并')
|
|
47
|
+
|
|
48
|
+
// 验证合并结果
|
|
49
|
+
manager.setLocale('test-locale')
|
|
50
|
+
const confirmText = manager.translate('common.confirm')
|
|
51
|
+
const cancelText = manager.translate('common.cancel')
|
|
52
|
+
const okText = manager.translate('common.ok')
|
|
53
|
+
const closeText = manager.translate('common.close')
|
|
54
|
+
const dialogTitle = manager.translate('dialog.title')
|
|
55
|
+
const emptyTitle = manager.translate('empty.title')
|
|
56
|
+
|
|
57
|
+
console.log(` common.confirm: ${confirmText} (应为 "好的")`)
|
|
58
|
+
console.log(` common.cancel: ${cancelText} (应为 "取消" - 保留)`)
|
|
59
|
+
console.log(` common.ok: ${okText} (应为 "好的" - 保留)`)
|
|
60
|
+
console.log(` common.close: ${closeText} (应为 "关闭" - 新增)`)
|
|
61
|
+
console.log(` dialog.title: ${dialogTitle} (应为 "提示" - 保留)`)
|
|
62
|
+
console.log(` empty.title: ${emptyTitle} (应为 "暂无数据" - 新模块)`)
|
|
63
|
+
|
|
64
|
+
const mergeSuccess =
|
|
65
|
+
confirmText === '好的' &&
|
|
66
|
+
cancelText === '取消' &&
|
|
67
|
+
okText === '好的' &&
|
|
68
|
+
closeText === '关闭' &&
|
|
69
|
+
dialogTitle === '提示' &&
|
|
70
|
+
emptyTitle === '暂无数据'
|
|
71
|
+
|
|
72
|
+
if (mergeSuccess) {
|
|
73
|
+
console.log('✅ 合并模式测试通过\n')
|
|
74
|
+
} else {
|
|
75
|
+
console.log('❌ 合并模式测试失败\n')
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// 测试 2: 替换模式(registerMessagesWithMode)
|
|
79
|
+
console.log('测试 2: 替换模式(registerMessagesWithMode)')
|
|
80
|
+
const replaceMessages: ModularLocaleMessages = {
|
|
81
|
+
button: {
|
|
82
|
+
submit: '提交',
|
|
83
|
+
reset: '重置'
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
manager.registerMessagesWithMode('test-locale', replaceMessages, 'replace')
|
|
88
|
+
console.log('✓ 语言包已完全替换')
|
|
89
|
+
|
|
90
|
+
// 验证替换结果 - 旧内容应该不存在
|
|
91
|
+
const submitText = manager.translate('button.submit')
|
|
92
|
+
const oldConfirmText = manager.translate('common.confirm')
|
|
93
|
+
const oldDialogTitle = manager.translate('dialog.title')
|
|
94
|
+
|
|
95
|
+
console.log(` button.submit: ${submitText} (应为 "提交")`)
|
|
96
|
+
console.log(` common.confirm: ${oldConfirmText} (应为 "common.confirm" - 已被清除)`)
|
|
97
|
+
console.log(` dialog.title: ${oldDialogTitle} (应为 "dialog.title" - 已被清除)`)
|
|
98
|
+
|
|
99
|
+
const replaceSuccess =
|
|
100
|
+
submitText === '提交' &&
|
|
101
|
+
oldConfirmText === 'common.confirm' &&
|
|
102
|
+
oldDialogTitle === 'dialog.title'
|
|
103
|
+
|
|
104
|
+
if (replaceSuccess) {
|
|
105
|
+
console.log('✅ 替换模式测试通过\n')
|
|
106
|
+
} else {
|
|
107
|
+
console.log('❌ 替换模式测试失败\n')
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 测试 3: replaceMessages 方法
|
|
111
|
+
console.log('测试 3: replaceMessages 方法')
|
|
112
|
+
const newMessages: ModularLocaleMessages = {
|
|
113
|
+
search: {
|
|
114
|
+
placeholder: '搜索',
|
|
115
|
+
cancel: '取消'
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
manager.replaceMessages('test-locale', newMessages)
|
|
120
|
+
console.log('✓ 使用 replaceMessages 方法替换')
|
|
121
|
+
|
|
122
|
+
const searchPlaceholder = manager.translate('search.placeholder')
|
|
123
|
+
const oldSubmitText = manager.translate('button.submit')
|
|
124
|
+
|
|
125
|
+
console.log(` search.placeholder: ${searchPlaceholder} (应为 "搜索")`)
|
|
126
|
+
console.log(` button.submit: ${oldSubmitText} (应为 "button.submit" - 已被清除)`)
|
|
127
|
+
|
|
128
|
+
const replaceMethodSuccess =
|
|
129
|
+
searchPlaceholder === '搜索' &&
|
|
130
|
+
oldSubmitText === 'button.submit'
|
|
131
|
+
|
|
132
|
+
if (replaceMethodSuccess) {
|
|
133
|
+
console.log('✅ replaceMessages 方法测试通过\n')
|
|
134
|
+
} else {
|
|
135
|
+
console.log('❌ replaceMessages 方法测试失败\n')
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// 测试 4: 合并模式使用 registerMessagesWithMode
|
|
139
|
+
console.log('测试 4: 合并模式使用 registerMessagesWithMode')
|
|
140
|
+
const baseMessages: ModularLocaleMessages = {
|
|
141
|
+
common: {
|
|
142
|
+
yes: '是',
|
|
143
|
+
no: '否'
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
manager.registerMessagesWithMode('test-locale-2', baseMessages, 'merge')
|
|
148
|
+
console.log('✓ 基础语言包已注册')
|
|
149
|
+
|
|
150
|
+
const additionalMessages: ModularLocaleMessages = {
|
|
151
|
+
common: {
|
|
152
|
+
yes: 'Yes', // 覆盖
|
|
153
|
+
maybe: 'Maybe' // 新增
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
manager.registerMessagesWithMode('test-locale-2', additionalMessages, 'merge')
|
|
158
|
+
console.log('✓ 额外语言包已合并')
|
|
159
|
+
|
|
160
|
+
manager.setLocale('test-locale-2')
|
|
161
|
+
const yesText = manager.translate('common.yes')
|
|
162
|
+
const noText = manager.translate('common.no')
|
|
163
|
+
const maybeText = manager.translate('common.maybe')
|
|
164
|
+
|
|
165
|
+
console.log(` common.yes: ${yesText} (应为 "Yes")`)
|
|
166
|
+
console.log(` common.no: ${noText} (应为 "否" - 保留)`)
|
|
167
|
+
console.log(` common.maybe: ${maybeText} (应为 "Maybe" - 新增)`)
|
|
168
|
+
|
|
169
|
+
const mergeWithModeSuccess =
|
|
170
|
+
yesText === 'Yes' &&
|
|
171
|
+
noText === '否' &&
|
|
172
|
+
maybeText === 'Maybe'
|
|
173
|
+
|
|
174
|
+
if (mergeWithModeSuccess) {
|
|
175
|
+
console.log('✅ registerMessagesWithMode 合并模式测试通过\n')
|
|
176
|
+
} else {
|
|
177
|
+
console.log('❌ registerMessagesWithMode 合并模式测试失败\n')
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// 总结
|
|
181
|
+
console.log('=== 测试总结 ===')
|
|
182
|
+
const allSuccess = mergeSuccess && replaceSuccess && replaceMethodSuccess && mergeWithModeSuccess
|
|
183
|
+
|
|
184
|
+
if (allSuccess) {
|
|
185
|
+
console.log('✅ 所有测试通过!')
|
|
186
|
+
console.log('\n实现的功能:')
|
|
187
|
+
console.log('1. registerMessages() - 默认使用合并模式')
|
|
188
|
+
console.log('2. registerMessagesWithMode() - 支持 merge 和 replace 模式')
|
|
189
|
+
console.log('3. replaceMessages() - 完全替换语言包')
|
|
190
|
+
console.log('4. 合并模式正确保留未覆盖的内容')
|
|
191
|
+
console.log('5. 替换模式完全清除旧内容')
|
|
192
|
+
} else {
|
|
193
|
+
console.log('❌ 部分测试失败,请检查实现')
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// 执行测试
|
|
198
|
+
testRegistrationModes()
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Task 23 验证脚本
|
|
3
|
+
* 验证全局函数支持注册模式
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
registerLocale,
|
|
8
|
+
registerLocaleWithMode,
|
|
9
|
+
replaceLocale,
|
|
10
|
+
setLanguage,
|
|
11
|
+
getCurrentLocale
|
|
12
|
+
} from './index.uts'
|
|
13
|
+
import { I18nManager } from './manager.uts'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 验证 registerLocale 保持向后兼容(默认合并模式)
|
|
17
|
+
*/
|
|
18
|
+
function testRegisterLocaleBackwardCompatibility(): boolean {
|
|
19
|
+
console.log('\n=== 测试 1: registerLocale 向后兼容性(默认合并模式) ===')
|
|
20
|
+
|
|
21
|
+
const manager = I18nManager.getInstance()
|
|
22
|
+
|
|
23
|
+
// 注册初始语言包
|
|
24
|
+
registerLocale('test-locale-1', {
|
|
25
|
+
common: {
|
|
26
|
+
confirm: '确定',
|
|
27
|
+
cancel: '取消',
|
|
28
|
+
ok: '好的'
|
|
29
|
+
},
|
|
30
|
+
dialog: {
|
|
31
|
+
title: '提示',
|
|
32
|
+
confirmText: '确定'
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
// 使用 registerLocale 覆盖部分内容(应该使用合并模式)
|
|
37
|
+
registerLocale('test-locale-1', {
|
|
38
|
+
common: {
|
|
39
|
+
confirm: '好的' // 覆盖这个键
|
|
40
|
+
// cancel 和 ok 应该保留
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
// 验证合并结果
|
|
45
|
+
setLanguage('test-locale-1')
|
|
46
|
+
const messages = manager.messages.get('test-locale-1')
|
|
47
|
+
|
|
48
|
+
if (!messages) {
|
|
49
|
+
console.log('❌ 失败:语言包未注册')
|
|
50
|
+
return false
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 检查 common 模块
|
|
54
|
+
const common = messages['common']
|
|
55
|
+
if (!common) {
|
|
56
|
+
console.log('❌ 失败:common 模块不存在')
|
|
57
|
+
return false
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 验证覆盖的键
|
|
61
|
+
if (common['confirm'] !== '好的') {
|
|
62
|
+
console.log('❌ 失败:confirm 应该被覆盖为 "好的",实际为:', common['confirm'])
|
|
63
|
+
return false
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 验证保留的键
|
|
67
|
+
if (common['cancel'] !== '取消') {
|
|
68
|
+
console.log('❌ 失败:cancel 应该保留为 "取消",实际为:', common['cancel'])
|
|
69
|
+
return false
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (common['ok'] !== '好的') {
|
|
73
|
+
console.log('❌ 失败:ok 应该保留为 "好的",实际为:', common['ok'])
|
|
74
|
+
return false
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// 验证 dialog 模块应该保留
|
|
78
|
+
const dialog = messages['dialog']
|
|
79
|
+
if (!dialog || dialog['title'] !== '提示') {
|
|
80
|
+
console.log('❌ 失败:dialog 模块应该保留')
|
|
81
|
+
return false
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
console.log('✅ 通过:registerLocale 默认使用合并模式')
|
|
85
|
+
return true
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 验证 registerLocaleWithMode 合并模式
|
|
90
|
+
*/
|
|
91
|
+
function testRegisterLocaleWithModeMerge(): boolean {
|
|
92
|
+
console.log('\n=== 测试 2: registerLocaleWithMode 合并模式 ===')
|
|
93
|
+
|
|
94
|
+
const manager = I18nManager.getInstance()
|
|
95
|
+
|
|
96
|
+
// 注册初始语言包
|
|
97
|
+
registerLocaleWithMode('test-locale-2', {
|
|
98
|
+
common: {
|
|
99
|
+
confirm: '确定',
|
|
100
|
+
cancel: '取消'
|
|
101
|
+
},
|
|
102
|
+
dialog: {
|
|
103
|
+
title: '提示'
|
|
104
|
+
}
|
|
105
|
+
}, 'merge')
|
|
106
|
+
|
|
107
|
+
// 使用合并模式添加新内容
|
|
108
|
+
registerLocaleWithMode('test-locale-2', {
|
|
109
|
+
common: {
|
|
110
|
+
ok: '好的' // 添加新键
|
|
111
|
+
},
|
|
112
|
+
empty: {
|
|
113
|
+
title: '暂无数据' // 添加新模块
|
|
114
|
+
}
|
|
115
|
+
}, 'merge')
|
|
116
|
+
|
|
117
|
+
const messages = manager.messages.get('test-locale-2')
|
|
118
|
+
|
|
119
|
+
if (!messages) {
|
|
120
|
+
console.log('❌ 失败:语言包未注册')
|
|
121
|
+
return false
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// 验证原有内容保留
|
|
125
|
+
const common = messages['common']
|
|
126
|
+
if (!common || common['confirm'] !== '确定' || common['cancel'] !== '取消') {
|
|
127
|
+
console.log('❌ 失败:原有 common 内容应该保留')
|
|
128
|
+
return false
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// 验证新内容添加
|
|
132
|
+
if (common['ok'] !== '好的') {
|
|
133
|
+
console.log('❌ 失败:新键 ok 应该被添加')
|
|
134
|
+
return false
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// 验证原有模块保留
|
|
138
|
+
const dialog = messages['dialog']
|
|
139
|
+
if (!dialog || dialog['title'] !== '提示') {
|
|
140
|
+
console.log('❌ 失败:原有 dialog 模块应该保留')
|
|
141
|
+
return false
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// 验证新模块添加
|
|
145
|
+
const empty = messages['empty']
|
|
146
|
+
if (!empty || empty['title'] !== '暂无数据') {
|
|
147
|
+
console.log('❌ 失败:新模块 empty 应该被添加')
|
|
148
|
+
return false
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
console.log('✅ 通过:registerLocaleWithMode 合并模式正常工作')
|
|
152
|
+
return true
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* 验证 registerLocaleWithMode 替换模式
|
|
157
|
+
*/
|
|
158
|
+
function testRegisterLocaleWithModeReplace(): boolean {
|
|
159
|
+
console.log('\n=== 测试 3: registerLocaleWithMode 替换模式 ===')
|
|
160
|
+
|
|
161
|
+
const manager = I18nManager.getInstance()
|
|
162
|
+
|
|
163
|
+
// 注册初始语言包
|
|
164
|
+
registerLocaleWithMode('test-locale-3', {
|
|
165
|
+
common: {
|
|
166
|
+
confirm: '确定',
|
|
167
|
+
cancel: '取消',
|
|
168
|
+
ok: '好的'
|
|
169
|
+
},
|
|
170
|
+
dialog: {
|
|
171
|
+
title: '提示',
|
|
172
|
+
confirmText: '确定'
|
|
173
|
+
}
|
|
174
|
+
}, 'merge')
|
|
175
|
+
|
|
176
|
+
// 使用替换模式完全替换
|
|
177
|
+
registerLocaleWithMode('test-locale-3', {
|
|
178
|
+
common: {
|
|
179
|
+
confirm: '好的' // 只有这一个键
|
|
180
|
+
}
|
|
181
|
+
// dialog 模块应该被移除
|
|
182
|
+
}, 'replace')
|
|
183
|
+
|
|
184
|
+
const messages = manager.messages.get('test-locale-3')
|
|
185
|
+
|
|
186
|
+
if (!messages) {
|
|
187
|
+
console.log('❌ 失败:语言包未注册')
|
|
188
|
+
return false
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// 验证只有新内容
|
|
192
|
+
const common = messages['common']
|
|
193
|
+
if (!common) {
|
|
194
|
+
console.log('❌ 失败:common 模块应该存在')
|
|
195
|
+
return false
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (common['confirm'] !== '好的') {
|
|
199
|
+
console.log('❌ 失败:confirm 应该是 "好的"')
|
|
200
|
+
return false
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// 验证原有的其他键被移除
|
|
204
|
+
if ('cancel' in common || 'ok' in common) {
|
|
205
|
+
console.log('❌ 失败:原有的 cancel 和 ok 键应该被移除')
|
|
206
|
+
return false
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// 验证原有模块被移除
|
|
210
|
+
if ('dialog' in messages) {
|
|
211
|
+
console.log('❌ 失败:原有的 dialog 模块应该被移除')
|
|
212
|
+
return false
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
console.log('✅ 通过:registerLocaleWithMode 替换模式正常工作')
|
|
216
|
+
return true
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* 验证 replaceLocale 函数
|
|
221
|
+
*/
|
|
222
|
+
function testReplaceLocale(): boolean {
|
|
223
|
+
console.log('\n=== 测试 4: replaceLocale 函数 ===')
|
|
224
|
+
|
|
225
|
+
const manager = I18nManager.getInstance()
|
|
226
|
+
|
|
227
|
+
// 注册初始语言包
|
|
228
|
+
registerLocale('test-locale-4', {
|
|
229
|
+
common: {
|
|
230
|
+
confirm: '确定',
|
|
231
|
+
cancel: '取消',
|
|
232
|
+
ok: '好的'
|
|
233
|
+
},
|
|
234
|
+
dialog: {
|
|
235
|
+
title: '提示'
|
|
236
|
+
},
|
|
237
|
+
empty: {
|
|
238
|
+
title: '暂无数据'
|
|
239
|
+
}
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
// 使用 replaceLocale 完全替换
|
|
243
|
+
replaceLocale('test-locale-4', {
|
|
244
|
+
common: {
|
|
245
|
+
confirm: '好的',
|
|
246
|
+
cancel: '取消'
|
|
247
|
+
}
|
|
248
|
+
// 只保留 common 模块,其他模块应该被移除
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
const messages = manager.messages.get('test-locale-4')
|
|
252
|
+
|
|
253
|
+
if (!messages) {
|
|
254
|
+
console.log('❌ 失败:语言包未注册')
|
|
255
|
+
return false
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// 验证新内容存在
|
|
259
|
+
const common = messages['common']
|
|
260
|
+
if (!common || common['confirm'] !== '好的' || common['cancel'] !== '取消') {
|
|
261
|
+
console.log('❌ 失败:新的 common 内容应该存在')
|
|
262
|
+
return false
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// 验证原有的 ok 键被移除
|
|
266
|
+
if ('ok' in common) {
|
|
267
|
+
console.log('❌ 失败:原有的 ok 键应该被移除')
|
|
268
|
+
return false
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// 验证原有模块被移除
|
|
272
|
+
if ('dialog' in messages || 'empty' in messages) {
|
|
273
|
+
console.log('❌ 失败:原有的 dialog 和 empty 模块应该被移除')
|
|
274
|
+
return false
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
console.log('✅ 通过:replaceLocale 函数正常工作')
|
|
278
|
+
return true
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* 验证函数导出
|
|
283
|
+
*/
|
|
284
|
+
function testFunctionExports(): boolean {
|
|
285
|
+
console.log('\n=== 测试 5: 函数导出验证 ===')
|
|
286
|
+
|
|
287
|
+
// 验证所有函数都已导出
|
|
288
|
+
if (typeof registerLocale !== 'function') {
|
|
289
|
+
console.log('❌ 失败:registerLocale 未导出')
|
|
290
|
+
return false
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (typeof registerLocaleWithMode !== 'function') {
|
|
294
|
+
console.log('❌ 失败:registerLocaleWithMode 未导出')
|
|
295
|
+
return false
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (typeof replaceLocale !== 'function') {
|
|
299
|
+
console.log('❌ 失败:replaceLocale 未导出')
|
|
300
|
+
return false
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
console.log('✅ 通过:所有函数都已正确导出')
|
|
304
|
+
return true
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* 运行所有测试
|
|
309
|
+
*/
|
|
310
|
+
export function runTask23Verification(): void {
|
|
311
|
+
console.log('========================================')
|
|
312
|
+
console.log('Task 23 验证:全局函数支持注册模式')
|
|
313
|
+
console.log('========================================')
|
|
314
|
+
|
|
315
|
+
const results: boolean[] = []
|
|
316
|
+
|
|
317
|
+
results.push(testFunctionExports())
|
|
318
|
+
results.push(testRegisterLocaleBackwardCompatibility())
|
|
319
|
+
results.push(testRegisterLocaleWithModeMerge())
|
|
320
|
+
results.push(testRegisterLocaleWithModeReplace())
|
|
321
|
+
results.push(testReplaceLocale())
|
|
322
|
+
|
|
323
|
+
const passed = results.filter(r => r).length
|
|
324
|
+
const total = results.length
|
|
325
|
+
|
|
326
|
+
console.log('\n========================================')
|
|
327
|
+
console.log(`测试结果: ${passed}/${total} 通过`)
|
|
328
|
+
console.log('========================================')
|
|
329
|
+
|
|
330
|
+
if (passed === total) {
|
|
331
|
+
console.log('✅ Task 23 验证通过!')
|
|
332
|
+
console.log('\n实现的功能:')
|
|
333
|
+
console.log('1. ✅ registerLocale() 保持向后兼容(默认合并模式)')
|
|
334
|
+
console.log('2. ✅ registerLocaleWithMode() 支持指定模式')
|
|
335
|
+
console.log('3. ✅ replaceLocale() 提供便捷的替换方法')
|
|
336
|
+
console.log('4. ✅ 所有函数都已正确导出')
|
|
337
|
+
} else {
|
|
338
|
+
console.log('❌ 部分测试失败,请检查实现')
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// 如果直接运行此文件,执行验证
|
|
343
|
+
// runTask23Verification()
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Task 8 功能演示
|
|
3
|
+
* 展示 getAvailableLocales() 和 getCurrentLocale() 的使用
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { I18nManager } from './manager.uts'
|
|
7
|
+
import type { ModularLocaleMessages } from './types.uts'
|
|
8
|
+
|
|
9
|
+
// 获取管理器实例
|
|
10
|
+
const manager = I18nManager.getInstance()
|
|
11
|
+
|
|
12
|
+
// 准备测试数据
|
|
13
|
+
const zhCN: ModularLocaleMessages = {
|
|
14
|
+
common: { confirm: '确定', cancel: '取消' }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const enUS: ModularLocaleMessages = {
|
|
18
|
+
common: { confirm: 'Confirm', cancel: 'Cancel' }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const zhTW: ModularLocaleMessages = {
|
|
22
|
+
common: { confirm: '確定', cancel: '取消' }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// 演示 1: 初始状态
|
|
26
|
+
console.log('=== 演示 1: 初始状态 ===')
|
|
27
|
+
console.log('当前语言:', manager.getCurrentLocale())
|
|
28
|
+
console.log('可用语言:', manager.getAvailableLocales().value)
|
|
29
|
+
console.log('')
|
|
30
|
+
|
|
31
|
+
// 演示 2: 注册语言包
|
|
32
|
+
console.log('=== 演示 2: 注册语言包 ===')
|
|
33
|
+
manager.registerMessages('zh-CN', zhCN)
|
|
34
|
+
console.log('注册 zh-CN 后:')
|
|
35
|
+
console.log(' 可用语言:', manager.getAvailableLocales().value)
|
|
36
|
+
console.log('')
|
|
37
|
+
|
|
38
|
+
manager.registerMessages('en-US', enUS)
|
|
39
|
+
console.log('注册 en-US 后:')
|
|
40
|
+
console.log(' 可用语言:', manager.getAvailableLocales().value)
|
|
41
|
+
console.log('')
|
|
42
|
+
|
|
43
|
+
manager.registerMessages('zh-TW', zhTW)
|
|
44
|
+
console.log('注册 zh-TW 后:')
|
|
45
|
+
console.log(' 可用语言:', manager.getAvailableLocales().value)
|
|
46
|
+
console.log('')
|
|
47
|
+
|
|
48
|
+
// 演示 3: 切换语言
|
|
49
|
+
console.log('=== 演示 3: 切换语言 ===')
|
|
50
|
+
console.log('初始语言:', manager.getCurrentLocale())
|
|
51
|
+
|
|
52
|
+
manager.setLocale('en-US')
|
|
53
|
+
console.log('切换到 en-US:', manager.getCurrentLocale())
|
|
54
|
+
|
|
55
|
+
manager.setLocale('zh-TW')
|
|
56
|
+
console.log('切换到 zh-TW:', manager.getCurrentLocale())
|
|
57
|
+
|
|
58
|
+
manager.setLocale('zh-CN')
|
|
59
|
+
console.log('切换回 zh-CN:', manager.getCurrentLocale())
|
|
60
|
+
console.log('')
|
|
61
|
+
|
|
62
|
+
// 演示 4: 响应式特性
|
|
63
|
+
console.log('=== 演示 4: 响应式特性 ===')
|
|
64
|
+
const availableLocales = manager.getAvailableLocales()
|
|
65
|
+
console.log('获取响应式列表:', availableLocales.value)
|
|
66
|
+
|
|
67
|
+
// 动态注册新语言
|
|
68
|
+
const jaJP: ModularLocaleMessages = {
|
|
69
|
+
common: { confirm: '確認', cancel: 'キャンセル' }
|
|
70
|
+
}
|
|
71
|
+
manager.registerMessages('ja-JP', jaJP)
|
|
72
|
+
console.log('注册 ja-JP 后,列表自动更新:', availableLocales.value)
|
|
73
|
+
console.log('')
|
|
74
|
+
|
|
75
|
+
// 演示 5: 实际应用场景
|
|
76
|
+
console.log('=== 演示 5: 实际应用场景 - 语言选择器 ===')
|
|
77
|
+
console.log('构建语言选择器选项:')
|
|
78
|
+
const locales = manager.getAvailableLocales().value
|
|
79
|
+
const localeNames: Record<string, string> = {
|
|
80
|
+
'zh-CN': '简体中文',
|
|
81
|
+
'en-US': 'English',
|
|
82
|
+
'zh-TW': '繁體中文',
|
|
83
|
+
'ja-JP': '日本語'
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
locales.forEach((locale: string) => {
|
|
87
|
+
const isCurrent = locale === manager.getCurrentLocale()
|
|
88
|
+
const marker = isCurrent ? '✓' : ' '
|
|
89
|
+
console.log(` [${marker}] ${locale} - ${localeNames[locale]}`)
|
|
90
|
+
})
|
|
91
|
+
console.log('')
|
|
92
|
+
|
|
93
|
+
console.log('=== 演示完成 ===')
|