intor-translator 1.2.5 → 1.3.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/dist/index.cjs CHANGED
@@ -466,5 +466,23 @@ function parseRichMessage(message) {
466
466
  return buildAST(tokens);
467
467
  }
468
468
 
469
+ // src/message/render/render.ts
470
+ function render(nodes, renderer) {
471
+ return nodes.map((node) => {
472
+ if (node.type === "text") {
473
+ return renderer.text(node.value);
474
+ }
475
+ const children = render(node.children, renderer);
476
+ return renderer.tag(node.name, node.attributes, children);
477
+ });
478
+ }
479
+
480
+ // src/message/render-rich-message.ts
481
+ function renderRichMessage(message, renderer) {
482
+ const nodes = parseRichMessage(message);
483
+ return render(nodes, renderer);
484
+ }
485
+
469
486
  exports.Translator = ScopeTranslator;
470
487
  exports.parseRichMessage = parseRichMessage;
488
+ exports.renderRichMessage = renderRichMessage;
package/dist/index.d.cts CHANGED
@@ -465,4 +465,35 @@ interface TagNode {
465
465
  */
466
466
  declare function parseRichMessage(message: string): ASTNode[];
467
467
 
468
- export { type DefaultDepth, type FallbackLocalesMap, type FormatHandler, type HandlerContext, type LeafKeys, type LoadingHandler, type Locale, type LocaleMessages, type LocalizedLeafKeys, type LocalizedMessagesUnion, type LocalizedNodeKeys, type MissingHandler, type NestedMessage, type NodeKeys, type Replacement, type ScopedLeafKeys, type TranslateConfig, type TranslateContext, type TranslateHandlers, type TranslateHook, ScopeTranslator as Translator, type ScopeTranslatorMethods as TranslatorMethods, type ScopeTranslatorOptions as TranslatorOptions, type TranslatorPlugin, parseRichMessage };
468
+ /**
469
+ * Renderer interface for semantic message ASTs.
470
+ *
471
+ * A renderer defines how semantic nodes are transformed into a concrete output
472
+ * representation (e.g. string, DOM nodes, React elements).
473
+ *
474
+ * This interface is stateless by design.
475
+ * All rendering behavior is expressed through implementation.
476
+ */
477
+ interface Renderer<Output> {
478
+ /** Render a plain text node. */
479
+ text(value: string): Output;
480
+ /** Render a semantic tag node with attributes and rendered children. */
481
+ tag(name: string, attributes: Attributes, children: Output[]): Output;
482
+ }
483
+
484
+ /**
485
+ * Render a rich-formatted message into a concrete output using a renderer.
486
+ *
487
+ * This function orchestrates the full rich message pipeline:
488
+ *
489
+ * - message (string) ⬇
490
+ * - tokenize
491
+ * - build AST
492
+ * - render via provided renderer
493
+ *
494
+ * All rendering behavior is defined by the given renderer, making this
495
+ * function environment-agnostic (string, DOM, React, etc.).
496
+ */
497
+ declare function renderRichMessage<Output>(message: string, renderer: Renderer<Output>): Output[];
498
+
499
+ export { type ASTNode, type DefaultDepth, type FallbackLocalesMap, type FormatHandler, type HandlerContext, type LeafKeys, type LoadingHandler, type Locale, type LocaleMessages, type LocalizedLeafKeys, type LocalizedMessagesUnion, type LocalizedNodeKeys, type MissingHandler, type NestedMessage, type NodeKeys, type Renderer, type Replacement, type ScopedLeafKeys, 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
@@ -465,4 +465,35 @@ interface TagNode {
465
465
  */
466
466
  declare function parseRichMessage(message: string): ASTNode[];
467
467
 
468
- export { type DefaultDepth, type FallbackLocalesMap, type FormatHandler, type HandlerContext, type LeafKeys, type LoadingHandler, type Locale, type LocaleMessages, type LocalizedLeafKeys, type LocalizedMessagesUnion, type LocalizedNodeKeys, type MissingHandler, type NestedMessage, type NodeKeys, type Replacement, type ScopedLeafKeys, type TranslateConfig, type TranslateContext, type TranslateHandlers, type TranslateHook, ScopeTranslator as Translator, type ScopeTranslatorMethods as TranslatorMethods, type ScopeTranslatorOptions as TranslatorOptions, type TranslatorPlugin, parseRichMessage };
468
+ /**
469
+ * Renderer interface for semantic message ASTs.
470
+ *
471
+ * A renderer defines how semantic nodes are transformed into a concrete output
472
+ * representation (e.g. string, DOM nodes, React elements).
473
+ *
474
+ * This interface is stateless by design.
475
+ * All rendering behavior is expressed through implementation.
476
+ */
477
+ interface Renderer<Output> {
478
+ /** Render a plain text node. */
479
+ text(value: string): Output;
480
+ /** Render a semantic tag node with attributes and rendered children. */
481
+ tag(name: string, attributes: Attributes, children: Output[]): Output;
482
+ }
483
+
484
+ /**
485
+ * Render a rich-formatted message into a concrete output using a renderer.
486
+ *
487
+ * This function orchestrates the full rich message pipeline:
488
+ *
489
+ * - message (string) ⬇
490
+ * - tokenize
491
+ * - build AST
492
+ * - render via provided renderer
493
+ *
494
+ * All rendering behavior is defined by the given renderer, making this
495
+ * function environment-agnostic (string, DOM, React, etc.).
496
+ */
497
+ declare function renderRichMessage<Output>(message: string, renderer: Renderer<Output>): Output[];
498
+
499
+ export { type ASTNode, type DefaultDepth, type FallbackLocalesMap, type FormatHandler, type HandlerContext, type LeafKeys, type LoadingHandler, type Locale, type LocaleMessages, type LocalizedLeafKeys, type LocalizedMessagesUnion, type LocalizedNodeKeys, type MissingHandler, type NestedMessage, type NodeKeys, type Renderer, type Replacement, type ScopedLeafKeys, 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
@@ -464,4 +464,21 @@ function parseRichMessage(message) {
464
464
  return buildAST(tokens);
465
465
  }
466
466
 
467
- export { ScopeTranslator as Translator, parseRichMessage };
467
+ // src/message/render/render.ts
468
+ function render(nodes, renderer) {
469
+ return nodes.map((node) => {
470
+ if (node.type === "text") {
471
+ return renderer.text(node.value);
472
+ }
473
+ const children = render(node.children, renderer);
474
+ return renderer.tag(node.name, node.attributes, children);
475
+ });
476
+ }
477
+
478
+ // src/message/render-rich-message.ts
479
+ function renderRichMessage(message, renderer) {
480
+ const nodes = parseRichMessage(message);
481
+ return render(nodes, renderer);
482
+ }
483
+
484
+ export { ScopeTranslator as Translator, parseRichMessage, renderRichMessage };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "intor-translator",
3
- "version": "1.2.5",
3
+ "version": "1.3.0",
4
4
  "description": "🤖 A modern, type-safe i18n engine.",
5
5
  "author": {
6
6
  "name": "Yiming Liao",
@@ -58,7 +58,7 @@
58
58
  "lint:debug": "eslint --debug",
59
59
  "knip": "knip --config .config/knip.config.ts",
60
60
  "examples:html": "vite --config examples/html/basic/vite.config.ts",
61
- "examples:html:rich": "vite --config examples/html/rich/vite.config.ts"
61
+ "examples:html:rich-message": "vite --config examples/html/rich-message/vite.config.ts"
62
62
  },
63
63
  "dependencies": {
64
64
  "rura": "1.0.7"