intor-translator 1.4.3 → 1.4.4

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
@@ -509,18 +509,44 @@ function buildAST(tokens) {
509
509
 
510
510
  // src/message/parse-rich-message.ts
511
511
  function parseRichMessage(message) {
512
- const tokens = tokenize(message);
513
- return buildAST(tokens);
512
+ if (message == null) return [];
513
+ if (typeof message === "string") {
514
+ const tokens = tokenize(message);
515
+ return buildAST(tokens);
516
+ }
517
+ if (typeof message === "number" || typeof message === "boolean") {
518
+ const tokens = tokenize(String(message));
519
+ return buildAST(tokens);
520
+ }
521
+ if (Array.isArray(message)) {
522
+ return message.flatMap((m) => parseRichMessage(m));
523
+ }
524
+ return [
525
+ {
526
+ type: "raw",
527
+ value: message
528
+ }
529
+ ];
514
530
  }
515
531
 
516
532
  // src/message/render/render.ts
517
533
  function render(nodes, renderer) {
518
534
  return nodes.map((node) => {
519
- if (node.type === "text") {
520
- return renderer.text(node.value);
535
+ switch (node.type) {
536
+ // Plain text node
537
+ case "text": {
538
+ return renderer.text(node.value);
539
+ }
540
+ // Semantic tag node
541
+ case "tag": {
542
+ const children = render(node.children, renderer);
543
+ return renderer.tag(node.name, node.attributes, children);
544
+ }
545
+ // Raw message value
546
+ case "raw": {
547
+ return renderer.raw(node.value);
548
+ }
521
549
  }
522
- const children = render(node.children, renderer);
523
- return renderer.tag(node.name, node.attributes, children);
524
550
  });
525
551
  }
526
552
 
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
@@ -507,18 +507,44 @@ function buildAST(tokens) {
507
507
 
508
508
  // src/message/parse-rich-message.ts
509
509
  function parseRichMessage(message) {
510
- const tokens = tokenize(message);
511
- return buildAST(tokens);
510
+ if (message == null) return [];
511
+ if (typeof message === "string") {
512
+ const tokens = tokenize(message);
513
+ return buildAST(tokens);
514
+ }
515
+ if (typeof message === "number" || typeof message === "boolean") {
516
+ const tokens = tokenize(String(message));
517
+ return buildAST(tokens);
518
+ }
519
+ if (Array.isArray(message)) {
520
+ return message.flatMap((m) => parseRichMessage(m));
521
+ }
522
+ return [
523
+ {
524
+ type: "raw",
525
+ value: message
526
+ }
527
+ ];
512
528
  }
513
529
 
514
530
  // src/message/render/render.ts
515
531
  function render(nodes, renderer) {
516
532
  return nodes.map((node) => {
517
- if (node.type === "text") {
518
- return renderer.text(node.value);
533
+ switch (node.type) {
534
+ // Plain text node
535
+ case "text": {
536
+ return renderer.text(node.value);
537
+ }
538
+ // Semantic tag node
539
+ case "tag": {
540
+ const children = render(node.children, renderer);
541
+ return renderer.tag(node.name, node.attributes, children);
542
+ }
543
+ // Raw message value
544
+ case "raw": {
545
+ return renderer.raw(node.value);
546
+ }
519
547
  }
520
- const children = render(node.children, renderer);
521
- return renderer.tag(node.name, node.attributes, children);
522
548
  });
523
549
  }
524
550
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "intor-translator",
3
- "version": "1.4.3",
3
+ "version": "1.4.4",
4
4
  "description": "🤖 A modern, type-safe i18n engine.",
5
5
  "author": {
6
6
  "name": "Yiming Liao",