@shuo-li/i18n 1.0.0

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 ADDED
File without changes
@@ -0,0 +1,132 @@
1
+ interface StoreConfig {
2
+ name: string;
3
+ cacheGroupKey?: string;
4
+ }
5
+ interface UnloginPullConfig {
6
+ path: string;
7
+ transform?: (raw: unknown) => PullLangBlock[];
8
+ }
9
+ interface PullConfig {
10
+ path: string;
11
+ method?: 'GET' | 'POST';
12
+ buildParams?: (internal: StandardPullParams) => Record<string, unknown>;
13
+ transform?: (raw: unknown) => PullLangBlock[];
14
+ }
15
+ interface SSESyncConfig {
16
+ path: string;
17
+ method?: 'GET' | 'POST';
18
+ buildParams?: (internal: Required<StandardPullParams>) => Record<string, unknown>;
19
+ transform?: (raw: unknown) => PullLangBlock[];
20
+ }
21
+ interface SSEConfig {
22
+ path: string;
23
+ transformMessage?: (raw: unknown) => SSEMessage;
24
+ }
25
+ interface I18nInitOptions {
26
+ language: string;
27
+ version?: number;
28
+ languages?: string[];
29
+ modules?: string[];
30
+ stores: StoreConfig[];
31
+ staticLocales?: Record<string, Partial<Record<string, Record<string, unknown>>>>;
32
+ apiContext: {
33
+ baseURL: string;
34
+ getHeaders: () => Record<string, string>;
35
+ isLoggedIn: () => boolean;
36
+ unloginPull?: UnloginPullConfig;
37
+ pull?: PullConfig;
38
+ sseSync?: SSESyncConfig;
39
+ sse?: SSEConfig;
40
+ };
41
+ }
42
+ interface I18nValue {
43
+ termCode?: string;
44
+ langValue?: string;
45
+ }
46
+ interface PullLangBlock {
47
+ langCode: string;
48
+ modules: Array<{
49
+ moduleCode: string;
50
+ version: number;
51
+ i18nValues: I18nValue[];
52
+ }>;
53
+ }
54
+ interface SSEMessage {
55
+ type: string;
56
+ langCode: string;
57
+ moduleCode: string;
58
+ /** SSE 消息携带的数据版本号,与本地 version 对比决定是否拉取 */
59
+ version: number;
60
+ storeName: string;
61
+ }
62
+ /**
63
+ * 传给 buildParams 的内部参数。
64
+ * 全量拉取时只有 storeName;增量拉取时四个字段均有值。
65
+ * version 对应旧的 fromSynTime,调用方通过 buildParams 映射为实际字段名。
66
+ */
67
+ interface StandardPullParams {
68
+ storeName: string;
69
+ langCode?: string;
70
+ moduleCode?: string;
71
+ version?: number;
72
+ }
73
+ interface ResourceRecord {
74
+ key: string;
75
+ moduleCode: string;
76
+ langCode: string;
77
+ /** 当前已存储数据的版本号(对应旧的 toSynTime) */
78
+ version: number;
79
+ resources: Record<string, unknown>;
80
+ }
81
+ interface WorkerTask {
82
+ storeName: string;
83
+ langCode?: string;
84
+ moduleCode?: string;
85
+ /** 全量拉取时为 0;SSE 增量拉取时为本地已有版本号 */
86
+ version?: number;
87
+ trigger?: 'init-full' | 'sse';
88
+ }
89
+ interface WorkerModuleLoadedMessage {
90
+ type: 'moduleLoaded';
91
+ storeName: string;
92
+ langCode: string;
93
+ moduleCode: string;
94
+ version: number;
95
+ }
96
+ interface WorkerDataReadyMessage {
97
+ type: 'dataReady';
98
+ storeName: string;
99
+ storeIndex: number;
100
+ record: ResourceRecord;
101
+ task: WorkerTask;
102
+ }
103
+ interface WorkerModuleErrorMessage {
104
+ type: 'moduleError';
105
+ storeName: string;
106
+ langCode?: string;
107
+ moduleCode?: string;
108
+ }
109
+ interface WorkerDoneMessage {
110
+ type: 'done';
111
+ }
112
+ interface WorkerErrorMessage {
113
+ type: 'error';
114
+ message?: string;
115
+ }
116
+ type WorkerOutMessage = WorkerModuleLoadedMessage | WorkerDataReadyMessage | WorkerModuleErrorMessage | WorkerDoneMessage | WorkerErrorMessage;
117
+ interface WorkerLike {
118
+ postMessage(data: unknown): void;
119
+ onMessage(handler: (data: WorkerOutMessage) => void): void;
120
+ terminate(): void;
121
+ }
122
+
123
+ declare function closeSSE(): void;
124
+ declare function initI18n(options: I18nInitOptions): Promise<void>;
125
+ declare function ensureModules(moduleCodes: string[]): Promise<void>;
126
+ declare function getResource(moduleCode: string, langCode: string): Promise<Record<string, unknown>>;
127
+ declare function getAllRecordsByModule(moduleCode: string): Promise<ResourceRecord[]>;
128
+
129
+ declare const I18N_RESOURCES_UPDATED_EVENT = "golucky:i18n-resources-updated";
130
+ declare function emitI18nResourcesUpdated(): void;
131
+
132
+ export { I18N_RESOURCES_UPDATED_EVENT as I, type PullLangBlock as P, type ResourceRecord as R, type SSEMessage as S, type WorkerLike as W, getResource as a, type I18nInitOptions as b, closeSSE as c, type StandardPullParams as d, ensureModules as e, type StoreConfig as f, getAllRecordsByModule as g, emitI18nResourcesUpdated as h, initI18n as i };
@@ -0,0 +1,132 @@
1
+ interface StoreConfig {
2
+ name: string;
3
+ cacheGroupKey?: string;
4
+ }
5
+ interface UnloginPullConfig {
6
+ path: string;
7
+ transform?: (raw: unknown) => PullLangBlock[];
8
+ }
9
+ interface PullConfig {
10
+ path: string;
11
+ method?: 'GET' | 'POST';
12
+ buildParams?: (internal: StandardPullParams) => Record<string, unknown>;
13
+ transform?: (raw: unknown) => PullLangBlock[];
14
+ }
15
+ interface SSESyncConfig {
16
+ path: string;
17
+ method?: 'GET' | 'POST';
18
+ buildParams?: (internal: Required<StandardPullParams>) => Record<string, unknown>;
19
+ transform?: (raw: unknown) => PullLangBlock[];
20
+ }
21
+ interface SSEConfig {
22
+ path: string;
23
+ transformMessage?: (raw: unknown) => SSEMessage;
24
+ }
25
+ interface I18nInitOptions {
26
+ language: string;
27
+ version?: number;
28
+ languages?: string[];
29
+ modules?: string[];
30
+ stores: StoreConfig[];
31
+ staticLocales?: Record<string, Partial<Record<string, Record<string, unknown>>>>;
32
+ apiContext: {
33
+ baseURL: string;
34
+ getHeaders: () => Record<string, string>;
35
+ isLoggedIn: () => boolean;
36
+ unloginPull?: UnloginPullConfig;
37
+ pull?: PullConfig;
38
+ sseSync?: SSESyncConfig;
39
+ sse?: SSEConfig;
40
+ };
41
+ }
42
+ interface I18nValue {
43
+ termCode?: string;
44
+ langValue?: string;
45
+ }
46
+ interface PullLangBlock {
47
+ langCode: string;
48
+ modules: Array<{
49
+ moduleCode: string;
50
+ version: number;
51
+ i18nValues: I18nValue[];
52
+ }>;
53
+ }
54
+ interface SSEMessage {
55
+ type: string;
56
+ langCode: string;
57
+ moduleCode: string;
58
+ /** SSE 消息携带的数据版本号,与本地 version 对比决定是否拉取 */
59
+ version: number;
60
+ storeName: string;
61
+ }
62
+ /**
63
+ * 传给 buildParams 的内部参数。
64
+ * 全量拉取时只有 storeName;增量拉取时四个字段均有值。
65
+ * version 对应旧的 fromSynTime,调用方通过 buildParams 映射为实际字段名。
66
+ */
67
+ interface StandardPullParams {
68
+ storeName: string;
69
+ langCode?: string;
70
+ moduleCode?: string;
71
+ version?: number;
72
+ }
73
+ interface ResourceRecord {
74
+ key: string;
75
+ moduleCode: string;
76
+ langCode: string;
77
+ /** 当前已存储数据的版本号(对应旧的 toSynTime) */
78
+ version: number;
79
+ resources: Record<string, unknown>;
80
+ }
81
+ interface WorkerTask {
82
+ storeName: string;
83
+ langCode?: string;
84
+ moduleCode?: string;
85
+ /** 全量拉取时为 0;SSE 增量拉取时为本地已有版本号 */
86
+ version?: number;
87
+ trigger?: 'init-full' | 'sse';
88
+ }
89
+ interface WorkerModuleLoadedMessage {
90
+ type: 'moduleLoaded';
91
+ storeName: string;
92
+ langCode: string;
93
+ moduleCode: string;
94
+ version: number;
95
+ }
96
+ interface WorkerDataReadyMessage {
97
+ type: 'dataReady';
98
+ storeName: string;
99
+ storeIndex: number;
100
+ record: ResourceRecord;
101
+ task: WorkerTask;
102
+ }
103
+ interface WorkerModuleErrorMessage {
104
+ type: 'moduleError';
105
+ storeName: string;
106
+ langCode?: string;
107
+ moduleCode?: string;
108
+ }
109
+ interface WorkerDoneMessage {
110
+ type: 'done';
111
+ }
112
+ interface WorkerErrorMessage {
113
+ type: 'error';
114
+ message?: string;
115
+ }
116
+ type WorkerOutMessage = WorkerModuleLoadedMessage | WorkerDataReadyMessage | WorkerModuleErrorMessage | WorkerDoneMessage | WorkerErrorMessage;
117
+ interface WorkerLike {
118
+ postMessage(data: unknown): void;
119
+ onMessage(handler: (data: WorkerOutMessage) => void): void;
120
+ terminate(): void;
121
+ }
122
+
123
+ declare function closeSSE(): void;
124
+ declare function initI18n(options: I18nInitOptions): Promise<void>;
125
+ declare function ensureModules(moduleCodes: string[]): Promise<void>;
126
+ declare function getResource(moduleCode: string, langCode: string): Promise<Record<string, unknown>>;
127
+ declare function getAllRecordsByModule(moduleCode: string): Promise<ResourceRecord[]>;
128
+
129
+ declare const I18N_RESOURCES_UPDATED_EVENT = "golucky:i18n-resources-updated";
130
+ declare function emitI18nResourcesUpdated(): void;
131
+
132
+ export { I18N_RESOURCES_UPDATED_EVENT as I, type PullLangBlock as P, type ResourceRecord as R, type SSEMessage as S, type WorkerLike as W, getResource as a, type I18nInitOptions as b, closeSSE as c, type StandardPullParams as d, ensureModules as e, type StoreConfig as f, getAllRecordsByModule as g, emitI18nResourcesUpdated as h, initI18n as i };
@@ -0,0 +1,88 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/adapters/web-worker-adapter.ts
2
+ var WebWorkerAdapter = class {
3
+ constructor(worker) {
4
+ this.worker = worker;
5
+ }
6
+ postMessage(data) {
7
+ this.worker.postMessage(data);
8
+ }
9
+ onMessage(handler) {
10
+ this.worker.onmessage = (e) => handler(e.data);
11
+ }
12
+ terminate() {
13
+ this.worker.terminate();
14
+ }
15
+ };
16
+
17
+ // src/core/hooks.ts
18
+
19
+
20
+
21
+
22
+ var _reacti18next = require('react-i18next');
23
+
24
+ var _react = require('react');
25
+ function useDict(dictCode) {
26
+ const { t, i18n } = _reacti18next.useTranslation.call(void 0, "ENUMS");
27
+ return _react.useMemo.call(void 0, () => {
28
+ const directNs = i18n.getResourceBundle(i18n.language, "ENUMS");
29
+ const directEntry = _optionalChain([directNs, 'optionalAccess', _ => _[dictCode]]);
30
+ if (directEntry && typeof directEntry === "object" && !Array.isArray(directEntry)) {
31
+ const raw = directEntry;
32
+ return {
33
+ raw,
34
+ options: Object.entries(raw).map(([value, label]) => ({ value, label }))
35
+ };
36
+ }
37
+ const wmsDict = _optionalChain([directNs, 'optionalAccess', _2 => _2["wms_dict_value"], 'optionalAccess', _3 => _3["dict_value_name_term_value"]]);
38
+ if (wmsDict) {
39
+ const prefix = `${dictCode}_`;
40
+ const raw = {};
41
+ for (const [k, v] of Object.entries(wmsDict)) {
42
+ if (k.startsWith(prefix)) {
43
+ raw[k.slice(prefix.length)] = v;
44
+ }
45
+ }
46
+ if (Object.keys(raw).length > 0) {
47
+ return {
48
+ raw,
49
+ options: Object.entries(raw).map(([value, label]) => ({ value, label }))
50
+ };
51
+ }
52
+ }
53
+ if (dictCode === "bool_active") {
54
+ return buildBoolActive(i18n.language, i18n);
55
+ }
56
+ const fallbackKey = `${dictCode}`;
57
+ const fallbackVal = t(fallbackKey, { returnObjects: true });
58
+ if (fallbackVal && typeof fallbackVal === "object") {
59
+ const raw = fallbackVal;
60
+ return {
61
+ raw,
62
+ options: Object.entries(raw).map(([value, label]) => ({ value, label }))
63
+ };
64
+ }
65
+ return { raw: {}, options: [] };
66
+ }, [dictCode, i18n.language]);
67
+ }
68
+ function buildBoolActive(lang, i18n) {
69
+ const common = i18n.getResourceBundle(lang, "COMMON");
70
+ const booleans = _optionalChain([common, 'optionalAccess', _4 => _4["select"], 'optionalAccess', _5 => _5["boolean"]]);
71
+ const button = _optionalChain([common, 'optionalAccess', _6 => _6["button"]]);
72
+ const trueLabel = _nullishCoalesce(_nullishCoalesce(_optionalChain([button, 'optionalAccess', _7 => _7["enable"]]), () => ( _optionalChain([booleans, 'optionalAccess', _8 => _8["true"]]))), () => ( "\u542F\u7528"));
73
+ const falseLabel = _nullishCoalesce(_nullishCoalesce(_optionalChain([button, 'optionalAccess', _9 => _9["deactivate"]]), () => ( _optionalChain([booleans, 'optionalAccess', _10 => _10["false"]]))), () => ( "\u505C\u7528"));
74
+ const raw = { true: trueLabel, false: falseLabel };
75
+ return {
76
+ raw,
77
+ options: [
78
+ { value: "true", label: trueLabel },
79
+ { value: "false", label: falseLabel }
80
+ ]
81
+ };
82
+ }
83
+
84
+
85
+
86
+
87
+
88
+ exports.WebWorkerAdapter = WebWorkerAdapter; exports.useDict = useDict; exports.useTranslation = _reacti18next.useTranslation;