@worldware/msg 0.8.0 → 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-DJFPVXQ5.mjs +69 -0
- package/dist/chunk-F3VAAZJZ.mjs +124 -0
- package/dist/chunk-IMOOTZIK.mjs +274 -0
- package/dist/chunk-ORHP7HGX.mjs +0 -0
- package/dist/chunk-PU7ZZ5XH.mjs +13 -0
- package/dist/chunk-XQVALCKX.mjs +12 -0
- package/dist/classes/MsgInterface/MsgInterface.cjs +37 -0
- package/dist/classes/MsgInterface/MsgInterface.d.cts +54 -0
- package/dist/classes/MsgInterface/MsgInterface.d.ts +54 -0
- package/dist/classes/MsgInterface/MsgInterface.mjs +8 -0
- package/dist/classes/MsgMessage/MsgMessage.cjs +153 -0
- package/dist/classes/MsgMessage/MsgMessage.d.cts +92 -0
- package/dist/classes/MsgMessage/MsgMessage.d.ts +92 -0
- package/dist/classes/MsgMessage/MsgMessage.mjs +7 -0
- package/dist/classes/MsgProject/MsgProject.cjs +94 -0
- package/dist/classes/MsgProject/MsgProject.d.cts +4 -0
- package/dist/classes/MsgProject/MsgProject.d.ts +4 -0
- package/dist/classes/MsgProject/MsgProject.mjs +7 -0
- package/dist/classes/MsgResource/MsgResource.cjs +418 -0
- package/dist/classes/MsgResource/MsgResource.d.cts +201 -0
- package/dist/classes/MsgResource/MsgResource.d.ts +201 -0
- package/dist/classes/MsgResource/MsgResource.mjs +8 -0
- package/dist/classes/index.cjs +486 -0
- package/dist/classes/index.d.cts +4 -0
- package/dist/classes/index.d.ts +4 -0
- package/dist/classes/index.mjs +21 -0
- package/dist/index.cjs +486 -0
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.mjs +21 -0
- 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 +2 -1
|
@@ -0,0 +1,418 @@
|
|
|
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/MsgResource/MsgResource.ts
|
|
21
|
+
var MsgResource_exports = {};
|
|
22
|
+
__export(MsgResource_exports, {
|
|
23
|
+
MsgResource: () => MsgResource
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(MsgResource_exports);
|
|
26
|
+
var import_messageformat2 = require("messageformat");
|
|
27
|
+
var import_pseudo_localization = require("pseudo-localization");
|
|
28
|
+
|
|
29
|
+
// src/classes/MsgMessage/MsgMessage.ts
|
|
30
|
+
var import_messageformat = require("messageformat");
|
|
31
|
+
var import_icu_messageformat_1 = require("@messageformat/icu-messageformat-1");
|
|
32
|
+
|
|
33
|
+
// src/classes/MsgInterface/MsgInterface.ts
|
|
34
|
+
var MSG_DEFAULT_FORMAT = "MF2";
|
|
35
|
+
var DEFAULT_ATTRIBUTES = {
|
|
36
|
+
lang: "und",
|
|
37
|
+
dir: "auto",
|
|
38
|
+
dnt: false
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/classes/MsgMessage/MsgMessage.ts
|
|
42
|
+
var MsgMessage = class _MsgMessage {
|
|
43
|
+
_key;
|
|
44
|
+
_value;
|
|
45
|
+
_mf;
|
|
46
|
+
_attributes;
|
|
47
|
+
_notes = [];
|
|
48
|
+
/**
|
|
49
|
+
* Creates a message from its parts. Prefer {@link MsgMessage.create}.
|
|
50
|
+
*/
|
|
51
|
+
constructor(key, value, attributes, notes) {
|
|
52
|
+
this._key = key;
|
|
53
|
+
this._value = value;
|
|
54
|
+
this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
|
|
55
|
+
if (notes) {
|
|
56
|
+
notes.forEach((note) => this.addNote(note));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Builds a message from {@link MsgMessageData}.
|
|
61
|
+
*/
|
|
62
|
+
static create(data) {
|
|
63
|
+
const { key, value, attributes, notes } = data;
|
|
64
|
+
const message = new _MsgMessage(key, value, attributes, notes);
|
|
65
|
+
return message;
|
|
66
|
+
}
|
|
67
|
+
/** Unique key that identifies this message within a resource. */
|
|
68
|
+
get key() {
|
|
69
|
+
return this._key;
|
|
70
|
+
}
|
|
71
|
+
/** The message string, possibly in MessageFormat syntax. */
|
|
72
|
+
get value() {
|
|
73
|
+
return this._value;
|
|
74
|
+
}
|
|
75
|
+
/** Locale and formatting metadata for this message. */
|
|
76
|
+
get attributes() {
|
|
77
|
+
return this._attributes;
|
|
78
|
+
}
|
|
79
|
+
/** Notes attached to this message for translators and tooling. */
|
|
80
|
+
get notes() {
|
|
81
|
+
return this._notes;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Appends a note to this message.
|
|
85
|
+
*/
|
|
86
|
+
addNote(note) {
|
|
87
|
+
this.notes.push(note);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
91
|
+
*/
|
|
92
|
+
resolveFormat() {
|
|
93
|
+
return this.attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Returns a cached MessageFormat formatter for this message's value.
|
|
97
|
+
*/
|
|
98
|
+
getFormatter(options) {
|
|
99
|
+
if (!this._mf) {
|
|
100
|
+
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);
|
|
101
|
+
}
|
|
102
|
+
return this._mf;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Formats the message with the given placeholder data.
|
|
106
|
+
*
|
|
107
|
+
* For `NONE` format messages, returns the raw value unchanged.
|
|
108
|
+
*/
|
|
109
|
+
format(data, options) {
|
|
110
|
+
if (this.resolveFormat() === "NONE") {
|
|
111
|
+
return this.value;
|
|
112
|
+
}
|
|
113
|
+
return this.getFormatter(options).format(data);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Formats the message into structured parts with the given placeholder data.
|
|
117
|
+
*
|
|
118
|
+
* For `NONE` format messages, returns a single text part with the raw value.
|
|
119
|
+
*/
|
|
120
|
+
formatToParts(data, options) {
|
|
121
|
+
if (this.resolveFormat() === "NONE") {
|
|
122
|
+
return [{ type: "text", value: this.value }];
|
|
123
|
+
}
|
|
124
|
+
return this.getFormatter(options).formatToParts(data);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Returns a plain data object for this message.
|
|
128
|
+
*
|
|
129
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
130
|
+
*/
|
|
131
|
+
getData(stripNotes = false) {
|
|
132
|
+
return {
|
|
133
|
+
key: this.key,
|
|
134
|
+
value: this.value,
|
|
135
|
+
attributes: this.attributes,
|
|
136
|
+
notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Returns the message value as a string.
|
|
141
|
+
*/
|
|
142
|
+
toString() {
|
|
143
|
+
return this.value;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Serializes this message to a formatted JSON string.
|
|
147
|
+
*
|
|
148
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
149
|
+
*/
|
|
150
|
+
toJSON(stripNotes = false) {
|
|
151
|
+
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// src/classes/MsgResource/MsgResource.ts
|
|
156
|
+
var MsgResource = class _MsgResource extends Map {
|
|
157
|
+
_attributes = {};
|
|
158
|
+
_notes = [];
|
|
159
|
+
_title;
|
|
160
|
+
_project;
|
|
161
|
+
/**
|
|
162
|
+
* Builds a resource from {@link MsgResourceData} and an owning project.
|
|
163
|
+
*
|
|
164
|
+
* When `messages` is provided, each entry is added to the resource.
|
|
165
|
+
* When omitted, the resource starts empty.
|
|
166
|
+
*/
|
|
167
|
+
static create(data, project) {
|
|
168
|
+
const { title, attributes, notes, messages } = data;
|
|
169
|
+
const res = new _MsgResource(title, attributes, project, notes);
|
|
170
|
+
if (messages) {
|
|
171
|
+
messages.forEach((messageData) => {
|
|
172
|
+
const { key, value, attributes: attributes2, notes: notes2 } = messageData;
|
|
173
|
+
res.add(key, value, attributes2, notes2);
|
|
174
|
+
});
|
|
175
|
+
} else {
|
|
176
|
+
res.clear();
|
|
177
|
+
}
|
|
178
|
+
return res;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Creates a resource bound to a project. Prefer {@link MsgResource.create}.
|
|
182
|
+
*/
|
|
183
|
+
constructor(title, attributes, project, notes) {
|
|
184
|
+
super();
|
|
185
|
+
this._title = title;
|
|
186
|
+
this._attributes = { ...DEFAULT_ATTRIBUTES, format: project.format, ...attributes };
|
|
187
|
+
this._project = project;
|
|
188
|
+
if (notes) {
|
|
189
|
+
notes.forEach((note) => this.addNote(note));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Returns true when the message's attributes match this resource's attributes.
|
|
194
|
+
*/
|
|
195
|
+
hasMatchingAttributes(message) {
|
|
196
|
+
const res = this.attributes;
|
|
197
|
+
const msg = message.attributes;
|
|
198
|
+
return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt && this.resolveFormat(res) === this.resolveFormat(msg);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Resolves a format from attributes, falling back to the library default.
|
|
202
|
+
*/
|
|
203
|
+
resolveFormat(attributes) {
|
|
204
|
+
return attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Pseudo-localizes an MF2 message by localizing only literal text parts.
|
|
208
|
+
*/
|
|
209
|
+
pseudoLocalizeMF2(source, options) {
|
|
210
|
+
const msg = (0, import_messageformat2.parseMessage)(source);
|
|
211
|
+
(0, import_messageformat2.visit)(msg, {
|
|
212
|
+
pattern: (pattern) => {
|
|
213
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
214
|
+
const part = pattern[i];
|
|
215
|
+
if (typeof part === "string") {
|
|
216
|
+
pattern[i] = (0, import_pseudo_localization.localize)(part, options);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
return (0, import_messageformat2.stringifyMessage)(msg);
|
|
222
|
+
}
|
|
223
|
+
/** Locale and formatting metadata for this resource. */
|
|
224
|
+
get attributes() {
|
|
225
|
+
return this._attributes;
|
|
226
|
+
}
|
|
227
|
+
/** Replaces the resource's locale and formatting metadata. */
|
|
228
|
+
set attributes(attributes) {
|
|
229
|
+
this._attributes = attributes;
|
|
230
|
+
}
|
|
231
|
+
/** Notes attached to this resource for translators and tooling. */
|
|
232
|
+
get notes() {
|
|
233
|
+
return this._notes;
|
|
234
|
+
}
|
|
235
|
+
/** Replaces the notes attached to this resource. */
|
|
236
|
+
set notes(notes) {
|
|
237
|
+
this._notes = notes;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Appends a note to this resource.
|
|
241
|
+
*/
|
|
242
|
+
addNote(note) {
|
|
243
|
+
this.notes.push(note);
|
|
244
|
+
}
|
|
245
|
+
/** Title that identifies this resource within a project. */
|
|
246
|
+
get title() {
|
|
247
|
+
return this._title;
|
|
248
|
+
}
|
|
249
|
+
/** Sets the title that identifies this resource within a project. */
|
|
250
|
+
set title(title) {
|
|
251
|
+
this._title = title;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Returns the project this resource belongs to.
|
|
255
|
+
*/
|
|
256
|
+
getProject() {
|
|
257
|
+
return this._project;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Adds a message to this resource, merging resource attributes with any overrides.
|
|
261
|
+
*
|
|
262
|
+
* @returns This resource, for chaining.
|
|
263
|
+
*/
|
|
264
|
+
add(key, value, attributes, notes) {
|
|
265
|
+
const merged = { ...this.attributes, ...attributes };
|
|
266
|
+
const msg = MsgMessage.create({
|
|
267
|
+
key,
|
|
268
|
+
value,
|
|
269
|
+
attributes: merged,
|
|
270
|
+
notes
|
|
271
|
+
});
|
|
272
|
+
this.set(key, msg);
|
|
273
|
+
return this;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Builds a translated copy of this resource from translation data.
|
|
277
|
+
*
|
|
278
|
+
* Messages present in the translation replace the source; missing keys keep
|
|
279
|
+
* the source message. Notes are carried over from the source.
|
|
280
|
+
*
|
|
281
|
+
* @throws {TypeError} When the translation title does not match this resource.
|
|
282
|
+
*/
|
|
283
|
+
translate(data) {
|
|
284
|
+
const { title, attributes, messages } = data;
|
|
285
|
+
if (title !== this.title) {
|
|
286
|
+
throw new TypeError("Title of resource and translations do not match.");
|
|
287
|
+
}
|
|
288
|
+
const translated = _MsgResource.create({
|
|
289
|
+
title,
|
|
290
|
+
// preserve the source resource's format unless the translation overrides it
|
|
291
|
+
attributes: this.preserveFormat(attributes, this.attributes.format),
|
|
292
|
+
notes: this.notes
|
|
293
|
+
// transfer the notes
|
|
294
|
+
}, this._project);
|
|
295
|
+
this.forEach((msg) => {
|
|
296
|
+
translated.set(msg.key, msg);
|
|
297
|
+
});
|
|
298
|
+
messages?.forEach((messageData) => {
|
|
299
|
+
const { key, value, attributes: attributes2 } = messageData;
|
|
300
|
+
const msg = MsgMessage.create({
|
|
301
|
+
key,
|
|
302
|
+
value,
|
|
303
|
+
// preserve the source message's format unless the translation overrides it
|
|
304
|
+
attributes: this.preserveFormat(attributes2, this.get(key)?.attributes.format)
|
|
305
|
+
});
|
|
306
|
+
const notes = this.get(key)?.notes || [];
|
|
307
|
+
notes.forEach((note) => {
|
|
308
|
+
msg.addNote(note);
|
|
309
|
+
});
|
|
310
|
+
translated.set(key, msg);
|
|
311
|
+
});
|
|
312
|
+
return translated;
|
|
313
|
+
}
|
|
314
|
+
preserveFormat(attributes, format) {
|
|
315
|
+
if (format === void 0 || attributes?.format !== void 0) {
|
|
316
|
+
return attributes;
|
|
317
|
+
}
|
|
318
|
+
return { ...attributes, format };
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Loads and applies translations for a locale via the project loader.
|
|
322
|
+
*
|
|
323
|
+
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
324
|
+
* copy without calling the loader.
|
|
325
|
+
*
|
|
326
|
+
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
327
|
+
*/
|
|
328
|
+
async getTranslation(lang) {
|
|
329
|
+
if (!lang) {
|
|
330
|
+
return _MsgResource.create(this.getData(), this.getProject());
|
|
331
|
+
}
|
|
332
|
+
const project = this._project;
|
|
333
|
+
const pseudoLocale = project.locales.pseudoLocale;
|
|
334
|
+
if (lang === pseudoLocale) {
|
|
335
|
+
const pseudolocalizedData = {
|
|
336
|
+
title: this.title,
|
|
337
|
+
attributes: { ...this.attributes, lang: pseudoLocale },
|
|
338
|
+
notes: this.notes.length > 0 ? this.notes : void 0,
|
|
339
|
+
messages: []
|
|
340
|
+
};
|
|
341
|
+
this.forEach((msg) => {
|
|
342
|
+
pseudolocalizedData.messages.push({
|
|
343
|
+
key: msg.key,
|
|
344
|
+
value: this.pseudoLocalizeMF2(msg.value),
|
|
345
|
+
attributes: { ...this.attributes, lang: pseudoLocale }
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
return this.translate(pseudolocalizedData);
|
|
349
|
+
}
|
|
350
|
+
const languageChain = project.getTargetLocale(lang);
|
|
351
|
+
if (!languageChain) {
|
|
352
|
+
throw new Error("Unsupported locale for resource.");
|
|
353
|
+
}
|
|
354
|
+
if (languageChain.length == 0) {
|
|
355
|
+
throw new Error(`Empty language chain for locale: ${lang}`);
|
|
356
|
+
}
|
|
357
|
+
let translated = this;
|
|
358
|
+
for (let i = 0; i < languageChain.length; i++) {
|
|
359
|
+
const lang2 = languageChain[i];
|
|
360
|
+
if (lang2 && project._locales.targetLocales[lang2]) {
|
|
361
|
+
translated = await translated._project._loader(project.project.name, translated.title, lang2).then((data) => translated.translate(data));
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return translated;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Returns a plain data object for this resource and its messages.
|
|
368
|
+
*
|
|
369
|
+
* Attributes that match the resource or project defaults are omitted where
|
|
370
|
+
* possible to keep the output compact.
|
|
371
|
+
*
|
|
372
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
373
|
+
*/
|
|
374
|
+
getData(stripNotes = false) {
|
|
375
|
+
const resourceFormat = this.resolveFormat(this.attributes);
|
|
376
|
+
const projectFormat = this._project.format;
|
|
377
|
+
const messages = [];
|
|
378
|
+
this.forEach((msg) => {
|
|
379
|
+
if (this.hasMatchingAttributes(msg)) {
|
|
380
|
+
const data = {
|
|
381
|
+
key: msg.key,
|
|
382
|
+
value: msg.value,
|
|
383
|
+
notes: !stripNotes && msg.notes.length > 0 ? msg.notes : void 0
|
|
384
|
+
};
|
|
385
|
+
messages.push(data);
|
|
386
|
+
} else {
|
|
387
|
+
const data = msg.getData(stripNotes);
|
|
388
|
+
if (data.attributes && this.resolveFormat(data.attributes) === resourceFormat) {
|
|
389
|
+
const { format, ...rest } = data.attributes;
|
|
390
|
+
data.attributes = rest;
|
|
391
|
+
}
|
|
392
|
+
messages.push(data);
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
const attributes = { ...this.attributes };
|
|
396
|
+
if (this.resolveFormat(attributes) === projectFormat) {
|
|
397
|
+
delete attributes.format;
|
|
398
|
+
}
|
|
399
|
+
return {
|
|
400
|
+
title: this.title,
|
|
401
|
+
attributes,
|
|
402
|
+
notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0,
|
|
403
|
+
messages
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Serializes this resource to a formatted JSON string.
|
|
408
|
+
*
|
|
409
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
410
|
+
*/
|
|
411
|
+
toJSON(stripNotes = false) {
|
|
412
|
+
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
416
|
+
0 && (module.exports = {
|
|
417
|
+
MsgResource
|
|
418
|
+
});
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { MsgAttributes, MsgNote, MsgInterface, MsgFormat } from '../MsgInterface/MsgInterface.cjs';
|
|
2
|
+
import { MsgMessageData, MsgMessage } from '../MsgMessage/MsgMessage.cjs';
|
|
3
|
+
import 'messageformat';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Plain data used to create or describe a {@link MsgResource}.
|
|
7
|
+
*/
|
|
8
|
+
type MsgResourceData = {
|
|
9
|
+
/** Title that identifies this resource within a project. */
|
|
10
|
+
title: string;
|
|
11
|
+
/** Locale and formatting metadata for the resource. */
|
|
12
|
+
attributes: MsgAttributes;
|
|
13
|
+
/** Optional notes for translators and tooling. */
|
|
14
|
+
notes?: MsgNote[];
|
|
15
|
+
/** Optional list of messages in this resource. */
|
|
16
|
+
messages?: MsgMessageData[];
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* A named collection of messages belonging to a {@link MsgProject}.
|
|
20
|
+
*
|
|
21
|
+
* Resources inherit format defaults from their project, support translation
|
|
22
|
+
* loading (including pseudo-localization), and can serialize to plain data.
|
|
23
|
+
*/
|
|
24
|
+
declare class MsgResource extends Map<string, MsgMessage> implements MsgInterface {
|
|
25
|
+
private _attributes;
|
|
26
|
+
private _notes;
|
|
27
|
+
private _title;
|
|
28
|
+
private _project;
|
|
29
|
+
/**
|
|
30
|
+
* Builds a resource from {@link MsgResourceData} and an owning project.
|
|
31
|
+
*
|
|
32
|
+
* When `messages` is provided, each entry is added to the resource.
|
|
33
|
+
* When omitted, the resource starts empty.
|
|
34
|
+
*/
|
|
35
|
+
static create(data: MsgResourceData, project: MsgProject): MsgResource;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a resource bound to a project. Prefer {@link MsgResource.create}.
|
|
38
|
+
*/
|
|
39
|
+
private constructor();
|
|
40
|
+
/**
|
|
41
|
+
* Returns true when the message's attributes match this resource's attributes.
|
|
42
|
+
*/
|
|
43
|
+
private hasMatchingAttributes;
|
|
44
|
+
/**
|
|
45
|
+
* Resolves a format from attributes, falling back to the library default.
|
|
46
|
+
*/
|
|
47
|
+
private resolveFormat;
|
|
48
|
+
/**
|
|
49
|
+
* Pseudo-localizes an MF2 message by localizing only literal text parts.
|
|
50
|
+
*/
|
|
51
|
+
private pseudoLocalizeMF2;
|
|
52
|
+
/** Locale and formatting metadata for this resource. */
|
|
53
|
+
get attributes(): MsgAttributes;
|
|
54
|
+
/** Replaces the resource's locale and formatting metadata. */
|
|
55
|
+
set attributes(attributes: MsgAttributes);
|
|
56
|
+
/** Notes attached to this resource for translators and tooling. */
|
|
57
|
+
get notes(): MsgNote[];
|
|
58
|
+
/** Replaces the notes attached to this resource. */
|
|
59
|
+
set notes(notes: MsgNote[]);
|
|
60
|
+
/**
|
|
61
|
+
* Appends a note to this resource.
|
|
62
|
+
*/
|
|
63
|
+
addNote(note: MsgNote): void;
|
|
64
|
+
/** Title that identifies this resource within a project. */
|
|
65
|
+
get title(): string;
|
|
66
|
+
/** Sets the title that identifies this resource within a project. */
|
|
67
|
+
set title(title: string);
|
|
68
|
+
/**
|
|
69
|
+
* Returns the project this resource belongs to.
|
|
70
|
+
*/
|
|
71
|
+
getProject(): MsgProject;
|
|
72
|
+
/**
|
|
73
|
+
* Adds a message to this resource, merging resource attributes with any overrides.
|
|
74
|
+
*
|
|
75
|
+
* @returns This resource, for chaining.
|
|
76
|
+
*/
|
|
77
|
+
add(key: string, value: string, attributes?: MsgAttributes, notes?: MsgNote[]): this;
|
|
78
|
+
/**
|
|
79
|
+
* Builds a translated copy of this resource from translation data.
|
|
80
|
+
*
|
|
81
|
+
* Messages present in the translation replace the source; missing keys keep
|
|
82
|
+
* the source message. Notes are carried over from the source.
|
|
83
|
+
*
|
|
84
|
+
* @throws {TypeError} When the translation title does not match this resource.
|
|
85
|
+
*/
|
|
86
|
+
translate(data: MsgResourceData): MsgResource;
|
|
87
|
+
/**
|
|
88
|
+
* Merge a `format` into an attribute set unless one is already present,
|
|
89
|
+
* so translations inherit the source's format without overriding an
|
|
90
|
+
* explicitly translated one. Returns the input unchanged when there is
|
|
91
|
+
* no format to preserve.
|
|
92
|
+
*/
|
|
93
|
+
private preserveFormat;
|
|
94
|
+
/**
|
|
95
|
+
* Loads and applies translations for a locale via the project loader.
|
|
96
|
+
*
|
|
97
|
+
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
98
|
+
* copy without calling the loader.
|
|
99
|
+
*
|
|
100
|
+
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
101
|
+
*/
|
|
102
|
+
getTranslation(lang?: string): Promise<MsgResource>;
|
|
103
|
+
/**
|
|
104
|
+
* Returns a plain data object for this resource and its messages.
|
|
105
|
+
*
|
|
106
|
+
* Attributes that match the resource or project defaults are omitted where
|
|
107
|
+
* possible to keep the output compact.
|
|
108
|
+
*
|
|
109
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
110
|
+
*/
|
|
111
|
+
getData(stripNotes?: boolean): MsgResourceData;
|
|
112
|
+
/**
|
|
113
|
+
* Serializes this resource to a formatted JSON string.
|
|
114
|
+
*
|
|
115
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
116
|
+
*/
|
|
117
|
+
toJSON(stripNotes?: boolean): string;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Project-level settings such as name, version, and default message format.
|
|
122
|
+
*/
|
|
123
|
+
type MsgProjectSettings = {
|
|
124
|
+
/** Display name of the localization project. */
|
|
125
|
+
name: string;
|
|
126
|
+
/** Optional project version number. */
|
|
127
|
+
version?: number;
|
|
128
|
+
/** Default message format inherited by resources and messages. */
|
|
129
|
+
format?: MsgFormat;
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Map of a requested locale to the chain of locales used to load translations.
|
|
133
|
+
*/
|
|
134
|
+
type MsgTargetLocalesSettings = {
|
|
135
|
+
[key: string]: string[];
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Locale configuration for a project: source, pseudo, and target locales.
|
|
139
|
+
*/
|
|
140
|
+
type MsgLocalesSettings = {
|
|
141
|
+
/** Locale of the source (authoring) strings. */
|
|
142
|
+
sourceLocale: string;
|
|
143
|
+
/** Locale used for pseudo-localization during development. */
|
|
144
|
+
pseudoLocale: string;
|
|
145
|
+
/** Target locales and their fallback load chains. */
|
|
146
|
+
targetLocales: MsgTargetLocalesSettings;
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* Async function that loads translated resource data for a project, title, and language.
|
|
150
|
+
*/
|
|
151
|
+
type MsgTranslationLoader = (project: string, title: string, lang: string) => Promise<MsgResourceData>;
|
|
152
|
+
/**
|
|
153
|
+
* Plain data used to create a {@link MsgProject}.
|
|
154
|
+
*/
|
|
155
|
+
type MsgProjectData = {
|
|
156
|
+
/** Project name, version, and default format. */
|
|
157
|
+
project: MsgProjectSettings;
|
|
158
|
+
/** Source, pseudo, and target locale settings. */
|
|
159
|
+
locales: MsgLocalesSettings;
|
|
160
|
+
/** Function used to load translations for a locale. */
|
|
161
|
+
loader: MsgTranslationLoader;
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* A localization project that holds settings, locales, and a translation loader.
|
|
165
|
+
*
|
|
166
|
+
* Resources are created against a project so they can inherit format defaults
|
|
167
|
+
* and load translations through the project's loader.
|
|
168
|
+
*/
|
|
169
|
+
declare class MsgProject {
|
|
170
|
+
/** Project name, version, and default format. */
|
|
171
|
+
_project: MsgProjectSettings;
|
|
172
|
+
/** Source, pseudo, and target locale settings. */
|
|
173
|
+
_locales: MsgLocalesSettings;
|
|
174
|
+
/** Function used to load translations for a locale. */
|
|
175
|
+
_loader: MsgTranslationLoader;
|
|
176
|
+
/**
|
|
177
|
+
* Builds a project from {@link MsgProjectData}.
|
|
178
|
+
*/
|
|
179
|
+
static create(data: MsgProjectData): MsgProject;
|
|
180
|
+
/**
|
|
181
|
+
* Creates a project from its settings and loader. Prefer {@link MsgProject.create}.
|
|
182
|
+
*/
|
|
183
|
+
private constructor();
|
|
184
|
+
/** Project name, version, and default format. */
|
|
185
|
+
get project(): MsgProjectSettings;
|
|
186
|
+
/** Source, pseudo, and target locale settings. */
|
|
187
|
+
get locales(): MsgLocalesSettings;
|
|
188
|
+
/** Function used to load translations for a locale. */
|
|
189
|
+
get loader(): MsgTranslationLoader;
|
|
190
|
+
/**
|
|
191
|
+
* The project-wide default message format. Resources (and, through them,
|
|
192
|
+
* messages) inherit this value unless they specify their own.
|
|
193
|
+
*/
|
|
194
|
+
get format(): MsgFormat;
|
|
195
|
+
/**
|
|
196
|
+
* Returns the translation load chain for a locale, or `undefined` if unsupported.
|
|
197
|
+
*/
|
|
198
|
+
getTargetLocale(locale: string): string[] | undefined;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export { type MsgLocalesSettings as M, MsgResource, type MsgResourceData, MsgProject as a, type MsgProjectData as b, type MsgProjectSettings as c, type MsgTargetLocalesSettings as d, type MsgTranslationLoader as e };
|