@worldware/msg 0.8.1 → 0.9.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/{chunk-NWJ7MVVV.mjs → chunk-DJFPVXQ5.mjs} +15 -0
- package/dist/{chunk-HCR2UNSE.mjs → chunk-F3VAAZJZ.mjs} +39 -0
- package/dist/{chunk-X6CRKSNU.mjs → chunk-IMOOTZIK.mjs} +68 -1
- package/dist/chunk-PU7ZZ5XH.mjs +13 -0
- package/dist/classes/MsgInterface/MsgInterface.d.cts +24 -1
- package/dist/classes/MsgInterface/MsgInterface.d.ts +24 -1
- package/dist/classes/MsgMessage/MsgMessage.cjs +39 -0
- package/dist/classes/MsgMessage/MsgMessage.d.cts +51 -0
- package/dist/classes/MsgMessage/MsgMessage.d.ts +51 -0
- package/dist/classes/MsgMessage/MsgMessage.mjs +1 -1
- package/dist/classes/MsgProject/MsgProject.cjs +15 -0
- package/dist/classes/MsgProject/MsgProject.d.cts +1 -1
- package/dist/classes/MsgProject/MsgProject.d.ts +1 -1
- package/dist/classes/MsgProject/MsgProject.mjs +1 -1
- package/dist/classes/MsgResource/MsgResource.cjs +106 -0
- package/dist/classes/MsgResource/MsgResource.d.cts +124 -2
- package/dist/classes/MsgResource/MsgResource.d.ts +124 -2
- package/dist/classes/MsgResource/MsgResource.mjs +2 -2
- package/dist/classes/index.cjs +121 -0
- package/dist/classes/index.d.cts +2 -2
- package/dist/classes/index.d.ts +2 -2
- package/dist/classes/index.mjs +5 -5
- package/dist/index.cjs +121 -0
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +5 -5
- package/dist/lib/index.cjs +40 -0
- package/dist/lib/index.d.cts +1 -0
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.mjs +8 -0
- package/dist/lib/lang-utils.cjs +38 -0
- package/dist/lib/lang-utils.d.cts +14 -0
- package/dist/lib/lang-utils.d.ts +14 -0
- package/dist/lib/lang-utils.mjs +8 -0
- package/package.json +1 -1
|
@@ -16,24 +16,36 @@ var defaultLocalesSettings = {
|
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
18
|
var MsgProject = class _MsgProject {
|
|
19
|
+
/** Project name, version, and default format. */
|
|
19
20
|
_project;
|
|
21
|
+
/** Source, pseudo, and target locale settings. */
|
|
20
22
|
_locales;
|
|
23
|
+
/** Function used to load translations for a locale. */
|
|
21
24
|
_loader;
|
|
25
|
+
/**
|
|
26
|
+
* Builds a project from {@link MsgProjectData}.
|
|
27
|
+
*/
|
|
22
28
|
static create(data) {
|
|
23
29
|
const { project, locales, loader } = data;
|
|
24
30
|
return new _MsgProject(project, locales, loader);
|
|
25
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates a project from its settings and loader. Prefer {@link MsgProject.create}.
|
|
34
|
+
*/
|
|
26
35
|
constructor(projectSettings, localesSettings, loader) {
|
|
27
36
|
this._project = { ...defaultProjectSettings, ...projectSettings };
|
|
28
37
|
this._locales = { ...defaultLocalesSettings, ...localesSettings };
|
|
29
38
|
this._loader = loader;
|
|
30
39
|
}
|
|
40
|
+
/** Project name, version, and default format. */
|
|
31
41
|
get project() {
|
|
32
42
|
return this._project;
|
|
33
43
|
}
|
|
44
|
+
/** Source, pseudo, and target locale settings. */
|
|
34
45
|
get locales() {
|
|
35
46
|
return this._locales;
|
|
36
47
|
}
|
|
48
|
+
/** Function used to load translations for a locale. */
|
|
37
49
|
get loader() {
|
|
38
50
|
return this._loader;
|
|
39
51
|
}
|
|
@@ -44,6 +56,9 @@ var MsgProject = class _MsgProject {
|
|
|
44
56
|
get format() {
|
|
45
57
|
return this._project.format ?? MSG_DEFAULT_FORMAT;
|
|
46
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Returns the translation load chain for a locale, or `undefined` if unsupported.
|
|
61
|
+
*/
|
|
47
62
|
getTargetLocale(locale) {
|
|
48
63
|
return this._locales.targetLocales[locale];
|
|
49
64
|
}
|
|
@@ -12,6 +12,9 @@ var MsgMessage = class _MsgMessage {
|
|
|
12
12
|
_mf;
|
|
13
13
|
_attributes;
|
|
14
14
|
_notes = [];
|
|
15
|
+
/**
|
|
16
|
+
* Creates a message from its parts. Prefer {@link MsgMessage.create}.
|
|
17
|
+
*/
|
|
15
18
|
constructor(key, value, attributes, notes) {
|
|
16
19
|
this._key = key;
|
|
17
20
|
this._value = value;
|
|
@@ -20,23 +23,33 @@ var MsgMessage = class _MsgMessage {
|
|
|
20
23
|
notes.forEach((note) => this.addNote(note));
|
|
21
24
|
}
|
|
22
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Builds a message from {@link MsgMessageData}.
|
|
28
|
+
*/
|
|
23
29
|
static create(data) {
|
|
24
30
|
const { key, value, attributes, notes } = data;
|
|
25
31
|
const message = new _MsgMessage(key, value, attributes, notes);
|
|
26
32
|
return message;
|
|
27
33
|
}
|
|
34
|
+
/** Unique key that identifies this message within a resource. */
|
|
28
35
|
get key() {
|
|
29
36
|
return this._key;
|
|
30
37
|
}
|
|
38
|
+
/** The message string, possibly in MessageFormat syntax. */
|
|
31
39
|
get value() {
|
|
32
40
|
return this._value;
|
|
33
41
|
}
|
|
42
|
+
/** Locale and formatting metadata for this message. */
|
|
34
43
|
get attributes() {
|
|
35
44
|
return this._attributes;
|
|
36
45
|
}
|
|
46
|
+
/** Notes attached to this message for translators and tooling. */
|
|
37
47
|
get notes() {
|
|
38
48
|
return this._notes;
|
|
39
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Appends a note to this message.
|
|
52
|
+
*/
|
|
40
53
|
addNote(note) {
|
|
41
54
|
this.notes.push(note);
|
|
42
55
|
}
|
|
@@ -46,24 +59,42 @@ var MsgMessage = class _MsgMessage {
|
|
|
46
59
|
resolveFormat() {
|
|
47
60
|
return this.attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
48
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Returns a cached MessageFormat formatter for this message's value.
|
|
64
|
+
*/
|
|
49
65
|
getFormatter(options) {
|
|
50
66
|
if (!this._mf) {
|
|
51
67
|
this._mf = this.resolveFormat() === "MF1" ? mf1ToMessage(this.attributes.lang, this.value, options) : new MessageFormat(this.attributes.lang, this.value, options);
|
|
52
68
|
}
|
|
53
69
|
return this._mf;
|
|
54
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Formats the message with the given placeholder data.
|
|
73
|
+
*
|
|
74
|
+
* For `NONE` format messages, returns the raw value unchanged.
|
|
75
|
+
*/
|
|
55
76
|
format(data, options) {
|
|
56
77
|
if (this.resolveFormat() === "NONE") {
|
|
57
78
|
return this.value;
|
|
58
79
|
}
|
|
59
80
|
return this.getFormatter(options).format(data);
|
|
60
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Formats the message into structured parts with the given placeholder data.
|
|
84
|
+
*
|
|
85
|
+
* For `NONE` format messages, returns a single text part with the raw value.
|
|
86
|
+
*/
|
|
61
87
|
formatToParts(data, options) {
|
|
62
88
|
if (this.resolveFormat() === "NONE") {
|
|
63
89
|
return [{ type: "text", value: this.value }];
|
|
64
90
|
}
|
|
65
91
|
return this.getFormatter(options).formatToParts(data);
|
|
66
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Returns a plain data object for this message.
|
|
95
|
+
*
|
|
96
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
97
|
+
*/
|
|
67
98
|
getData(stripNotes = false) {
|
|
68
99
|
return {
|
|
69
100
|
key: this.key,
|
|
@@ -72,9 +103,17 @@ var MsgMessage = class _MsgMessage {
|
|
|
72
103
|
notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0
|
|
73
104
|
};
|
|
74
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Returns the message value as a string.
|
|
108
|
+
*/
|
|
75
109
|
toString() {
|
|
76
110
|
return this.value;
|
|
77
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Serializes this message to a formatted JSON string.
|
|
114
|
+
*
|
|
115
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
116
|
+
*/
|
|
78
117
|
toJSON(stripNotes = false) {
|
|
79
118
|
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
80
119
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MsgMessage
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-F3VAAZJZ.mjs";
|
|
4
4
|
import {
|
|
5
5
|
DEFAULT_ATTRIBUTES,
|
|
6
6
|
MSG_DEFAULT_FORMAT
|
|
@@ -14,6 +14,12 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
14
14
|
_notes = [];
|
|
15
15
|
_title;
|
|
16
16
|
_project;
|
|
17
|
+
/**
|
|
18
|
+
* Builds a resource from {@link MsgResourceData} and an owning project.
|
|
19
|
+
*
|
|
20
|
+
* When `messages` is provided, each entry is added to the resource.
|
|
21
|
+
* When omitted, the resource starts empty.
|
|
22
|
+
*/
|
|
17
23
|
static create(data, project) {
|
|
18
24
|
const { title, attributes, notes, messages } = data;
|
|
19
25
|
const res = new _MsgResource(title, attributes, project, notes);
|
|
@@ -27,6 +33,9 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
27
33
|
}
|
|
28
34
|
return res;
|
|
29
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Creates a resource bound to a project. Prefer {@link MsgResource.create}.
|
|
38
|
+
*/
|
|
30
39
|
constructor(title, attributes, project, notes) {
|
|
31
40
|
super();
|
|
32
41
|
this._title = title;
|
|
@@ -36,14 +45,23 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
36
45
|
notes.forEach((note) => this.addNote(note));
|
|
37
46
|
}
|
|
38
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Returns true when the message's attributes match this resource's attributes.
|
|
50
|
+
*/
|
|
39
51
|
hasMatchingAttributes(message) {
|
|
40
52
|
const res = this.attributes;
|
|
41
53
|
const msg = message.attributes;
|
|
42
54
|
return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt && this.resolveFormat(res) === this.resolveFormat(msg);
|
|
43
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Resolves a format from attributes, falling back to the library default.
|
|
58
|
+
*/
|
|
44
59
|
resolveFormat(attributes) {
|
|
45
60
|
return attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
46
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Pseudo-localizes an MF2 message by localizing only literal text parts.
|
|
64
|
+
*/
|
|
47
65
|
pseudoLocalizeMF2(source, options) {
|
|
48
66
|
const msg = parseMessage(source);
|
|
49
67
|
visit(msg, {
|
|
@@ -58,30 +76,47 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
58
76
|
});
|
|
59
77
|
return stringifyMessage(msg);
|
|
60
78
|
}
|
|
79
|
+
/** Locale and formatting metadata for this resource. */
|
|
61
80
|
get attributes() {
|
|
62
81
|
return this._attributes;
|
|
63
82
|
}
|
|
83
|
+
/** Replaces the resource's locale and formatting metadata. */
|
|
64
84
|
set attributes(attributes) {
|
|
65
85
|
this._attributes = attributes;
|
|
66
86
|
}
|
|
87
|
+
/** Notes attached to this resource for translators and tooling. */
|
|
67
88
|
get notes() {
|
|
68
89
|
return this._notes;
|
|
69
90
|
}
|
|
91
|
+
/** Replaces the notes attached to this resource. */
|
|
70
92
|
set notes(notes) {
|
|
71
93
|
this._notes = notes;
|
|
72
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Appends a note to this resource.
|
|
97
|
+
*/
|
|
73
98
|
addNote(note) {
|
|
74
99
|
this.notes.push(note);
|
|
75
100
|
}
|
|
101
|
+
/** Title that identifies this resource within a project. */
|
|
76
102
|
get title() {
|
|
77
103
|
return this._title;
|
|
78
104
|
}
|
|
105
|
+
/** Sets the title that identifies this resource within a project. */
|
|
79
106
|
set title(title) {
|
|
80
107
|
this._title = title;
|
|
81
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Returns the project this resource belongs to.
|
|
111
|
+
*/
|
|
82
112
|
getProject() {
|
|
83
113
|
return this._project;
|
|
84
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Adds a message to this resource, merging resource attributes with any overrides.
|
|
117
|
+
*
|
|
118
|
+
* @returns This resource, for chaining.
|
|
119
|
+
*/
|
|
85
120
|
add(key, value, attributes, notes) {
|
|
86
121
|
const merged = { ...this.attributes, ...attributes };
|
|
87
122
|
const msg = MsgMessage.create({
|
|
@@ -93,6 +128,14 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
93
128
|
this.set(key, msg);
|
|
94
129
|
return this;
|
|
95
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Builds a translated copy of this resource from translation data.
|
|
133
|
+
*
|
|
134
|
+
* Messages present in the translation replace the source; missing keys keep
|
|
135
|
+
* the source message. Notes are carried over from the source.
|
|
136
|
+
*
|
|
137
|
+
* @throws {TypeError} When the translation title does not match this resource.
|
|
138
|
+
*/
|
|
96
139
|
translate(data) {
|
|
97
140
|
const { title, attributes, messages } = data;
|
|
98
141
|
if (title !== this.title) {
|
|
@@ -130,7 +173,18 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
130
173
|
}
|
|
131
174
|
return { ...attributes, format };
|
|
132
175
|
}
|
|
176
|
+
/**
|
|
177
|
+
* Loads and applies translations for a locale via the project loader.
|
|
178
|
+
*
|
|
179
|
+
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
180
|
+
* copy without calling the loader.
|
|
181
|
+
*
|
|
182
|
+
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
183
|
+
*/
|
|
133
184
|
async getTranslation(lang) {
|
|
185
|
+
if (!lang) {
|
|
186
|
+
return _MsgResource.create(this.getData(), this.getProject());
|
|
187
|
+
}
|
|
134
188
|
const project = this._project;
|
|
135
189
|
const pseudoLocale = project.locales.pseudoLocale;
|
|
136
190
|
if (lang === pseudoLocale) {
|
|
@@ -165,6 +219,14 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
165
219
|
}
|
|
166
220
|
return translated;
|
|
167
221
|
}
|
|
222
|
+
/**
|
|
223
|
+
* Returns a plain data object for this resource and its messages.
|
|
224
|
+
*
|
|
225
|
+
* Attributes that match the resource or project defaults are omitted where
|
|
226
|
+
* possible to keep the output compact.
|
|
227
|
+
*
|
|
228
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
229
|
+
*/
|
|
168
230
|
getData(stripNotes = false) {
|
|
169
231
|
const resourceFormat = this.resolveFormat(this.attributes);
|
|
170
232
|
const projectFormat = this._project.format;
|
|
@@ -197,6 +259,11 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
197
259
|
messages
|
|
198
260
|
};
|
|
199
261
|
}
|
|
262
|
+
/**
|
|
263
|
+
* Serializes this resource to a formatted JSON string.
|
|
264
|
+
*
|
|
265
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
266
|
+
*/
|
|
200
267
|
toJSON(stripNotes = false) {
|
|
201
268
|
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
202
269
|
}
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Categories of notes that can be attached to a message or resource.
|
|
3
|
+
*/
|
|
1
4
|
type NoteTypes = 'DESCRIPTION' | 'AUTHORSHIP' | 'PARAMETERS' | 'CONTEXT' | 'COMMENT';
|
|
5
|
+
/**
|
|
6
|
+
* A free-form note attached to a message or resource for translators and tools.
|
|
7
|
+
*/
|
|
2
8
|
type MsgNote = {
|
|
9
|
+
/** What kind of note this is. */
|
|
3
10
|
type: NoteTypes;
|
|
11
|
+
/** The note text. */
|
|
4
12
|
content: string;
|
|
5
13
|
};
|
|
6
14
|
/**
|
|
@@ -16,16 +24,31 @@ type MsgFormat = 'MF1' | 'MF2' | 'NONE';
|
|
|
16
24
|
* Kept at MF2 for backwards compatibility.
|
|
17
25
|
*/
|
|
18
26
|
declare const MSG_DEFAULT_FORMAT: MsgFormat;
|
|
27
|
+
/**
|
|
28
|
+
* Locale and formatting metadata shared by messages and resources.
|
|
29
|
+
*/
|
|
19
30
|
type MsgAttributes = {
|
|
31
|
+
/** BCP 47 language tag for the content (for example, `en` or `zh-Hans`). */
|
|
20
32
|
lang?: string;
|
|
33
|
+
/** Text direction: typically `ltr`, `rtl`, or `auto`. */
|
|
21
34
|
dir?: string;
|
|
35
|
+
/** When true, the content should not be translated (Do Not Translate). */
|
|
22
36
|
dnt?: boolean;
|
|
37
|
+
/** MessageFormat syntax used by the string, if any. */
|
|
23
38
|
format?: MsgFormat;
|
|
24
39
|
};
|
|
40
|
+
/**
|
|
41
|
+
* Default attribute values applied when none are provided.
|
|
42
|
+
*/
|
|
25
43
|
declare const DEFAULT_ATTRIBUTES: MsgAttributes;
|
|
44
|
+
/**
|
|
45
|
+
* Shared shape for objects that carry attributes and translator notes.
|
|
46
|
+
*/
|
|
26
47
|
interface MsgInterface {
|
|
48
|
+
/** Locale and formatting metadata. */
|
|
27
49
|
attributes: MsgAttributes;
|
|
50
|
+
/** Notes for translators and tooling. */
|
|
28
51
|
notes: MsgNote[];
|
|
29
52
|
}
|
|
30
53
|
|
|
31
|
-
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, type MsgAttributes, type MsgFormat, type MsgInterface, type MsgNote };
|
|
54
|
+
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, type MsgAttributes, type MsgFormat, type MsgInterface, type MsgNote, type NoteTypes };
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Categories of notes that can be attached to a message or resource.
|
|
3
|
+
*/
|
|
1
4
|
type NoteTypes = 'DESCRIPTION' | 'AUTHORSHIP' | 'PARAMETERS' | 'CONTEXT' | 'COMMENT';
|
|
5
|
+
/**
|
|
6
|
+
* A free-form note attached to a message or resource for translators and tools.
|
|
7
|
+
*/
|
|
2
8
|
type MsgNote = {
|
|
9
|
+
/** What kind of note this is. */
|
|
3
10
|
type: NoteTypes;
|
|
11
|
+
/** The note text. */
|
|
4
12
|
content: string;
|
|
5
13
|
};
|
|
6
14
|
/**
|
|
@@ -16,16 +24,31 @@ type MsgFormat = 'MF1' | 'MF2' | 'NONE';
|
|
|
16
24
|
* Kept at MF2 for backwards compatibility.
|
|
17
25
|
*/
|
|
18
26
|
declare const MSG_DEFAULT_FORMAT: MsgFormat;
|
|
27
|
+
/**
|
|
28
|
+
* Locale and formatting metadata shared by messages and resources.
|
|
29
|
+
*/
|
|
19
30
|
type MsgAttributes = {
|
|
31
|
+
/** BCP 47 language tag for the content (for example, `en` or `zh-Hans`). */
|
|
20
32
|
lang?: string;
|
|
33
|
+
/** Text direction: typically `ltr`, `rtl`, or `auto`. */
|
|
21
34
|
dir?: string;
|
|
35
|
+
/** When true, the content should not be translated (Do Not Translate). */
|
|
22
36
|
dnt?: boolean;
|
|
37
|
+
/** MessageFormat syntax used by the string, if any. */
|
|
23
38
|
format?: MsgFormat;
|
|
24
39
|
};
|
|
40
|
+
/**
|
|
41
|
+
* Default attribute values applied when none are provided.
|
|
42
|
+
*/
|
|
25
43
|
declare const DEFAULT_ATTRIBUTES: MsgAttributes;
|
|
44
|
+
/**
|
|
45
|
+
* Shared shape for objects that carry attributes and translator notes.
|
|
46
|
+
*/
|
|
26
47
|
interface MsgInterface {
|
|
48
|
+
/** Locale and formatting metadata. */
|
|
27
49
|
attributes: MsgAttributes;
|
|
50
|
+
/** Notes for translators and tooling. */
|
|
28
51
|
notes: MsgNote[];
|
|
29
52
|
}
|
|
30
53
|
|
|
31
|
-
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, type MsgAttributes, type MsgFormat, type MsgInterface, type MsgNote };
|
|
54
|
+
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, type MsgAttributes, type MsgFormat, type MsgInterface, type MsgNote, type NoteTypes };
|
|
@@ -41,6 +41,9 @@ var MsgMessage = class _MsgMessage {
|
|
|
41
41
|
_mf;
|
|
42
42
|
_attributes;
|
|
43
43
|
_notes = [];
|
|
44
|
+
/**
|
|
45
|
+
* Creates a message from its parts. Prefer {@link MsgMessage.create}.
|
|
46
|
+
*/
|
|
44
47
|
constructor(key, value, attributes, notes) {
|
|
45
48
|
this._key = key;
|
|
46
49
|
this._value = value;
|
|
@@ -49,23 +52,33 @@ var MsgMessage = class _MsgMessage {
|
|
|
49
52
|
notes.forEach((note) => this.addNote(note));
|
|
50
53
|
}
|
|
51
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Builds a message from {@link MsgMessageData}.
|
|
57
|
+
*/
|
|
52
58
|
static create(data) {
|
|
53
59
|
const { key, value, attributes, notes } = data;
|
|
54
60
|
const message = new _MsgMessage(key, value, attributes, notes);
|
|
55
61
|
return message;
|
|
56
62
|
}
|
|
63
|
+
/** Unique key that identifies this message within a resource. */
|
|
57
64
|
get key() {
|
|
58
65
|
return this._key;
|
|
59
66
|
}
|
|
67
|
+
/** The message string, possibly in MessageFormat syntax. */
|
|
60
68
|
get value() {
|
|
61
69
|
return this._value;
|
|
62
70
|
}
|
|
71
|
+
/** Locale and formatting metadata for this message. */
|
|
63
72
|
get attributes() {
|
|
64
73
|
return this._attributes;
|
|
65
74
|
}
|
|
75
|
+
/** Notes attached to this message for translators and tooling. */
|
|
66
76
|
get notes() {
|
|
67
77
|
return this._notes;
|
|
68
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Appends a note to this message.
|
|
81
|
+
*/
|
|
69
82
|
addNote(note) {
|
|
70
83
|
this.notes.push(note);
|
|
71
84
|
}
|
|
@@ -75,24 +88,42 @@ var MsgMessage = class _MsgMessage {
|
|
|
75
88
|
resolveFormat() {
|
|
76
89
|
return this.attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
77
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* Returns a cached MessageFormat formatter for this message's value.
|
|
93
|
+
*/
|
|
78
94
|
getFormatter(options) {
|
|
79
95
|
if (!this._mf) {
|
|
80
96
|
this._mf = this.resolveFormat() === "MF1" ? (0, import_icu_messageformat_1.mf1ToMessage)(this.attributes.lang, this.value, options) : new import_messageformat.MessageFormat(this.attributes.lang, this.value, options);
|
|
81
97
|
}
|
|
82
98
|
return this._mf;
|
|
83
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Formats the message with the given placeholder data.
|
|
102
|
+
*
|
|
103
|
+
* For `NONE` format messages, returns the raw value unchanged.
|
|
104
|
+
*/
|
|
84
105
|
format(data, options) {
|
|
85
106
|
if (this.resolveFormat() === "NONE") {
|
|
86
107
|
return this.value;
|
|
87
108
|
}
|
|
88
109
|
return this.getFormatter(options).format(data);
|
|
89
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* Formats the message into structured parts with the given placeholder data.
|
|
113
|
+
*
|
|
114
|
+
* For `NONE` format messages, returns a single text part with the raw value.
|
|
115
|
+
*/
|
|
90
116
|
formatToParts(data, options) {
|
|
91
117
|
if (this.resolveFormat() === "NONE") {
|
|
92
118
|
return [{ type: "text", value: this.value }];
|
|
93
119
|
}
|
|
94
120
|
return this.getFormatter(options).formatToParts(data);
|
|
95
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Returns a plain data object for this message.
|
|
124
|
+
*
|
|
125
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
126
|
+
*/
|
|
96
127
|
getData(stripNotes = false) {
|
|
97
128
|
return {
|
|
98
129
|
key: this.key,
|
|
@@ -101,9 +132,17 @@ var MsgMessage = class _MsgMessage {
|
|
|
101
132
|
notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0
|
|
102
133
|
};
|
|
103
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Returns the message value as a string.
|
|
137
|
+
*/
|
|
104
138
|
toString() {
|
|
105
139
|
return this.value;
|
|
106
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Serializes this message to a formatted JSON string.
|
|
143
|
+
*
|
|
144
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
145
|
+
*/
|
|
107
146
|
toJSON(stripNotes = false) {
|
|
108
147
|
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
109
148
|
}
|
|
@@ -2,39 +2,90 @@ import * as messageformat from 'messageformat';
|
|
|
2
2
|
import { MessageFormatOptions } from 'messageformat';
|
|
3
3
|
import { MsgInterface, MsgAttributes, MsgNote } from '../MsgInterface/MsgInterface.cjs';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Plain data used to create a {@link MsgMessage}.
|
|
7
|
+
*/
|
|
5
8
|
type MsgMessageData = {
|
|
9
|
+
/** Unique key that identifies this message within a resource. */
|
|
6
10
|
key: string;
|
|
11
|
+
/** The message string, possibly in MessageFormat syntax. */
|
|
7
12
|
value: string;
|
|
13
|
+
/** Optional locale and formatting metadata. */
|
|
8
14
|
attributes?: MsgAttributes;
|
|
15
|
+
/** Optional notes for translators and tooling. */
|
|
9
16
|
notes?: MsgNote[];
|
|
10
17
|
};
|
|
18
|
+
/**
|
|
19
|
+
* A single localizable message with a key, value, attributes, and notes.
|
|
20
|
+
*
|
|
21
|
+
* Supports MessageFormat 1, MessageFormat 2, and unformatted (`NONE`) strings.
|
|
22
|
+
*/
|
|
11
23
|
declare class MsgMessage implements MsgInterface {
|
|
12
24
|
private _key;
|
|
13
25
|
private _value;
|
|
14
26
|
private _mf?;
|
|
15
27
|
private _attributes;
|
|
16
28
|
private _notes;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a message from its parts. Prefer {@link MsgMessage.create}.
|
|
31
|
+
*/
|
|
17
32
|
private constructor();
|
|
33
|
+
/**
|
|
34
|
+
* Builds a message from {@link MsgMessageData}.
|
|
35
|
+
*/
|
|
18
36
|
static create(data: MsgMessageData): MsgMessage;
|
|
37
|
+
/** Unique key that identifies this message within a resource. */
|
|
19
38
|
get key(): string;
|
|
39
|
+
/** The message string, possibly in MessageFormat syntax. */
|
|
20
40
|
get value(): string;
|
|
41
|
+
/** Locale and formatting metadata for this message. */
|
|
21
42
|
get attributes(): MsgAttributes;
|
|
43
|
+
/** Notes attached to this message for translators and tooling. */
|
|
22
44
|
get notes(): MsgNote[];
|
|
45
|
+
/**
|
|
46
|
+
* Appends a note to this message.
|
|
47
|
+
*/
|
|
23
48
|
addNote(note: MsgNote): void;
|
|
24
49
|
/**
|
|
25
50
|
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
26
51
|
*/
|
|
27
52
|
private resolveFormat;
|
|
53
|
+
/**
|
|
54
|
+
* Returns a cached MessageFormat formatter for this message's value.
|
|
55
|
+
*/
|
|
28
56
|
private getFormatter;
|
|
57
|
+
/**
|
|
58
|
+
* Formats the message with the given placeholder data.
|
|
59
|
+
*
|
|
60
|
+
* For `NONE` format messages, returns the raw value unchanged.
|
|
61
|
+
*/
|
|
29
62
|
format(data: Record<string, any>, options?: MessageFormatOptions): string;
|
|
63
|
+
/**
|
|
64
|
+
* Formats the message into structured parts with the given placeholder data.
|
|
65
|
+
*
|
|
66
|
+
* For `NONE` format messages, returns a single text part with the raw value.
|
|
67
|
+
*/
|
|
30
68
|
formatToParts(data: Record<string, any>, options?: MessageFormatOptions): messageformat.MessagePart<never>[];
|
|
69
|
+
/**
|
|
70
|
+
* Returns a plain data object for this message.
|
|
71
|
+
*
|
|
72
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
73
|
+
*/
|
|
31
74
|
getData(stripNotes?: boolean): {
|
|
32
75
|
key: string;
|
|
33
76
|
value: string;
|
|
34
77
|
attributes: MsgAttributes;
|
|
35
78
|
notes: MsgNote[] | undefined;
|
|
36
79
|
};
|
|
80
|
+
/**
|
|
81
|
+
* Returns the message value as a string.
|
|
82
|
+
*/
|
|
37
83
|
toString(): string;
|
|
84
|
+
/**
|
|
85
|
+
* Serializes this message to a formatted JSON string.
|
|
86
|
+
*
|
|
87
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
88
|
+
*/
|
|
38
89
|
toJSON(stripNotes?: boolean): string;
|
|
39
90
|
}
|
|
40
91
|
|