@worldware/msg 0.8.1 → 0.10.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 +2 -1
- package/dist/{chunk-NWJ7MVVV.mjs → chunk-DJFPVXQ5.mjs} +15 -0
- package/dist/{chunk-X6CRKSNU.mjs → chunk-HUWFAJAN.mjs} +82 -4
- package/dist/chunk-KQTWUIAM.mjs +0 -0
- package/dist/{chunk-HCR2UNSE.mjs → chunk-OKEV7FY7.mjs} +52 -2
- 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 +52 -2
- package/dist/classes/MsgMessage/MsgMessage.d.cts +61 -2
- package/dist/classes/MsgMessage/MsgMessage.d.ts +61 -2
- 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 +133 -5
- package/dist/classes/MsgResource/MsgResource.d.cts +3 -78
- package/dist/classes/MsgResource/MsgResource.d.ts +3 -78
- package/dist/classes/MsgResource/MsgResource.mjs +2 -2
- package/dist/classes/index.cjs +148 -5
- package/dist/classes/index.d.cts +209 -3
- package/dist/classes/index.d.ts +209 -3
- package/dist/classes/index.mjs +5 -5
- package/dist/index.cjs +170 -14
- package/dist/index.d.cts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.mjs +13 -6
- 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 +9 -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
package/README.md
CHANGED
|
@@ -267,6 +267,7 @@ const data = resource.getData();
|
|
|
267
267
|
|
|
268
268
|
**Methods:**
|
|
269
269
|
- `add(key: string, value: string, attributes?: MsgAttributes, notes?: MsgNote[]): MsgResource` - Add a message
|
|
270
|
+
- `addNote(type: NoteTypes, content: string): MsgResource` - Add a note (returns `this` for chaining)
|
|
270
271
|
- `translate(data: MsgResourceData): MsgResource` - Create a translated version
|
|
271
272
|
- `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.
|
|
272
273
|
- `getProject(): MsgProject` - Returns the project instance associated with the resource
|
|
@@ -286,7 +287,7 @@ const data = resource.getData();
|
|
|
286
287
|
**Methods:**
|
|
287
288
|
- `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
289
|
- `formatToParts(data: Record<string, any>, options?: MessageFormatOptions): MessagePart[]` - Format to parts (for `NONE`, a single `{ type: 'text', value }` part)
|
|
289
|
-
- `addNote(
|
|
290
|
+
- `addNote(type: NoteTypes, content: string): MsgMessage` - Add a note (returns `this` for chaining)
|
|
290
291
|
- `getData(stripNotes?: boolean): MsgMessageData` - Get message data
|
|
291
292
|
- `toJSON(stripNotes?: boolean): string` - Serialize to JSON
|
|
292
293
|
|
|
@@ -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
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MsgMessage
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-OKEV7FY7.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,23 +33,35 @@ 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;
|
|
33
42
|
this._attributes = { ...DEFAULT_ATTRIBUTES, format: project.format, ...attributes };
|
|
34
43
|
this._project = project;
|
|
35
44
|
if (notes) {
|
|
36
|
-
notes.forEach((note) => this.
|
|
45
|
+
notes.forEach((note) => this.setNote(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,58 @@ 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
|
}
|
|
73
|
-
|
|
95
|
+
/**
|
|
96
|
+
* Appends a note to this resource.
|
|
97
|
+
*
|
|
98
|
+
* @returns This resource, for chaining.
|
|
99
|
+
*/
|
|
100
|
+
addNote(type, content) {
|
|
101
|
+
return this.setNote({ type, content });
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Appends a full note object to this resource.
|
|
105
|
+
*
|
|
106
|
+
* @returns This resource, for chaining.
|
|
107
|
+
*/
|
|
108
|
+
setNote(note) {
|
|
74
109
|
this.notes.push(note);
|
|
110
|
+
return this;
|
|
75
111
|
}
|
|
112
|
+
/** Title that identifies this resource within a project. */
|
|
76
113
|
get title() {
|
|
77
114
|
return this._title;
|
|
78
115
|
}
|
|
116
|
+
/** Sets the title that identifies this resource within a project. */
|
|
79
117
|
set title(title) {
|
|
80
118
|
this._title = title;
|
|
81
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Returns the project this resource belongs to.
|
|
122
|
+
*/
|
|
82
123
|
getProject() {
|
|
83
124
|
return this._project;
|
|
84
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Adds a message to this resource, merging resource attributes with any overrides.
|
|
128
|
+
*
|
|
129
|
+
* @returns This resource, for chaining.
|
|
130
|
+
*/
|
|
85
131
|
add(key, value, attributes, notes) {
|
|
86
132
|
const merged = { ...this.attributes, ...attributes };
|
|
87
133
|
const msg = MsgMessage.create({
|
|
@@ -93,6 +139,14 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
93
139
|
this.set(key, msg);
|
|
94
140
|
return this;
|
|
95
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Builds a translated copy of this resource from translation data.
|
|
144
|
+
*
|
|
145
|
+
* Messages present in the translation replace the source; missing keys keep
|
|
146
|
+
* the source message. Notes are carried over from the source.
|
|
147
|
+
*
|
|
148
|
+
* @throws {TypeError} When the translation title does not match this resource.
|
|
149
|
+
*/
|
|
96
150
|
translate(data) {
|
|
97
151
|
const { title, attributes, messages } = data;
|
|
98
152
|
if (title !== this.title) {
|
|
@@ -118,7 +172,7 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
118
172
|
});
|
|
119
173
|
const notes = this.get(key)?.notes || [];
|
|
120
174
|
notes.forEach((note) => {
|
|
121
|
-
msg.addNote(note);
|
|
175
|
+
msg.addNote(note.type, note.content);
|
|
122
176
|
});
|
|
123
177
|
translated.set(key, msg);
|
|
124
178
|
});
|
|
@@ -130,7 +184,18 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
130
184
|
}
|
|
131
185
|
return { ...attributes, format };
|
|
132
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* Loads and applies translations for a locale via the project loader.
|
|
189
|
+
*
|
|
190
|
+
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
191
|
+
* copy without calling the loader.
|
|
192
|
+
*
|
|
193
|
+
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
194
|
+
*/
|
|
133
195
|
async getTranslation(lang) {
|
|
196
|
+
if (!lang) {
|
|
197
|
+
return _MsgResource.create(this.getData(), this.getProject());
|
|
198
|
+
}
|
|
134
199
|
const project = this._project;
|
|
135
200
|
const pseudoLocale = project.locales.pseudoLocale;
|
|
136
201
|
if (lang === pseudoLocale) {
|
|
@@ -165,6 +230,14 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
165
230
|
}
|
|
166
231
|
return translated;
|
|
167
232
|
}
|
|
233
|
+
/**
|
|
234
|
+
* Returns a plain data object for this resource and its messages.
|
|
235
|
+
*
|
|
236
|
+
* Attributes that match the resource or project defaults are omitted where
|
|
237
|
+
* possible to keep the output compact.
|
|
238
|
+
*
|
|
239
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
240
|
+
*/
|
|
168
241
|
getData(stripNotes = false) {
|
|
169
242
|
const resourceFormat = this.resolveFormat(this.attributes);
|
|
170
243
|
const projectFormat = this._project.format;
|
|
@@ -197,6 +270,11 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
197
270
|
messages
|
|
198
271
|
};
|
|
199
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Serializes this resource to a formatted JSON string.
|
|
275
|
+
*
|
|
276
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
277
|
+
*/
|
|
200
278
|
toJSON(stripNotes = false) {
|
|
201
279
|
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
202
280
|
}
|
|
File without changes
|
|
@@ -12,33 +12,57 @@ 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;
|
|
18
21
|
this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
|
|
19
22
|
if (notes) {
|
|
20
|
-
notes.forEach((note) => this.
|
|
23
|
+
notes.forEach((note) => this.setNote(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
|
}
|
|
40
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Appends a note to this message.
|
|
52
|
+
*
|
|
53
|
+
* @returns This message, for chaining.
|
|
54
|
+
*/
|
|
55
|
+
addNote(type, content) {
|
|
56
|
+
return this.setNote({ type, content });
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Appends a full note object to this message.
|
|
60
|
+
*
|
|
61
|
+
* @returns This message, for chaining.
|
|
62
|
+
*/
|
|
63
|
+
setNote(note) {
|
|
41
64
|
this.notes.push(note);
|
|
65
|
+
return this;
|
|
42
66
|
}
|
|
43
67
|
/**
|
|
44
68
|
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
@@ -46,24 +70,42 @@ var MsgMessage = class _MsgMessage {
|
|
|
46
70
|
resolveFormat() {
|
|
47
71
|
return this.attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
48
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Returns a cached MessageFormat formatter for this message's value.
|
|
75
|
+
*/
|
|
49
76
|
getFormatter(options) {
|
|
50
77
|
if (!this._mf) {
|
|
51
78
|
this._mf = this.resolveFormat() === "MF1" ? mf1ToMessage(this.attributes.lang, this.value, options) : new MessageFormat(this.attributes.lang, this.value, options);
|
|
52
79
|
}
|
|
53
80
|
return this._mf;
|
|
54
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Formats the message with the given placeholder data.
|
|
84
|
+
*
|
|
85
|
+
* For `NONE` format messages, returns the raw value unchanged.
|
|
86
|
+
*/
|
|
55
87
|
format(data, options) {
|
|
56
88
|
if (this.resolveFormat() === "NONE") {
|
|
57
89
|
return this.value;
|
|
58
90
|
}
|
|
59
91
|
return this.getFormatter(options).format(data);
|
|
60
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Formats the message into structured parts with the given placeholder data.
|
|
95
|
+
*
|
|
96
|
+
* For `NONE` format messages, returns a single text part with the raw value.
|
|
97
|
+
*/
|
|
61
98
|
formatToParts(data, options) {
|
|
62
99
|
if (this.resolveFormat() === "NONE") {
|
|
63
100
|
return [{ type: "text", value: this.value }];
|
|
64
101
|
}
|
|
65
102
|
return this.getFormatter(options).formatToParts(data);
|
|
66
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Returns a plain data object for this message.
|
|
106
|
+
*
|
|
107
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
108
|
+
*/
|
|
67
109
|
getData(stripNotes = false) {
|
|
68
110
|
return {
|
|
69
111
|
key: this.key,
|
|
@@ -72,9 +114,17 @@ var MsgMessage = class _MsgMessage {
|
|
|
72
114
|
notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0
|
|
73
115
|
};
|
|
74
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Returns the message value as a string.
|
|
119
|
+
*/
|
|
75
120
|
toString() {
|
|
76
121
|
return this.value;
|
|
77
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Serializes this message to a formatted JSON string.
|
|
125
|
+
*
|
|
126
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
127
|
+
*/
|
|
78
128
|
toJSON(stripNotes = false) {
|
|
79
129
|
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
80
130
|
}
|
|
@@ -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,33 +41,57 @@ 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;
|
|
47
50
|
this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
|
|
48
51
|
if (notes) {
|
|
49
|
-
notes.forEach((note) => this.
|
|
52
|
+
notes.forEach((note) => this.setNote(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
|
}
|
|
69
|
-
|
|
79
|
+
/**
|
|
80
|
+
* Appends a note to this message.
|
|
81
|
+
*
|
|
82
|
+
* @returns This message, for chaining.
|
|
83
|
+
*/
|
|
84
|
+
addNote(type, content) {
|
|
85
|
+
return this.setNote({ type, content });
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Appends a full note object to this message.
|
|
89
|
+
*
|
|
90
|
+
* @returns This message, for chaining.
|
|
91
|
+
*/
|
|
92
|
+
setNote(note) {
|
|
70
93
|
this.notes.push(note);
|
|
94
|
+
return this;
|
|
71
95
|
}
|
|
72
96
|
/**
|
|
73
97
|
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
@@ -75,24 +99,42 @@ var MsgMessage = class _MsgMessage {
|
|
|
75
99
|
resolveFormat() {
|
|
76
100
|
return this.attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
77
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* Returns a cached MessageFormat formatter for this message's value.
|
|
104
|
+
*/
|
|
78
105
|
getFormatter(options) {
|
|
79
106
|
if (!this._mf) {
|
|
80
107
|
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
108
|
}
|
|
82
109
|
return this._mf;
|
|
83
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* Formats the message with the given placeholder data.
|
|
113
|
+
*
|
|
114
|
+
* For `NONE` format messages, returns the raw value unchanged.
|
|
115
|
+
*/
|
|
84
116
|
format(data, options) {
|
|
85
117
|
if (this.resolveFormat() === "NONE") {
|
|
86
118
|
return this.value;
|
|
87
119
|
}
|
|
88
120
|
return this.getFormatter(options).format(data);
|
|
89
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Formats the message into structured parts with the given placeholder data.
|
|
124
|
+
*
|
|
125
|
+
* For `NONE` format messages, returns a single text part with the raw value.
|
|
126
|
+
*/
|
|
90
127
|
formatToParts(data, options) {
|
|
91
128
|
if (this.resolveFormat() === "NONE") {
|
|
92
129
|
return [{ type: "text", value: this.value }];
|
|
93
130
|
}
|
|
94
131
|
return this.getFormatter(options).formatToParts(data);
|
|
95
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* Returns a plain data object for this message.
|
|
135
|
+
*
|
|
136
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
137
|
+
*/
|
|
96
138
|
getData(stripNotes = false) {
|
|
97
139
|
return {
|
|
98
140
|
key: this.key,
|
|
@@ -101,9 +143,17 @@ var MsgMessage = class _MsgMessage {
|
|
|
101
143
|
notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0
|
|
102
144
|
};
|
|
103
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* Returns the message value as a string.
|
|
148
|
+
*/
|
|
104
149
|
toString() {
|
|
105
150
|
return this.value;
|
|
106
151
|
}
|
|
152
|
+
/**
|
|
153
|
+
* Serializes this message to a formatted JSON string.
|
|
154
|
+
*
|
|
155
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
156
|
+
*/
|
|
107
157
|
toJSON(stripNotes = false) {
|
|
108
158
|
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
109
159
|
}
|