@worldware/msg 0.7.1 → 0.8.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 CHANGED
@@ -4,12 +4,13 @@ A TypeScript library for managing internationalization (i18n) messages with supp
4
4
 
5
5
  ## Overview
6
6
 
7
- `msg` provides a structured approach to managing translatable messages in your application. It integrates with [MessageFormat 2](https://messageformat.unicode.org/) (MF2) for advanced message formatting and supports:
7
+ `msg` provides a structured approach to managing translatable messages in your application. It integrates with [MessageFormat 2](https://messageformat.unicode.org/) (MF2) and [ICU MessageFormat 1](https://messageformat.github.io/) (MF1) for message formatting and supports:
8
8
 
9
9
  - **Message Management**: Organize messages into resources with keys and values
10
10
  - **Translation Loading**: Load translations from external sources via customizable loaders
11
11
  - **Pseudo Localization**: Request a pseudolocalized resource for UI testing via `getTranslation(pseudoLocale)`
12
- - **Message Formatting**: Format messages with parameters using MessageFormat 2 (MF2) syntax
12
+ - **Message Formatting**: Format messages with parameters using MessageFormat 2 (MF2) or MessageFormat 1 (MF1) syntax, or pass strings through unformatted
13
+ - **Configurable Format**: Choose `MF1`, `MF2`, or `NONE` per project, resource, or message via an inheritable `format` attribute (defaults to `MF2`)
13
14
  - **Attributes & Notes**: Attach metadata (language, direction, do-not-translate flags) and notes to messages
14
15
  - **Project Configuration**: Configure projects with locale settings and translation loaders
15
16
 
@@ -25,6 +26,7 @@ npm install @worldware/msg
25
26
 
26
27
  A project configuration that defines:
27
28
  - Project name and version
29
+ - The default message `format` (`MF1` | `MF2` | `NONE`, defaults to `MF2`) inherited by resources and messages
28
30
  - Source and target locales (with language fallback chains)
29
31
  - Pseudo locale (for pseudolocalized output via `getTranslation`)
30
32
  - A translation loader function
@@ -41,10 +43,10 @@ A collection of messages (extends `Map<string, MsgMessage>`) representing a reso
41
43
 
42
44
  An individual message with:
43
45
  - A key (identifier)
44
- - A value (the message text, supports MessageFormat 2 (MF2) syntax)
45
- - Attributes (lang, dir, dnt)
46
+ - A value (the message text, in MF2, MF1, or plain syntax depending on its `format`)
47
+ - Attributes (lang, dir, dnt, format)
46
48
  - Notes
47
- - Formatting methods using MessageFormat 2
49
+ - Formatting methods that honor the resolved `format` (MF2, MF1, or NONE)
48
50
 
49
51
  ## Usage
50
52
 
@@ -128,6 +130,42 @@ const formatted = greetingMsg?.format({ name: 'Alice' });
128
130
  // Result: "Hello, Alice!"
129
131
  ```
130
132
 
133
+ ### Message Formats (MF1, MF2, NONE)
134
+
135
+ Every message is formatted according to its resolved `format` attribute:
136
+
137
+ - `MF2` (default) — [Unicode MessageFormat 2](https://messageformat.unicode.org/) syntax, e.g. `Hello, {$name}!`.
138
+ - `MF1` — [ICU MessageFormat 1](https://messageformat.github.io/) syntax, e.g. `{count, plural, one {# file} other {# files}}`, formatted via [`@messageformat/icu-messageformat-1`](https://www.npmjs.com/package/@messageformat/icu-messageformat-1).
139
+ - `NONE` — the value is returned verbatim, with no parsing or interpolation.
140
+
141
+ The `format` is inheritable: a resource inherits its project's `format` unless it sets its own, and a message inherits its resource's `format` unless it sets its own. The default is `MF2`, so existing code keeps working unchanged. Use a TypeScript union (`'MF1' | 'MF2' | 'NONE'`) — there is no enum.
142
+
143
+ ```typescript
144
+ import { MsgProject, MsgResource } from '@worldware/msg';
145
+
146
+ // A project whose messages are MF1 by default
147
+ const project = MsgProject.create({
148
+ project: { name: 'legacy-app', version: 1, format: 'MF1' },
149
+ locales: { sourceLocale: 'en', pseudoLocale: 'en-XA', targetLocales: { en: ['en'] } },
150
+ loader
151
+ });
152
+
153
+ const resource = MsgResource.create({
154
+ title: 'Files',
155
+ attributes: { lang: 'en', dir: 'ltr' } // inherits format: 'MF1' from the project
156
+ }, project);
157
+
158
+ resource.add('files', '{count, plural, one {# file} other {# files}}'); // MF1 (inherited)
159
+ resource.add('brand', 'msg {version}', { format: 'NONE' }); // passed through
160
+ resource.add('hi', 'Hello, {$name}!', { format: 'MF2' }); // MF2 (override)
161
+
162
+ resource.get('files')?.format({ count: 2 }); // "2 files"
163
+ resource.get('brand')?.format({ version: 1 }); // "msg {version}"
164
+ resource.get('hi')?.format({ name: 'Ada' }); // "Hello, Ada!"
165
+ ```
166
+
167
+ When serializing, an inherited `format` is omitted to keep output compact: a resource omits `format` when it equals the project's, and a message omits `format` when it equals its resource's.
168
+
131
169
  ### Loading Translations
132
170
 
133
171
  ```typescript
@@ -214,9 +252,10 @@ const data = resource.getData();
214
252
  - `create(data: MsgProjectData): MsgProject` - Create a new project instance
215
253
 
216
254
  **Properties:**
217
- - `project: MsgProjectSettings` - Project name and version
255
+ - `project: MsgProjectSettings` - Project name, version, and default `format`
218
256
  - `locales: MsgLocalesSettings` - Locale configuration
219
257
  - `loader: MsgTranslationLoader` - Translation loader function
258
+ - `format: MsgFormat` - The project-wide default format (`'MF1' | 'MF2' | 'NONE'`), defaulting to `'MF2'`; resources (and, through them, messages) inherit this value unless they specify their own
220
259
 
221
260
  **Methods:**
222
261
  - `getTargetLocale(locale: string): string[] | undefined` - Returns the language fallback chain (array of locale codes) for the specified locale, or `undefined` if the locale is not configured in `targetLocales`
@@ -231,7 +270,7 @@ const data = resource.getData();
231
270
  - `translate(data: MsgResourceData): MsgResource` - Create a translated version
232
271
  - `getTranslation(lang: string): Promise<MsgResource>` - Load and apply translations. When `lang` matches the project's `pseudoLocale`, returns a resource with pseudolocalized message values instead of loading from the loader.
233
272
  - `getProject(): MsgProject` - Returns the project instance associated with the resource
234
- - `getData(stripNotes?: boolean): MsgResourceData` - Get resource data. Message objects in the output omit `attributes` when they match the resource's attributes (to avoid redundancy)
273
+ - `getData(stripNotes?: boolean): MsgResourceData` - Get resource data. Message objects in the output omit `attributes` when they match the resource's attributes (to avoid redundancy). The resource's `format` is omitted when it equals the project's, and a message's `format` is omitted when it equals the resource's
235
274
  - `toJSON(stripNotes?: boolean): string` - Serialize to JSON
236
275
 
237
276
  **Properties:**
@@ -245,8 +284,8 @@ const data = resource.getData();
245
284
  - `create(data: MsgMessageData): MsgMessage` - Create a new message
246
285
 
247
286
  **Methods:**
248
- - `format(data: Record<string, any>, options?: MessageFormatOptions): string` - Format the message
249
- - `formatToParts(data: Record<string, any>, options?: MessageFormatOptions): MessagePart[]` - Format to parts
287
+ - `format(data: Record<string, any>, options?: MessageFormatOptions): string` - Format the message according to its resolved `format`: `MF2` uses MessageFormat 2, `MF1` compiles via `@messageformat/icu-messageformat-1`, and `NONE` returns the raw value
288
+ - `formatToParts(data: Record<string, any>, options?: MessageFormatOptions): MessagePart[]` - Format to parts (for `NONE`, a single `{ type: 'text', value }` part)
250
289
  - `addNote(note: MsgNote): void` - Add a note
251
290
  - `getData(stripNotes?: boolean): MsgMessageData` - Get message data
252
291
  - `toJSON(stripNotes?: boolean): string` - Serialize to JSON
@@ -254,9 +293,14 @@ const data = resource.getData();
254
293
  **Properties:**
255
294
  - `key: string` - Message key
256
295
  - `value: string` - Message value
257
- - `attributes: MsgAttributes` - Message attributes (lang, dir, dnt)
296
+ - `attributes: MsgAttributes` - Message attributes (lang, dir, dnt, format)
258
297
  - `notes: MsgNote[]` - Message notes
259
298
 
299
+ ### Types
300
+
301
+ - `MsgFormat` - `'MF1' | 'MF2' | 'NONE'`; the formatting syntax for a message.
302
+ - `MsgAttributes` - `{ lang?: string; dir?: string; dnt?: boolean; format?: MsgFormat }`.
303
+
260
304
  ## Development
261
305
 
262
306
  ```bash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@worldware/msg",
3
- "version": "0.7.1",
3
+ "version": "0.8.0",
4
4
  "description": "Message localization tooling",
5
5
  "license": "MIT",
6
6
  "author": "Joel Sahleen",
@@ -33,18 +33,22 @@
33
33
  "devDependencies": {
34
34
  "@types/typescript": "^0.4.29",
35
35
  "@vitest/coverage-v8": "^4.0.15",
36
+ "semantic-release": "^25.0.8",
36
37
  "tsup": "^8.5.1",
38
+ "typedoc": "^0.28.20",
37
39
  "typescript": "^5.9.3",
38
40
  "vitest": "^4.0.15"
39
41
  },
40
42
  "dependencies": {
41
- "messageformat": "4.0.0-10",
43
+ "@messageformat/icu-messageformat-1": "^0.12.0",
44
+ "messageformat": "^4.0.0",
42
45
  "pseudo-localization": "^2.4.0"
43
46
  },
44
47
  "scripts": {
45
48
  "build": "tsup",
46
49
  "test": "vitest run",
47
50
  "test:watch": "vitest",
48
- "coverage": "vitest run --coverage"
51
+ "coverage": "vitest run --coverage",
52
+ "docs": "typedoc src/index.ts"
49
53
  }
50
54
  }
@@ -1,184 +0,0 @@
1
- import {
2
- MsgMessage
3
- } from "./chunk-WUDKNZV2.mjs";
4
- import {
5
- DEFAULT_ATTRIBUTES
6
- } from "./chunk-QWBDIKQK.mjs";
7
-
8
- // src/classes/MsgResource/MsgResource.ts
9
- import { parseMessage, stringifyMessage, visit } from "messageformat";
10
- import { localize } from "pseudo-localization";
11
- var MsgResource = class _MsgResource extends Map {
12
- _attributes = {};
13
- _notes = [];
14
- _title;
15
- _project;
16
- static create(data, project) {
17
- const { title, attributes, notes, messages } = data;
18
- const res = new _MsgResource(title, attributes, project, notes);
19
- if (messages) {
20
- messages.forEach((messageData) => {
21
- const { key, value, attributes: attributes2, notes: notes2 } = messageData;
22
- res.add(key, value, attributes2, notes2);
23
- });
24
- } else {
25
- res.clear();
26
- }
27
- return res;
28
- }
29
- constructor(title, attributes, project, notes) {
30
- super();
31
- this._title = title;
32
- this._attributes = { ...DEFAULT_ATTRIBUTES, ...attributes };
33
- this._project = project;
34
- if (notes) {
35
- notes.forEach((note) => this.addNote(note));
36
- }
37
- }
38
- hasMatchingAttributes(message) {
39
- const res = this.attributes;
40
- const msg = message.attributes;
41
- return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt;
42
- }
43
- pseudoLocalizeMF2(source, options) {
44
- const msg = parseMessage(source);
45
- visit(msg, {
46
- pattern: (pattern) => {
47
- for (let i = 0; i < pattern.length; i++) {
48
- const part = pattern[i];
49
- if (typeof part === "string") {
50
- pattern[i] = localize(part, options);
51
- }
52
- }
53
- }
54
- });
55
- return stringifyMessage(msg);
56
- }
57
- get attributes() {
58
- return this._attributes;
59
- }
60
- set attributes(attributes) {
61
- this._attributes = attributes;
62
- }
63
- get notes() {
64
- return this._notes;
65
- }
66
- set notes(notes) {
67
- this._notes = notes;
68
- }
69
- addNote(note) {
70
- this.notes.push(note);
71
- }
72
- get title() {
73
- return this._title;
74
- }
75
- set title(title) {
76
- this._title = title;
77
- }
78
- getProject() {
79
- return this._project;
80
- }
81
- add(key, value, attributes, notes) {
82
- const merged = { ...this.attributes, ...attributes };
83
- const msg = MsgMessage.create({
84
- key,
85
- value,
86
- attributes: merged,
87
- notes
88
- });
89
- this.set(key, msg);
90
- return this;
91
- }
92
- translate(data) {
93
- const { title, attributes, messages } = data;
94
- if (title !== this.title) {
95
- throw new TypeError("Title of resource and translations do not match.");
96
- }
97
- const translated = _MsgResource.create({
98
- title,
99
- attributes,
100
- notes: this.notes
101
- // transfer the notes
102
- }, this._project);
103
- this.forEach((msg) => {
104
- translated.set(msg.key, msg);
105
- });
106
- messages?.forEach((messageData) => {
107
- const { key, value, attributes: attributes2 } = messageData;
108
- const msg = MsgMessage.create({
109
- key,
110
- value,
111
- attributes: attributes2
112
- });
113
- const notes = this.get(key)?.notes || [];
114
- notes.forEach((note) => {
115
- msg.addNote(note);
116
- });
117
- translated.set(key, msg);
118
- });
119
- return translated;
120
- }
121
- async getTranslation(lang) {
122
- const project = this._project;
123
- const pseudoLocale = project.locales.pseudoLocale;
124
- if (lang === pseudoLocale) {
125
- const pseudolocalizedData = {
126
- title: this.title,
127
- attributes: { ...this.attributes, lang: pseudoLocale },
128
- notes: this.notes.length > 0 ? this.notes : void 0,
129
- messages: []
130
- };
131
- this.forEach((msg) => {
132
- pseudolocalizedData.messages.push({
133
- key: msg.key,
134
- value: this.pseudoLocalizeMF2(msg.value),
135
- attributes: { ...this.attributes, lang: pseudoLocale }
136
- });
137
- });
138
- return this.translate(pseudolocalizedData);
139
- }
140
- const languageChain = project.getTargetLocale(lang);
141
- if (!languageChain) {
142
- throw new Error("Unsupported locale for resource.");
143
- }
144
- if (languageChain.length == 0) {
145
- throw new Error(`Empty language chain for locale: ${lang}`);
146
- }
147
- let translated = this;
148
- for (let i = 0; i < languageChain.length; i++) {
149
- const lang2 = languageChain[i];
150
- if (lang2 && project._locales.targetLocales[lang2]) {
151
- translated = await translated._project._loader(project.project.name, translated.title, lang2).then((data) => translated.translate(data));
152
- }
153
- }
154
- return translated;
155
- }
156
- getData(stripNotes = false) {
157
- const messages = [];
158
- this.forEach((msg) => {
159
- if (this.hasMatchingAttributes(msg)) {
160
- const data = {
161
- key: msg.key,
162
- value: msg.value,
163
- notes: !stripNotes && msg.notes.length > 0 ? msg.notes : void 0
164
- };
165
- messages.push(data);
166
- } else {
167
- messages.push(msg.getData(stripNotes));
168
- }
169
- });
170
- return {
171
- title: this.title,
172
- attributes: this.attributes,
173
- notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0,
174
- messages
175
- };
176
- }
177
- toJSON(stripNotes = false) {
178
- return JSON.stringify(this.getData(stripNotes), null, 2);
179
- }
180
- };
181
-
182
- export {
183
- MsgResource
184
- };
@@ -1,10 +0,0 @@
1
- // src/classes/MsgInterface/MsgInterface.ts
2
- var DEFAULT_ATTRIBUTES = {
3
- lang: "und",
4
- dir: "auto",
5
- dnt: false
6
- };
7
-
8
- export {
9
- DEFAULT_ATTRIBUTES
10
- };
@@ -1,71 +0,0 @@
1
- import {
2
- DEFAULT_ATTRIBUTES
3
- } from "./chunk-QWBDIKQK.mjs";
4
-
5
- // src/classes/MsgMessage/MsgMessage.ts
6
- import { MessageFormat } from "messageformat";
7
- var MsgMessage = class _MsgMessage {
8
- _key;
9
- _value;
10
- _mf;
11
- _attributes;
12
- _notes = [];
13
- constructor(key, value, attributes, notes) {
14
- this._key = key;
15
- this._value = value;
16
- this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
17
- if (notes) {
18
- notes.forEach((note) => this.addNote(note));
19
- }
20
- }
21
- static create(data) {
22
- const { key, value, attributes, notes } = data;
23
- const message = new _MsgMessage(key, value, attributes, notes);
24
- return message;
25
- }
26
- get key() {
27
- return this._key;
28
- }
29
- get value() {
30
- return this._value;
31
- }
32
- get attributes() {
33
- return this._attributes;
34
- }
35
- get notes() {
36
- return this._notes;
37
- }
38
- addNote(note) {
39
- this.notes.push(note);
40
- }
41
- format(data, options) {
42
- if (!this._mf) {
43
- this._mf = new MessageFormat(this.attributes.lang, this.value, options);
44
- }
45
- return this._mf.format(data);
46
- }
47
- formatToParts(data, options) {
48
- if (!this._mf) {
49
- this._mf = new MessageFormat(this.attributes.lang, this.value, options);
50
- }
51
- return this._mf?.formatToParts(data);
52
- }
53
- getData(stripNotes = false) {
54
- return {
55
- key: this.key,
56
- value: this.value,
57
- attributes: this.attributes,
58
- notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0
59
- };
60
- }
61
- toString() {
62
- return this.value;
63
- }
64
- toJSON(stripNotes = false) {
65
- return JSON.stringify(this.getData(stripNotes), null, 2);
66
- }
67
- };
68
-
69
- export {
70
- MsgMessage
71
- };
@@ -1,42 +0,0 @@
1
- // src/classes/MsgProject/MsgProject.ts
2
- var defaultProjectSettings = {
3
- name: "messages",
4
- version: 1
5
- };
6
- var defaultLocalesSettings = {
7
- sourceLocale: "en",
8
- pseudoLocale: "en-XA",
9
- targetLocales: {
10
- en: ["en"]
11
- }
12
- };
13
- var MsgProject = class _MsgProject {
14
- _project;
15
- _locales;
16
- _loader;
17
- static create(data) {
18
- const { project, locales, loader } = data;
19
- return new _MsgProject(project, locales, loader);
20
- }
21
- constructor(projectSettings, localesSettings, loader) {
22
- this._project = { ...defaultProjectSettings, ...projectSettings };
23
- this._locales = { ...defaultLocalesSettings, ...localesSettings };
24
- this._loader = loader;
25
- }
26
- get project() {
27
- return this._project;
28
- }
29
- get locales() {
30
- return this._locales;
31
- }
32
- get loader() {
33
- return this._loader;
34
- }
35
- getTargetLocale(locale) {
36
- return this._locales.targetLocales[locale];
37
- }
38
- };
39
-
40
- export {
41
- MsgProject
42
- };
File without changes
@@ -1,34 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/classes/MsgInterface/MsgInterface.ts
21
- var MsgInterface_exports = {};
22
- __export(MsgInterface_exports, {
23
- DEFAULT_ATTRIBUTES: () => DEFAULT_ATTRIBUTES
24
- });
25
- module.exports = __toCommonJS(MsgInterface_exports);
26
- var DEFAULT_ATTRIBUTES = {
27
- lang: "und",
28
- dir: "auto",
29
- dnt: false
30
- };
31
- // Annotate the CommonJS export names for ESM import in node:
32
- 0 && (module.exports = {
33
- DEFAULT_ATTRIBUTES
34
- });
@@ -1,17 +0,0 @@
1
- type NoteTypes = 'DESCRIPTION' | 'AUTHORSHIP' | 'PARAMETERS' | 'CONTEXT' | 'COMMENT';
2
- type MsgNote = {
3
- type: NoteTypes;
4
- content: string;
5
- };
6
- type MsgAttributes = {
7
- lang?: string;
8
- dir?: string;
9
- dnt?: boolean;
10
- };
11
- declare const DEFAULT_ATTRIBUTES: MsgAttributes;
12
- interface MsgInterface {
13
- attributes: MsgAttributes;
14
- notes: MsgNote[];
15
- }
16
-
17
- export { DEFAULT_ATTRIBUTES, type MsgAttributes, type MsgInterface, type MsgNote };
@@ -1,17 +0,0 @@
1
- type NoteTypes = 'DESCRIPTION' | 'AUTHORSHIP' | 'PARAMETERS' | 'CONTEXT' | 'COMMENT';
2
- type MsgNote = {
3
- type: NoteTypes;
4
- content: string;
5
- };
6
- type MsgAttributes = {
7
- lang?: string;
8
- dir?: string;
9
- dnt?: boolean;
10
- };
11
- declare const DEFAULT_ATTRIBUTES: MsgAttributes;
12
- interface MsgInterface {
13
- attributes: MsgAttributes;
14
- notes: MsgNote[];
15
- }
16
-
17
- export { DEFAULT_ATTRIBUTES, type MsgAttributes, type MsgInterface, type MsgNote };
@@ -1,6 +0,0 @@
1
- import {
2
- DEFAULT_ATTRIBUTES
3
- } from "../../chunk-QWBDIKQK.mjs";
4
- export {
5
- DEFAULT_ATTRIBUTES
6
- };
@@ -1,100 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/classes/MsgMessage/MsgMessage.ts
21
- var MsgMessage_exports = {};
22
- __export(MsgMessage_exports, {
23
- MsgMessage: () => MsgMessage
24
- });
25
- module.exports = __toCommonJS(MsgMessage_exports);
26
- var import_messageformat = require("messageformat");
27
-
28
- // src/classes/MsgInterface/MsgInterface.ts
29
- var DEFAULT_ATTRIBUTES = {
30
- lang: "und",
31
- dir: "auto",
32
- dnt: false
33
- };
34
-
35
- // src/classes/MsgMessage/MsgMessage.ts
36
- var MsgMessage = class _MsgMessage {
37
- _key;
38
- _value;
39
- _mf;
40
- _attributes;
41
- _notes = [];
42
- constructor(key, value, attributes, notes) {
43
- this._key = key;
44
- this._value = value;
45
- this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
46
- if (notes) {
47
- notes.forEach((note) => this.addNote(note));
48
- }
49
- }
50
- static create(data) {
51
- const { key, value, attributes, notes } = data;
52
- const message = new _MsgMessage(key, value, attributes, notes);
53
- return message;
54
- }
55
- get key() {
56
- return this._key;
57
- }
58
- get value() {
59
- return this._value;
60
- }
61
- get attributes() {
62
- return this._attributes;
63
- }
64
- get notes() {
65
- return this._notes;
66
- }
67
- addNote(note) {
68
- this.notes.push(note);
69
- }
70
- format(data, options) {
71
- if (!this._mf) {
72
- this._mf = new import_messageformat.MessageFormat(this.attributes.lang, this.value, options);
73
- }
74
- return this._mf.format(data);
75
- }
76
- formatToParts(data, options) {
77
- if (!this._mf) {
78
- this._mf = new import_messageformat.MessageFormat(this.attributes.lang, this.value, options);
79
- }
80
- return this._mf?.formatToParts(data);
81
- }
82
- getData(stripNotes = false) {
83
- return {
84
- key: this.key,
85
- value: this.value,
86
- attributes: this.attributes,
87
- notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0
88
- };
89
- }
90
- toString() {
91
- return this.value;
92
- }
93
- toJSON(stripNotes = false) {
94
- return JSON.stringify(this.getData(stripNotes), null, 2);
95
- }
96
- };
97
- // Annotate the CommonJS export names for ESM import in node:
98
- 0 && (module.exports = {
99
- MsgMessage
100
- });
@@ -1,36 +0,0 @@
1
- import * as messageformat from 'messageformat';
2
- import { MessageFormatOptions } from 'messageformat';
3
- import { MsgAttributes, MsgNote, MsgInterface } from '../MsgInterface/MsgInterface.cjs';
4
-
5
- type MsgMessageData = {
6
- key: string;
7
- value: string;
8
- attributes?: MsgAttributes;
9
- notes?: MsgNote[];
10
- };
11
- declare class MsgMessage implements MsgInterface {
12
- private _key;
13
- private _value;
14
- private _mf?;
15
- private _attributes;
16
- private _notes;
17
- private constructor();
18
- static create(data: MsgMessageData): MsgMessage;
19
- get key(): string;
20
- get value(): string;
21
- get attributes(): MsgAttributes;
22
- get notes(): MsgNote[];
23
- addNote(note: MsgNote): void;
24
- format(data: Record<string, any>, options?: MessageFormatOptions): string;
25
- formatToParts(data: Record<string, any>, options?: MessageFormatOptions): messageformat.MessagePart<never>[];
26
- getData(stripNotes?: boolean): {
27
- key: string;
28
- value: string;
29
- attributes: MsgAttributes;
30
- notes: MsgNote[] | undefined;
31
- };
32
- toString(): string;
33
- toJSON(stripNotes?: boolean): string;
34
- }
35
-
36
- export { MsgMessage, type MsgMessageData };