intor-translator 1.4.3 → 1.4.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
@@ -176,7 +176,6 @@ var DEFAULT_HOOKS = [
176
176
  format,
177
177
  interpolate
178
178
  ];
179
- rura.rura.createPipeline(DEFAULT_HOOKS).debugHooks();
180
179
 
181
180
  // src/translators/base-translator/base-translator.ts
182
181
  var BaseTranslator = class {
@@ -509,18 +508,44 @@ function buildAST(tokens) {
509
508
 
510
509
  // src/message/parse-rich-message.ts
511
510
  function parseRichMessage(message) {
512
- const tokens = tokenize(message);
513
- return buildAST(tokens);
511
+ if (message == null) return [];
512
+ if (typeof message === "string") {
513
+ const tokens = tokenize(message);
514
+ return buildAST(tokens);
515
+ }
516
+ if (typeof message === "number" || typeof message === "boolean") {
517
+ const tokens = tokenize(String(message));
518
+ return buildAST(tokens);
519
+ }
520
+ if (Array.isArray(message)) {
521
+ return message.flatMap((m) => parseRichMessage(m));
522
+ }
523
+ return [
524
+ {
525
+ type: "raw",
526
+ value: message
527
+ }
528
+ ];
514
529
  }
515
530
 
516
531
  // src/message/render/render.ts
517
532
  function render(nodes, renderer) {
518
533
  return nodes.map((node) => {
519
- if (node.type === "text") {
520
- return renderer.text(node.value);
534
+ switch (node.type) {
535
+ // Plain text node
536
+ case "text": {
537
+ return renderer.text(node.value);
538
+ }
539
+ // Semantic tag node
540
+ case "tag": {
541
+ const children = render(node.children, renderer);
542
+ return renderer.tag(node.name, node.attributes, children);
543
+ }
544
+ // Raw message value
545
+ case "raw": {
546
+ return renderer.raw(node.value);
547
+ }
521
548
  }
522
- const children = render(node.children, renderer);
523
- return renderer.tag(node.name, node.attributes, children);
524
549
  });
525
550
  }
526
551
 
package/dist/index.d.cts CHANGED
@@ -458,7 +458,7 @@ declare class ScopeTranslator<M extends LocaleMessages | unknown = unknown, L ex
458
458
  type Attributes = Record<string, string>;
459
459
 
460
460
  /** Semantic node produced by the AST builder. */
461
- type ASTNode = TextNode | TagNode;
461
+ type ASTNode = TextNode | TagNode | RawNode;
462
462
  /** Plain text node in the semantic AST. */
463
463
  interface TextNode {
464
464
  type: "text";
@@ -471,23 +471,27 @@ interface TagNode {
471
471
  attributes: Attributes;
472
472
  children: ASTNode[];
473
473
  }
474
+ /** Raw node representing a non-tokenizable message value. */
475
+ interface RawNode {
476
+ type: "raw";
477
+ value: Exclude<MessageValue, string>;
478
+ }
474
479
 
475
480
  /**
476
- * Parse a rich-formatted message string into a semantic AST.
477
- *
478
- * This function is a high-level entry point for processing translated
479
- * messages that contain semantic tags (e.g. <b>, <a>, <i>).
481
+ * Parse a rich message value into a semantic AST.
480
482
  *
481
- * Internally, it performs the following steps:
483
+ * This is the main entry point for processing translated messages that may
484
+ * contain semantic markup (e.g. <b>, <a>, <i>) or non-string values.
482
485
  *
483
- * - message (string) ⬇
484
- * - tokenize
485
- * - build AST
486
+ * Behavior by message type:
487
+ * - string → tokenized and parsed into semantic AST
488
+ * - number/boolean → stringified, then tokenized and parsed
489
+ * - array → recursively flattened and parsed
490
+ * - object → preserved as a raw AST node
486
491
  *
487
- * The returned AST represents the semantic structure of the message
488
- * and is intended to be consumed by renderers or further processing stages.
492
+ * The returned AST is renderer-agnostic and represents semantic structure only.
489
493
  */
490
- declare function parseRichMessage(message: string): ASTNode[];
494
+ declare function parseRichMessage(message: MessageValue): ASTNode[];
491
495
 
492
496
  /**
493
497
  * Renderer interface for semantic message ASTs.
@@ -503,21 +507,23 @@ interface Renderer<Output> {
503
507
  text(value: string): Output;
504
508
  /** Render a semantic tag node with attributes and rendered children. */
505
509
  tag(name: string, attributes: Attributes, children: Output[]): Output;
510
+ /** Render a raw (non-tokenized) message value. */
511
+ raw(value: Exclude<MessageValue, string>): Output;
506
512
  }
507
513
 
508
514
  /**
509
- * Render a rich-formatted message into a concrete output using a renderer.
515
+ * Render a rich message value into a concrete output using the given renderer.
510
516
  *
511
- * This function orchestrates the full rich message pipeline:
517
+ * This function is the main entry point of the rich message pipeline.
518
+ * It orchestrates the full flow from message value to rendered output:
512
519
  *
513
- * - message (string)
514
- * - tokenize
515
- * - build AST
516
- * - render via provided renderer
520
+ * - MessageValue
521
+ * - parse into semantic AST
522
+ * - render AST via the provided renderer
517
523
  *
518
- * All rendering behavior is defined by the given renderer, making this
519
- * function environment-agnostic (string, DOM, React, etc.).
524
+ * All rendering behavior is delegated to the renderer, making this function
525
+ * fully environment-agnostic (e.g. string, DOM, React).
520
526
  */
521
- declare function renderRichMessage<Output>(message: string, renderer: Renderer<Output>): Output[];
527
+ declare function renderRichMessage<Output>(message: MessageValue, renderer: Renderer<Output>): Output[];
522
528
 
523
529
  export { type ASTNode, type Attributes, type DefaultDepth, type FallbackLocalesMap, type FormatHandler, type HandlerContext, type LeafKeys, type LeafValue, type LoadingHandler, type Locale, type LocaleMessages, type LocalizedLeafKeys, type LocalizedLeafValue, type LocalizedMessagesUnion, type LocalizedNodeKeys, type MessageObject, type MessageValue, type MissingHandler, type NodeKeys, type Renderer, type Replacement, type ScopedLeafKeys, type ScopedLeafValue, type TranslateConfig, type TranslateContext, type TranslateHandlers, type TranslateHook, ScopeTranslator as Translator, type ScopeTranslatorMethods as TranslatorMethods, type ScopeTranslatorOptions as TranslatorOptions, type TranslatorPlugin, parseRichMessage, renderRichMessage };
package/dist/index.d.ts CHANGED
@@ -458,7 +458,7 @@ declare class ScopeTranslator<M extends LocaleMessages | unknown = unknown, L ex
458
458
  type Attributes = Record<string, string>;
459
459
 
460
460
  /** Semantic node produced by the AST builder. */
461
- type ASTNode = TextNode | TagNode;
461
+ type ASTNode = TextNode | TagNode | RawNode;
462
462
  /** Plain text node in the semantic AST. */
463
463
  interface TextNode {
464
464
  type: "text";
@@ -471,23 +471,27 @@ interface TagNode {
471
471
  attributes: Attributes;
472
472
  children: ASTNode[];
473
473
  }
474
+ /** Raw node representing a non-tokenizable message value. */
475
+ interface RawNode {
476
+ type: "raw";
477
+ value: Exclude<MessageValue, string>;
478
+ }
474
479
 
475
480
  /**
476
- * Parse a rich-formatted message string into a semantic AST.
477
- *
478
- * This function is a high-level entry point for processing translated
479
- * messages that contain semantic tags (e.g. <b>, <a>, <i>).
481
+ * Parse a rich message value into a semantic AST.
480
482
  *
481
- * Internally, it performs the following steps:
483
+ * This is the main entry point for processing translated messages that may
484
+ * contain semantic markup (e.g. <b>, <a>, <i>) or non-string values.
482
485
  *
483
- * - message (string) ⬇
484
- * - tokenize
485
- * - build AST
486
+ * Behavior by message type:
487
+ * - string → tokenized and parsed into semantic AST
488
+ * - number/boolean → stringified, then tokenized and parsed
489
+ * - array → recursively flattened and parsed
490
+ * - object → preserved as a raw AST node
486
491
  *
487
- * The returned AST represents the semantic structure of the message
488
- * and is intended to be consumed by renderers or further processing stages.
492
+ * The returned AST is renderer-agnostic and represents semantic structure only.
489
493
  */
490
- declare function parseRichMessage(message: string): ASTNode[];
494
+ declare function parseRichMessage(message: MessageValue): ASTNode[];
491
495
 
492
496
  /**
493
497
  * Renderer interface for semantic message ASTs.
@@ -503,21 +507,23 @@ interface Renderer<Output> {
503
507
  text(value: string): Output;
504
508
  /** Render a semantic tag node with attributes and rendered children. */
505
509
  tag(name: string, attributes: Attributes, children: Output[]): Output;
510
+ /** Render a raw (non-tokenized) message value. */
511
+ raw(value: Exclude<MessageValue, string>): Output;
506
512
  }
507
513
 
508
514
  /**
509
- * Render a rich-formatted message into a concrete output using a renderer.
515
+ * Render a rich message value into a concrete output using the given renderer.
510
516
  *
511
- * This function orchestrates the full rich message pipeline:
517
+ * This function is the main entry point of the rich message pipeline.
518
+ * It orchestrates the full flow from message value to rendered output:
512
519
  *
513
- * - message (string)
514
- * - tokenize
515
- * - build AST
516
- * - render via provided renderer
520
+ * - MessageValue
521
+ * - parse into semantic AST
522
+ * - render AST via the provided renderer
517
523
  *
518
- * All rendering behavior is defined by the given renderer, making this
519
- * function environment-agnostic (string, DOM, React, etc.).
524
+ * All rendering behavior is delegated to the renderer, making this function
525
+ * fully environment-agnostic (e.g. string, DOM, React).
520
526
  */
521
- declare function renderRichMessage<Output>(message: string, renderer: Renderer<Output>): Output[];
527
+ declare function renderRichMessage<Output>(message: MessageValue, renderer: Renderer<Output>): Output[];
522
528
 
523
529
  export { type ASTNode, type Attributes, type DefaultDepth, type FallbackLocalesMap, type FormatHandler, type HandlerContext, type LeafKeys, type LeafValue, type LoadingHandler, type Locale, type LocaleMessages, type LocalizedLeafKeys, type LocalizedLeafValue, type LocalizedMessagesUnion, type LocalizedNodeKeys, type MessageObject, type MessageValue, type MissingHandler, type NodeKeys, type Renderer, type Replacement, type ScopedLeafKeys, type ScopedLeafValue, type TranslateConfig, type TranslateContext, type TranslateHandlers, type TranslateHook, ScopeTranslator as Translator, type ScopeTranslatorMethods as TranslatorMethods, type ScopeTranslatorOptions as TranslatorOptions, type TranslatorPlugin, parseRichMessage, renderRichMessage };
package/dist/index.js CHANGED
@@ -174,7 +174,6 @@ var DEFAULT_HOOKS = [
174
174
  format,
175
175
  interpolate
176
176
  ];
177
- rura.createPipeline(DEFAULT_HOOKS).debugHooks();
178
177
 
179
178
  // src/translators/base-translator/base-translator.ts
180
179
  var BaseTranslator = class {
@@ -507,18 +506,44 @@ function buildAST(tokens) {
507
506
 
508
507
  // src/message/parse-rich-message.ts
509
508
  function parseRichMessage(message) {
510
- const tokens = tokenize(message);
511
- return buildAST(tokens);
509
+ if (message == null) return [];
510
+ if (typeof message === "string") {
511
+ const tokens = tokenize(message);
512
+ return buildAST(tokens);
513
+ }
514
+ if (typeof message === "number" || typeof message === "boolean") {
515
+ const tokens = tokenize(String(message));
516
+ return buildAST(tokens);
517
+ }
518
+ if (Array.isArray(message)) {
519
+ return message.flatMap((m) => parseRichMessage(m));
520
+ }
521
+ return [
522
+ {
523
+ type: "raw",
524
+ value: message
525
+ }
526
+ ];
512
527
  }
513
528
 
514
529
  // src/message/render/render.ts
515
530
  function render(nodes, renderer) {
516
531
  return nodes.map((node) => {
517
- if (node.type === "text") {
518
- return renderer.text(node.value);
532
+ switch (node.type) {
533
+ // Plain text node
534
+ case "text": {
535
+ return renderer.text(node.value);
536
+ }
537
+ // Semantic tag node
538
+ case "tag": {
539
+ const children = render(node.children, renderer);
540
+ return renderer.tag(node.name, node.attributes, children);
541
+ }
542
+ // Raw message value
543
+ case "raw": {
544
+ return renderer.raw(node.value);
545
+ }
519
546
  }
520
- const children = render(node.children, renderer);
521
- return renderer.tag(node.name, node.attributes, children);
522
547
  });
523
548
  }
524
549
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "intor-translator",
3
- "version": "1.4.3",
3
+ "version": "1.4.5",
4
4
  "description": "🤖 A modern, type-safe i18n engine.",
5
5
  "author": {
6
6
  "name": "Yiming Liao",