@campus-admin/locale 1.0.0 → 2.0.1

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 CHANGED
@@ -1,70 +1,5 @@
1
1
  # @campus-admin/locale
2
2
 
3
- 面向智慧校园后台的国际化运行时:词条管理、取词与切换、日期与数字格式化。
3
+ Campus Admin locale package. Install the framework with `npm install @campus-admin/core vue`.
4
4
 
5
- 组件库(`@campus-admin/ui`)与业务共用同一个语言实例:组件词条挂 `ca.*` 命名空间,
6
- 业务词条放自己的根键(示例里是 `app.*` / `menu.*` / `page.*`)。
7
-
8
- ## 安装
9
-
10
- ```bash
11
- pnpm add @campus-admin/locale
12
- ```
13
-
14
- ## 用法
15
-
16
- ```ts
17
- import { createLocale, installLocale, useLocale, setLocale } from '@campus-admin/locale'
18
- import { createApp } from 'vue'
19
-
20
- const app = createApp(App)
21
-
22
- const locale = createLocale({
23
- locale: 'zh-CN',
24
- fallbackLocale: 'zh-CN',
25
- messages: { 'zh-CN': zhCN, 'en-US': enUS }, // 业务词条,与内置 ca.* 深合并
26
- })
27
-
28
- installLocale(app, locale) // 之后 setLocale / useLocale 都指向这个实例
29
- setLocale('en-US') // 切换:组件文案与业务文案一起变
30
- ```
31
-
32
- 组件内取词:
33
-
34
- ```ts
35
- const { t, te } = useLocale()
36
-
37
- t('ca.pagination.total', { total: 12 }) // 组件词条
38
- t('page.home.count', { total: 12 }) // 业务词条
39
- te('menu.school-student') // 判断词条是否存在(后端文案兜底用)
40
- ```
41
-
42
- 日期与数字统一走 `Intl` 封装,不要在业务里拼「年 / 月 / 日」:
43
-
44
- ```ts
45
- import { formatDay, formatMonth, formatNumber, weekdayLabels } from '@campus-admin/locale'
46
-
47
- formatMonth(new Date(2026, 8, 11), 'en-US') // September 2026
48
- weekdayLabels('zh-CN') // ['周一', ...]
49
- formatNumber(3286, 'en-US') // 3,286
50
- ```
51
-
52
- ## 约定
53
-
54
- - `ca.*` 由组件库保留;业务要改写组件文案时只覆盖 `ca` 下的**叶子键**,不要覆盖父级路径(会整段静默失效)。
55
- - 词条结构就是 vue-i18n 的 messages,已经用 vue-i18n 的项目不要再装第二个实例,把 `caMessages` 合并进自己的实例即可。
56
- - 找不到词条时原样返回 key,开发环境会打一次 `[@campus-admin/locale] 缺少词条:xxx`。
57
-
58
- 完整说明见仓库文档《Campus Admin 国际化使用指南》。
59
-
60
- ## 版权
61
-
62
- 使用本项目时请保留版权信息(MIT 许可要求随副本保留版权声明):
63
-
64
- - 保留包内的 `LICENSE` 文件与版权声明;
65
- - 保留页面页脚中的「Powered By 众校通 ®」与联系方式(示例用 `Copyright` 组件统一渲染);
66
- - 二次分发或修改后的版本,请在显著位置保留同样的版权声明。
67
-
68
- ---
69
-
70
- Powered By 众校通 ®
5
+ License: MIT.
@@ -0,0 +1,3 @@
1
+ import type { zhCN } from './zh-CN.js';
2
+ /** 与中文词条同结构;用类型约束保证键成对 */
3
+ export declare const enUS: typeof zhCN;
package/dist/index.d.ts CHANGED
@@ -1,50 +1,338 @@
1
- import type { App, InjectionKey, Ref } from 'vue';
2
- import type { CaLocale, CaLocaleOptions, LocaleMessages, LocaleMessageMap } from './types';
3
- export * from './format';
4
- export type { CaLocale, CaLocaleConfig, CaLocaleOptions, LocaleMessages, LocaleMessageMap, TranslateFn } from './types';
5
- /** 默认语言 */
6
- export declare const DEFAULT_LOCALE = "zh-CN";
7
- /** 内置支持的语言,业务可以在此基础上追加 */
8
- export declare const AVAILABLE_LOCALES: readonly ["zh-CN", "en-US"];
9
- /**
10
- * 组件库内置词条,按语言分组。
11
- * 结构与 vue-i18n 的 messages 一致,业务可以直接 mergeLocaleMessage 合并。
12
- */
13
- export declare const caMessages: LocaleMessageMap;
14
- /** 语言实例的注入键,业务替换实现时使用 */
15
- export declare const CA_LOCALE_KEY: InjectionKey<CaLocale>;
16
- /**
17
- * 按 'a.b.c' 取词条,取到非字符串节点时视为未命中。
18
- * 包内需要“取默认语言文案”的地方(例如考勤状态词典的兜底 label)也用它。
19
- */
20
- export declare function lookupMessage(messages: LocaleMessages, key: string): string | undefined;
21
- /**
22
- * 取内置默认语言(zh-CN)的某条文案。
23
- * 给不接入国际化的场景兜底用,例如考勤状态词典里的 label。
24
- */
25
- export declare function defaultMessage(key: string): string | undefined;
26
- /**
27
- * 创建语言实例。
28
- * 内置 zh-CN / en-US 词条,options.messages 里的业务词条深合并在其上。
29
- */
30
- export declare function createLocale(options?: CaLocaleOptions): CaLocale;
31
- /**
32
- * 把语言实例注入到应用,并设为当前实例。
33
- * campus-admin 与 CampusUI 插件在 install 时调用。
34
- */
35
- export declare function installLocale(app: App, locale: CaLocale): void;
36
- /** 当前语言实例,非 setup 场景(路由、请求拦截器)使用 */
37
- export declare function getCurrentLocale(): CaLocale;
38
- /** 切换当前实例的语言 */
39
- export declare function setLocale(locale: string): void;
40
- /** 当前语言 */
41
- export declare function getLocale(): string;
42
- /** 当前语言的响应式引用,需要跟随语言变化时使用 */
43
- export declare function useLocaleRef(): Ref<string>;
44
- /**
45
- * 组件内取词。返回当前应用的语言实例,注入不到时回退到内置实例。
46
- * 必须在 setup 中调用(内部使用 inject)。
47
- */
48
- export declare function useLocale(): CaLocale;
49
- /** 仅 setup 中可用:向子组件提供语言实例(一般由应用安装时统一处理) */
50
- export declare function provideLocale(locale: CaLocale): void;
1
+ import { enUS } from './en-US.js';
2
+ import { zhCN } from './zh-CN.js';
3
+ export type LocaleCode = 'zh-CN' | 'en-US';
4
+ export declare const LOCALES: {
5
+ code: LocaleCode;
6
+ label: string;
7
+ }[];
8
+ export declare const frameworkMessages: {
9
+ 'zh-CN': {
10
+ campus: {
11
+ notify: {
12
+ success: string;
13
+ info: string;
14
+ warning: string;
15
+ error: string;
16
+ };
17
+ common: {
18
+ ok: string;
19
+ cancel: string;
20
+ confirm: string;
21
+ tip: string;
22
+ loading: string;
23
+ };
24
+ login: {
25
+ title: string;
26
+ username: string;
27
+ password: string;
28
+ submit: string;
29
+ failed: string;
30
+ required: string;
31
+ };
32
+ auth: {
33
+ tabAccount: string;
34
+ tabWechat: string;
35
+ tabRegister: string;
36
+ tabForgot: string;
37
+ backLogin: string;
38
+ accountPlaceholder: string;
39
+ passwordPlaceholder: string;
40
+ captcha: string;
41
+ captchaPlaceholder: string;
42
+ captchaRefresh: string;
43
+ captchaFailed: string;
44
+ captchaRequired: string;
45
+ wechatHint: string;
46
+ wechatNote: string;
47
+ qrcodeAlt: string;
48
+ helpTitle: string;
49
+ help1: string;
50
+ help2: string;
51
+ help3: string;
52
+ help4: string;
53
+ findPassword: string;
54
+ forgotLink: string;
55
+ registerLink: string;
56
+ phone: string;
57
+ phonePlaceholder: string;
58
+ phoneInvalid: string;
59
+ account: string;
60
+ accountRequired: string;
61
+ smsCode: string;
62
+ smsPlaceholder: string;
63
+ getCode: string;
64
+ resend: string;
65
+ smsSent: string;
66
+ smsNeedPhone: string;
67
+ smsNeedAccount: string;
68
+ registerPassword: string;
69
+ registerPasswordPlaceholder: string;
70
+ newPasswordPlaceholder: string;
71
+ confirmPlaceholder: string;
72
+ passwordPolicy: string;
73
+ sessionExpired: string;
74
+ passwordMismatch: string;
75
+ agreePrefix: string;
76
+ agreeRequired: string;
77
+ registerOk: string;
78
+ resetOk: string;
79
+ previewNote: string;
80
+ unavailableTitle: string;
81
+ unavailableDesc: string;
82
+ };
83
+ header: {
84
+ language: string;
85
+ theme: string;
86
+ logout: string;
87
+ myAccount: string;
88
+ search: string;
89
+ shortcuts: string;
90
+ fullscreen: string;
91
+ exitFullscreen: string;
92
+ notifications: string;
93
+ assistant: string;
94
+ appearance: string;
95
+ settings: string;
96
+ account: string;
97
+ };
98
+ settings: {
99
+ title: string;
100
+ themeMode: string;
101
+ themeColor: string;
102
+ themeColorCustom: string;
103
+ themeColorReset: string;
104
+ boxStyle: string;
105
+ boxBorder: string;
106
+ boxShadow: string;
107
+ close: string;
108
+ };
109
+ mode: {
110
+ light: string;
111
+ dark: string;
112
+ auto: string;
113
+ };
114
+ side: {
115
+ home: string;
116
+ collapse: string;
117
+ expand: string;
118
+ hideText: string;
119
+ showText: string;
120
+ openMenu: string;
121
+ closeMenu: string;
122
+ };
123
+ breadcrumb: {
124
+ current: string;
125
+ };
126
+ tabs: {
127
+ refresh: string;
128
+ close: string;
129
+ closeLeft: string;
130
+ closeRight: string;
131
+ closeOthers: string;
132
+ closeAll: string;
133
+ fullscreen: string;
134
+ exitFullscreen: string;
135
+ };
136
+ table: {
137
+ search: string;
138
+ reset: string;
139
+ empty: string;
140
+ loadFailed: string;
141
+ retry: string;
142
+ total: string;
143
+ refresh: string;
144
+ };
145
+ form: {
146
+ required: string;
147
+ submit: string;
148
+ reset: string;
149
+ };
150
+ chart: {
151
+ compact: string;
152
+ empty: string;
153
+ loading: string;
154
+ total: string;
155
+ category: string;
156
+ value: string;
157
+ percent: string;
158
+ summaryLine: string;
159
+ summaryBar: string;
160
+ summarySeries: string;
161
+ summaryPie: string;
162
+ summaryPieItem: string;
163
+ summarySpark: string;
164
+ summaryGauge: string;
165
+ };
166
+ error: {
167
+ forbidden: string;
168
+ notFound: string;
169
+ backHome: string;
170
+ };
171
+ };
172
+ };
173
+ 'en-US': {
174
+ campus: {
175
+ notify: {
176
+ success: string;
177
+ info: string;
178
+ warning: string;
179
+ error: string;
180
+ };
181
+ common: {
182
+ ok: string;
183
+ cancel: string;
184
+ confirm: string;
185
+ tip: string;
186
+ loading: string;
187
+ };
188
+ login: {
189
+ title: string;
190
+ username: string;
191
+ password: string;
192
+ submit: string;
193
+ failed: string;
194
+ required: string;
195
+ };
196
+ auth: {
197
+ tabAccount: string;
198
+ tabWechat: string;
199
+ tabRegister: string;
200
+ tabForgot: string;
201
+ backLogin: string;
202
+ accountPlaceholder: string;
203
+ passwordPlaceholder: string;
204
+ captcha: string;
205
+ captchaPlaceholder: string;
206
+ captchaRefresh: string;
207
+ captchaFailed: string;
208
+ captchaRequired: string;
209
+ wechatHint: string;
210
+ wechatNote: string;
211
+ qrcodeAlt: string;
212
+ helpTitle: string;
213
+ help1: string;
214
+ help2: string;
215
+ help3: string;
216
+ help4: string;
217
+ findPassword: string;
218
+ forgotLink: string;
219
+ registerLink: string;
220
+ phone: string;
221
+ phonePlaceholder: string;
222
+ phoneInvalid: string;
223
+ account: string;
224
+ accountRequired: string;
225
+ smsCode: string;
226
+ smsPlaceholder: string;
227
+ getCode: string;
228
+ resend: string;
229
+ smsSent: string;
230
+ smsNeedPhone: string;
231
+ smsNeedAccount: string;
232
+ registerPassword: string;
233
+ registerPasswordPlaceholder: string;
234
+ newPasswordPlaceholder: string;
235
+ confirmPlaceholder: string;
236
+ passwordPolicy: string;
237
+ sessionExpired: string;
238
+ passwordMismatch: string;
239
+ agreePrefix: string;
240
+ agreeRequired: string;
241
+ registerOk: string;
242
+ resetOk: string;
243
+ previewNote: string;
244
+ unavailableTitle: string;
245
+ unavailableDesc: string;
246
+ };
247
+ header: {
248
+ language: string;
249
+ theme: string;
250
+ logout: string;
251
+ myAccount: string;
252
+ search: string;
253
+ shortcuts: string;
254
+ fullscreen: string;
255
+ exitFullscreen: string;
256
+ notifications: string;
257
+ assistant: string;
258
+ appearance: string;
259
+ settings: string;
260
+ account: string;
261
+ };
262
+ settings: {
263
+ title: string;
264
+ themeMode: string;
265
+ themeColor: string;
266
+ themeColorCustom: string;
267
+ themeColorReset: string;
268
+ boxStyle: string;
269
+ boxBorder: string;
270
+ boxShadow: string;
271
+ close: string;
272
+ };
273
+ mode: {
274
+ light: string;
275
+ dark: string;
276
+ auto: string;
277
+ };
278
+ side: {
279
+ home: string;
280
+ collapse: string;
281
+ expand: string;
282
+ hideText: string;
283
+ showText: string;
284
+ openMenu: string;
285
+ closeMenu: string;
286
+ };
287
+ breadcrumb: {
288
+ current: string;
289
+ };
290
+ tabs: {
291
+ refresh: string;
292
+ close: string;
293
+ closeLeft: string;
294
+ closeRight: string;
295
+ closeOthers: string;
296
+ closeAll: string;
297
+ fullscreen: string;
298
+ exitFullscreen: string;
299
+ };
300
+ table: {
301
+ search: string;
302
+ reset: string;
303
+ empty: string;
304
+ loadFailed: string;
305
+ retry: string;
306
+ total: string;
307
+ refresh: string;
308
+ };
309
+ form: {
310
+ required: string;
311
+ submit: string;
312
+ reset: string;
313
+ };
314
+ chart: {
315
+ compact: string;
316
+ empty: string;
317
+ loading: string;
318
+ total: string;
319
+ category: string;
320
+ value: string;
321
+ percent: string;
322
+ summaryLine: string;
323
+ summaryBar: string;
324
+ summarySeries: string;
325
+ summaryPie: string;
326
+ summaryPieItem: string;
327
+ summarySpark: string;
328
+ summaryGauge: string;
329
+ };
330
+ error: {
331
+ forbidden: string;
332
+ notFound: string;
333
+ backHome: string;
334
+ };
335
+ };
336
+ };
337
+ };
338
+ export { zhCN, enUS };