@pactor-app/core 1.54.3 → 1.54.5

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/dist/index.cjs CHANGED
@@ -25,6 +25,9 @@ __export(index_exports, {
25
25
  createI18nContext: () => createI18nContext,
26
26
  isDslFragmentPayload: () => isDslFragmentPayload,
27
27
  isDslSourceRef: () => isDslSourceRef,
28
+ isI18nDescriptor: () => isI18nDescriptor,
29
+ isLocalizedText: () => isLocalizedText,
30
+ lookupI18nMessage: () => lookupI18nMessage,
28
31
  resolveRootNodes: () => resolveRootNodes
29
32
  });
30
33
  module.exports = __toCommonJS(index_exports);
@@ -110,38 +113,55 @@ function isStringMap(value) {
110
113
  }
111
114
  return Object.values(value).every((v) => typeof v === "string");
112
115
  }
116
+ function isRecord(value) {
117
+ return typeof value === "object" && value !== null;
118
+ }
119
+ function isI18nDescriptor(value) {
120
+ if (!isRecord(value) || !isRecord(value.$i18n)) {
121
+ return false;
122
+ }
123
+ const inner = value.$i18n;
124
+ if (typeof inner.key === "string") {
125
+ return true;
126
+ }
127
+ return isStringMap(inner.messages);
128
+ }
129
+ function isLocalizedText(value) {
130
+ return isRecord(value) && isRecord(value.$localizedText) && typeof value.$localizedText.template === "string";
131
+ }
132
+ function lookupI18nMessage(messages, locale, defaultLocale, key) {
133
+ const currentLocaleMessages = messages[locale];
134
+ if (isStringMap(currentLocaleMessages)) {
135
+ const value = currentLocaleMessages[key];
136
+ if (typeof value === "string") {
137
+ return { found: true, value, resolvedLocale: locale };
138
+ }
139
+ }
140
+ const flatValue = messages[key];
141
+ if (typeof flatValue === "string") {
142
+ return { found: true, value: flatValue };
143
+ }
144
+ if (locale !== defaultLocale) {
145
+ const defaultLocaleMessages = messages[defaultLocale];
146
+ if (isStringMap(defaultLocaleMessages)) {
147
+ const value = defaultLocaleMessages[key];
148
+ if (typeof value === "string") {
149
+ return { found: true, value, resolvedLocale: defaultLocale };
150
+ }
151
+ }
152
+ }
153
+ return { found: false, value: key };
154
+ }
113
155
  function createI18nContext(options = {}) {
114
156
  const locale = options.locale ?? "zh-CN";
115
157
  const defaultLocale = options.defaultLocale ?? "zh-CN";
116
158
  const messages = options.messages ?? {};
117
159
  const t = (key, params) => {
118
- let template;
119
- const currentLocaleMessages = messages[locale];
120
- if (isStringMap(currentLocaleMessages)) {
121
- const value = currentLocaleMessages[key];
122
- if (typeof value === "string") {
123
- template = value;
124
- }
125
- }
126
- if (template === void 0) {
127
- const flatValue = messages[key];
128
- if (typeof flatValue === "string") {
129
- template = flatValue;
130
- }
131
- }
132
- if (template === void 0 && locale !== defaultLocale) {
133
- const defaultLocaleMessages = messages[defaultLocale];
134
- if (isStringMap(defaultLocaleMessages)) {
135
- const value = defaultLocaleMessages[key];
136
- if (typeof value === "string") {
137
- template = value;
138
- }
139
- }
140
- }
141
- if (template === void 0) {
160
+ const { found, value } = lookupI18nMessage(messages, locale, defaultLocale, key);
161
+ if (!found) {
142
162
  return key;
143
163
  }
144
- return params ? interpolate(template, params) : template;
164
+ return params ? interpolate(value, params) : value;
145
165
  };
146
166
  return { locale, defaultLocale, t };
147
167
  }
@@ -152,5 +172,8 @@ function createI18nContext(options = {}) {
152
172
  createI18nContext,
153
173
  isDslFragmentPayload,
154
174
  isDslSourceRef,
175
+ isI18nDescriptor,
176
+ isLocalizedText,
177
+ lookupI18nMessage,
155
178
  resolveRootNodes
156
179
  });
package/dist/index.d.cts CHANGED
@@ -285,6 +285,54 @@ type Locale = string;
285
285
  type I18nMessages = Record<string, string | Record<string, string>>;
286
286
  /** 参数插值值 */
287
287
  type I18nParams = Record<string, unknown>;
288
+ /** 引用独立词条的可翻译描述(方式二:属性引用语言包) */
289
+ interface I18nKeyRef {
290
+ key: string;
291
+ params?: Record<string, unknown>;
292
+ fallback?: string;
293
+ }
294
+ /** 内联多语言的可翻译描述(方式一:属性内联多语言) */
295
+ interface I18nInlineMessages {
296
+ messages: Record<string, string>;
297
+ params?: Record<string, unknown>;
298
+ fallback?: string;
299
+ }
300
+ /**
301
+ * 作者态可翻译值。`$i18n` 包装对象不允许其他同级字段;
302
+ * 同一描述内 key 与 messages 互斥,由 schema 校验保证。
303
+ */
304
+ interface I18nValueDescriptor {
305
+ $i18n: I18nKeyRef | I18nInlineMessages;
306
+ }
307
+ /** 下发态动态文本:语言选择已完成,模板插值在客户端完成 */
308
+ interface LocalizedText {
309
+ $localizedText: {
310
+ template: string;
311
+ params?: Record<string, unknown>;
312
+ };
313
+ }
314
+ /** 单个语言包文件的内容 */
315
+ interface MessageBundle {
316
+ version: string;
317
+ messages: Record<string, string>;
318
+ }
319
+ /** 语言包集合:服务端语言包 API 与本地 YAML 转换入口的统一输出结构 */
320
+ interface MessageBundleSet {
321
+ appId: string;
322
+ locale: string;
323
+ release: string;
324
+ format: 'simple-v1';
325
+ bundles: Record<string, MessageBundle>;
326
+ }
327
+ /** 页面/片段下发时附带的本地化元信息 */
328
+ interface LocalizationMeta {
329
+ locale: string;
330
+ defaultLocale: string;
331
+ source?: string;
332
+ explicit?: boolean;
333
+ release?: string;
334
+ bundleVersions?: Record<string, string>;
335
+ }
288
336
  /**
289
337
  * i18n 上下文:暴露给 RuntimeContext 与表达式引擎。
290
338
  * `t` 为翻译函数,支持参数插值与回退。
@@ -303,6 +351,27 @@ interface CreateI18nOptions {
303
351
  defaultLocale?: Locale;
304
352
  messages?: I18nMessages;
305
353
  }
354
+ /** 判断是否为作者态 `$i18n` 翻译描述 */
355
+ declare function isI18nDescriptor(value: unknown): value is I18nValueDescriptor;
356
+ /** 判断是否为下发态 `$localizedText` 动态文本 */
357
+ declare function isLocalizedText(value: unknown): value is LocalizedText;
358
+ /** 词条查找结果 */
359
+ interface I18nLookupResult {
360
+ /** 是否命中词条(空字符串译文视为命中) */
361
+ found: boolean;
362
+ /** 命中时为译文原文(未插值),未命中时为 key 本身 */
363
+ value: string;
364
+ /** 命中的 locale 分组;扁平 key 命中或未命中时为 undefined */
365
+ resolvedLocale?: Locale;
366
+ }
367
+ /**
368
+ * 显式命中的词条查找。
369
+ *
370
+ * 查找顺序:当前 locale 分组 → 扁平 key → defaultLocale 分组 → 未命中。
371
+ * 空字符串视为命中(found=true),不能用 `value === key` 判断命中,
372
+ * 因为合法译文可能恰好等于 key。
373
+ */
374
+ declare function lookupI18nMessage(messages: I18nMessages, locale: Locale, defaultLocale: Locale, key: string): I18nLookupResult;
306
375
  /**
307
376
  * 创建翻译函数。
308
377
  *
@@ -315,4 +384,4 @@ interface CreateI18nOptions {
315
384
  */
316
385
  declare function createI18nContext(options?: CreateI18nOptions): I18nContext;
317
386
 
318
- export { type ActionConfig, type ApplicationDsl, type ConditionConfig, type CreateI18nOptions, type DataSourceConfig, type DataSourceType, type DslChildren, type DslChildrenItem, type DslComponentNode, type DslFragmentPayload, type DslNode, type DslNodeBase, type DslReferenceNode, type DslSourceRef, type DslVersion, type EventMap, type ExpressionString, type FragmentDsl, type I18nContext, type I18nMessages, type I18nParams, type LifecycleConfig, type LifecycleEventName, type Locale, type PageDsl, type PageMeta, type PermissionConfig, type PermissionDeniedStrategy, RUNTIME_SCOPES, Registry, type RequiresConfig, type RuntimeScope, createI18nContext, isDslFragmentPayload, isDslSourceRef, resolveRootNodes };
387
+ export { type ActionConfig, type ApplicationDsl, type ConditionConfig, type CreateI18nOptions, type DataSourceConfig, type DataSourceType, type DslChildren, type DslChildrenItem, type DslComponentNode, type DslFragmentPayload, type DslNode, type DslNodeBase, type DslReferenceNode, type DslSourceRef, type DslVersion, type EventMap, type ExpressionString, type FragmentDsl, type I18nContext, type I18nInlineMessages, type I18nKeyRef, type I18nLookupResult, type I18nMessages, type I18nParams, type I18nValueDescriptor, type LifecycleConfig, type LifecycleEventName, type Locale, type LocalizationMeta, type LocalizedText, type MessageBundle, type MessageBundleSet, type PageDsl, type PageMeta, type PermissionConfig, type PermissionDeniedStrategy, RUNTIME_SCOPES, Registry, type RequiresConfig, type RuntimeScope, createI18nContext, isDslFragmentPayload, isDslSourceRef, isI18nDescriptor, isLocalizedText, lookupI18nMessage, resolveRootNodes };
package/dist/index.d.ts CHANGED
@@ -285,6 +285,54 @@ type Locale = string;
285
285
  type I18nMessages = Record<string, string | Record<string, string>>;
286
286
  /** 参数插值值 */
287
287
  type I18nParams = Record<string, unknown>;
288
+ /** 引用独立词条的可翻译描述(方式二:属性引用语言包) */
289
+ interface I18nKeyRef {
290
+ key: string;
291
+ params?: Record<string, unknown>;
292
+ fallback?: string;
293
+ }
294
+ /** 内联多语言的可翻译描述(方式一:属性内联多语言) */
295
+ interface I18nInlineMessages {
296
+ messages: Record<string, string>;
297
+ params?: Record<string, unknown>;
298
+ fallback?: string;
299
+ }
300
+ /**
301
+ * 作者态可翻译值。`$i18n` 包装对象不允许其他同级字段;
302
+ * 同一描述内 key 与 messages 互斥,由 schema 校验保证。
303
+ */
304
+ interface I18nValueDescriptor {
305
+ $i18n: I18nKeyRef | I18nInlineMessages;
306
+ }
307
+ /** 下发态动态文本:语言选择已完成,模板插值在客户端完成 */
308
+ interface LocalizedText {
309
+ $localizedText: {
310
+ template: string;
311
+ params?: Record<string, unknown>;
312
+ };
313
+ }
314
+ /** 单个语言包文件的内容 */
315
+ interface MessageBundle {
316
+ version: string;
317
+ messages: Record<string, string>;
318
+ }
319
+ /** 语言包集合:服务端语言包 API 与本地 YAML 转换入口的统一输出结构 */
320
+ interface MessageBundleSet {
321
+ appId: string;
322
+ locale: string;
323
+ release: string;
324
+ format: 'simple-v1';
325
+ bundles: Record<string, MessageBundle>;
326
+ }
327
+ /** 页面/片段下发时附带的本地化元信息 */
328
+ interface LocalizationMeta {
329
+ locale: string;
330
+ defaultLocale: string;
331
+ source?: string;
332
+ explicit?: boolean;
333
+ release?: string;
334
+ bundleVersions?: Record<string, string>;
335
+ }
288
336
  /**
289
337
  * i18n 上下文:暴露给 RuntimeContext 与表达式引擎。
290
338
  * `t` 为翻译函数,支持参数插值与回退。
@@ -303,6 +351,27 @@ interface CreateI18nOptions {
303
351
  defaultLocale?: Locale;
304
352
  messages?: I18nMessages;
305
353
  }
354
+ /** 判断是否为作者态 `$i18n` 翻译描述 */
355
+ declare function isI18nDescriptor(value: unknown): value is I18nValueDescriptor;
356
+ /** 判断是否为下发态 `$localizedText` 动态文本 */
357
+ declare function isLocalizedText(value: unknown): value is LocalizedText;
358
+ /** 词条查找结果 */
359
+ interface I18nLookupResult {
360
+ /** 是否命中词条(空字符串译文视为命中) */
361
+ found: boolean;
362
+ /** 命中时为译文原文(未插值),未命中时为 key 本身 */
363
+ value: string;
364
+ /** 命中的 locale 分组;扁平 key 命中或未命中时为 undefined */
365
+ resolvedLocale?: Locale;
366
+ }
367
+ /**
368
+ * 显式命中的词条查找。
369
+ *
370
+ * 查找顺序:当前 locale 分组 → 扁平 key → defaultLocale 分组 → 未命中。
371
+ * 空字符串视为命中(found=true),不能用 `value === key` 判断命中,
372
+ * 因为合法译文可能恰好等于 key。
373
+ */
374
+ declare function lookupI18nMessage(messages: I18nMessages, locale: Locale, defaultLocale: Locale, key: string): I18nLookupResult;
306
375
  /**
307
376
  * 创建翻译函数。
308
377
  *
@@ -315,4 +384,4 @@ interface CreateI18nOptions {
315
384
  */
316
385
  declare function createI18nContext(options?: CreateI18nOptions): I18nContext;
317
386
 
318
- export { type ActionConfig, type ApplicationDsl, type ConditionConfig, type CreateI18nOptions, type DataSourceConfig, type DataSourceType, type DslChildren, type DslChildrenItem, type DslComponentNode, type DslFragmentPayload, type DslNode, type DslNodeBase, type DslReferenceNode, type DslSourceRef, type DslVersion, type EventMap, type ExpressionString, type FragmentDsl, type I18nContext, type I18nMessages, type I18nParams, type LifecycleConfig, type LifecycleEventName, type Locale, type PageDsl, type PageMeta, type PermissionConfig, type PermissionDeniedStrategy, RUNTIME_SCOPES, Registry, type RequiresConfig, type RuntimeScope, createI18nContext, isDslFragmentPayload, isDslSourceRef, resolveRootNodes };
387
+ export { type ActionConfig, type ApplicationDsl, type ConditionConfig, type CreateI18nOptions, type DataSourceConfig, type DataSourceType, type DslChildren, type DslChildrenItem, type DslComponentNode, type DslFragmentPayload, type DslNode, type DslNodeBase, type DslReferenceNode, type DslSourceRef, type DslVersion, type EventMap, type ExpressionString, type FragmentDsl, type I18nContext, type I18nInlineMessages, type I18nKeyRef, type I18nLookupResult, type I18nMessages, type I18nParams, type I18nValueDescriptor, type LifecycleConfig, type LifecycleEventName, type Locale, type LocalizationMeta, type LocalizedText, type MessageBundle, type MessageBundleSet, type PageDsl, type PageMeta, type PermissionConfig, type PermissionDeniedStrategy, RUNTIME_SCOPES, Registry, type RequiresConfig, type RuntimeScope, createI18nContext, isDslFragmentPayload, isDslSourceRef, isI18nDescriptor, isLocalizedText, lookupI18nMessage, resolveRootNodes };
package/dist/index.js CHANGED
@@ -79,38 +79,55 @@ function isStringMap(value) {
79
79
  }
80
80
  return Object.values(value).every((v) => typeof v === "string");
81
81
  }
82
+ function isRecord(value) {
83
+ return typeof value === "object" && value !== null;
84
+ }
85
+ function isI18nDescriptor(value) {
86
+ if (!isRecord(value) || !isRecord(value.$i18n)) {
87
+ return false;
88
+ }
89
+ const inner = value.$i18n;
90
+ if (typeof inner.key === "string") {
91
+ return true;
92
+ }
93
+ return isStringMap(inner.messages);
94
+ }
95
+ function isLocalizedText(value) {
96
+ return isRecord(value) && isRecord(value.$localizedText) && typeof value.$localizedText.template === "string";
97
+ }
98
+ function lookupI18nMessage(messages, locale, defaultLocale, key) {
99
+ const currentLocaleMessages = messages[locale];
100
+ if (isStringMap(currentLocaleMessages)) {
101
+ const value = currentLocaleMessages[key];
102
+ if (typeof value === "string") {
103
+ return { found: true, value, resolvedLocale: locale };
104
+ }
105
+ }
106
+ const flatValue = messages[key];
107
+ if (typeof flatValue === "string") {
108
+ return { found: true, value: flatValue };
109
+ }
110
+ if (locale !== defaultLocale) {
111
+ const defaultLocaleMessages = messages[defaultLocale];
112
+ if (isStringMap(defaultLocaleMessages)) {
113
+ const value = defaultLocaleMessages[key];
114
+ if (typeof value === "string") {
115
+ return { found: true, value, resolvedLocale: defaultLocale };
116
+ }
117
+ }
118
+ }
119
+ return { found: false, value: key };
120
+ }
82
121
  function createI18nContext(options = {}) {
83
122
  const locale = options.locale ?? "zh-CN";
84
123
  const defaultLocale = options.defaultLocale ?? "zh-CN";
85
124
  const messages = options.messages ?? {};
86
125
  const t = (key, params) => {
87
- let template;
88
- const currentLocaleMessages = messages[locale];
89
- if (isStringMap(currentLocaleMessages)) {
90
- const value = currentLocaleMessages[key];
91
- if (typeof value === "string") {
92
- template = value;
93
- }
94
- }
95
- if (template === void 0) {
96
- const flatValue = messages[key];
97
- if (typeof flatValue === "string") {
98
- template = flatValue;
99
- }
100
- }
101
- if (template === void 0 && locale !== defaultLocale) {
102
- const defaultLocaleMessages = messages[defaultLocale];
103
- if (isStringMap(defaultLocaleMessages)) {
104
- const value = defaultLocaleMessages[key];
105
- if (typeof value === "string") {
106
- template = value;
107
- }
108
- }
109
- }
110
- if (template === void 0) {
126
+ const { found, value } = lookupI18nMessage(messages, locale, defaultLocale, key);
127
+ if (!found) {
111
128
  return key;
112
129
  }
113
- return params ? interpolate(template, params) : template;
130
+ return params ? interpolate(value, params) : value;
114
131
  };
115
132
  return { locale, defaultLocale, t };
116
133
  }
@@ -120,5 +137,8 @@ export {
120
137
  createI18nContext,
121
138
  isDslFragmentPayload,
122
139
  isDslSourceRef,
140
+ isI18nDescriptor,
141
+ isLocalizedText,
142
+ lookupI18nMessage,
123
143
  resolveRootNodes
124
144
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pactor-app/core",
3
- "version": "1.54.3",
3
+ "version": "1.54.5",
4
4
  "description": "Pactor core: DSL protocol types and generic Registry (React-free, zero dependencies)",
5
5
  "keywords": [
6
6
  "pactor",