@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,21 @@
|
|
|
1
|
+
import "../chunk-ORHP7HGX.mjs";
|
|
2
|
+
import {
|
|
3
|
+
MsgProject
|
|
4
|
+
} from "../chunk-DJFPVXQ5.mjs";
|
|
5
|
+
import {
|
|
6
|
+
MsgResource
|
|
7
|
+
} from "../chunk-IMOOTZIK.mjs";
|
|
8
|
+
import {
|
|
9
|
+
MsgMessage
|
|
10
|
+
} from "../chunk-F3VAAZJZ.mjs";
|
|
11
|
+
import {
|
|
12
|
+
DEFAULT_ATTRIBUTES,
|
|
13
|
+
MSG_DEFAULT_FORMAT
|
|
14
|
+
} from "../chunk-XQVALCKX.mjs";
|
|
15
|
+
export {
|
|
16
|
+
DEFAULT_ATTRIBUTES,
|
|
17
|
+
MSG_DEFAULT_FORMAT,
|
|
18
|
+
MsgMessage,
|
|
19
|
+
MsgProject,
|
|
20
|
+
MsgResource
|
|
21
|
+
};
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,486 @@
|
|
|
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/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
DEFAULT_ATTRIBUTES: () => DEFAULT_ATTRIBUTES,
|
|
24
|
+
MSG_DEFAULT_FORMAT: () => MSG_DEFAULT_FORMAT,
|
|
25
|
+
MsgMessage: () => MsgMessage,
|
|
26
|
+
MsgProject: () => MsgProject,
|
|
27
|
+
MsgResource: () => MsgResource
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(index_exports);
|
|
30
|
+
|
|
31
|
+
// src/classes/MsgInterface/MsgInterface.ts
|
|
32
|
+
var MSG_DEFAULT_FORMAT = "MF2";
|
|
33
|
+
var DEFAULT_ATTRIBUTES = {
|
|
34
|
+
lang: "und",
|
|
35
|
+
dir: "auto",
|
|
36
|
+
dnt: false
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/classes/MsgMessage/MsgMessage.ts
|
|
40
|
+
var import_messageformat = require("messageformat");
|
|
41
|
+
var import_icu_messageformat_1 = require("@messageformat/icu-messageformat-1");
|
|
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 import_messageformat2 = require("messageformat");
|
|
157
|
+
var import_pseudo_localization = require("pseudo-localization");
|
|
158
|
+
var MsgResource = class _MsgResource extends Map {
|
|
159
|
+
_attributes = {};
|
|
160
|
+
_notes = [];
|
|
161
|
+
_title;
|
|
162
|
+
_project;
|
|
163
|
+
/**
|
|
164
|
+
* Builds a resource from {@link MsgResourceData} and an owning project.
|
|
165
|
+
*
|
|
166
|
+
* When `messages` is provided, each entry is added to the resource.
|
|
167
|
+
* When omitted, the resource starts empty.
|
|
168
|
+
*/
|
|
169
|
+
static create(data, project) {
|
|
170
|
+
const { title, attributes, notes, messages } = data;
|
|
171
|
+
const res = new _MsgResource(title, attributes, project, notes);
|
|
172
|
+
if (messages) {
|
|
173
|
+
messages.forEach((messageData) => {
|
|
174
|
+
const { key, value, attributes: attributes2, notes: notes2 } = messageData;
|
|
175
|
+
res.add(key, value, attributes2, notes2);
|
|
176
|
+
});
|
|
177
|
+
} else {
|
|
178
|
+
res.clear();
|
|
179
|
+
}
|
|
180
|
+
return res;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Creates a resource bound to a project. Prefer {@link MsgResource.create}.
|
|
184
|
+
*/
|
|
185
|
+
constructor(title, attributes, project, notes) {
|
|
186
|
+
super();
|
|
187
|
+
this._title = title;
|
|
188
|
+
this._attributes = { ...DEFAULT_ATTRIBUTES, format: project.format, ...attributes };
|
|
189
|
+
this._project = project;
|
|
190
|
+
if (notes) {
|
|
191
|
+
notes.forEach((note) => this.addNote(note));
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Returns true when the message's attributes match this resource's attributes.
|
|
196
|
+
*/
|
|
197
|
+
hasMatchingAttributes(message) {
|
|
198
|
+
const res = this.attributes;
|
|
199
|
+
const msg = message.attributes;
|
|
200
|
+
return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt && this.resolveFormat(res) === this.resolveFormat(msg);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Resolves a format from attributes, falling back to the library default.
|
|
204
|
+
*/
|
|
205
|
+
resolveFormat(attributes) {
|
|
206
|
+
return attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Pseudo-localizes an MF2 message by localizing only literal text parts.
|
|
210
|
+
*/
|
|
211
|
+
pseudoLocalizeMF2(source, options) {
|
|
212
|
+
const msg = (0, import_messageformat2.parseMessage)(source);
|
|
213
|
+
(0, import_messageformat2.visit)(msg, {
|
|
214
|
+
pattern: (pattern) => {
|
|
215
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
216
|
+
const part = pattern[i];
|
|
217
|
+
if (typeof part === "string") {
|
|
218
|
+
pattern[i] = (0, import_pseudo_localization.localize)(part, options);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
return (0, import_messageformat2.stringifyMessage)(msg);
|
|
224
|
+
}
|
|
225
|
+
/** Locale and formatting metadata for this resource. */
|
|
226
|
+
get attributes() {
|
|
227
|
+
return this._attributes;
|
|
228
|
+
}
|
|
229
|
+
/** Replaces the resource's locale and formatting metadata. */
|
|
230
|
+
set attributes(attributes) {
|
|
231
|
+
this._attributes = attributes;
|
|
232
|
+
}
|
|
233
|
+
/** Notes attached to this resource for translators and tooling. */
|
|
234
|
+
get notes() {
|
|
235
|
+
return this._notes;
|
|
236
|
+
}
|
|
237
|
+
/** Replaces the notes attached to this resource. */
|
|
238
|
+
set notes(notes) {
|
|
239
|
+
this._notes = notes;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Appends a note to this resource.
|
|
243
|
+
*/
|
|
244
|
+
addNote(note) {
|
|
245
|
+
this.notes.push(note);
|
|
246
|
+
}
|
|
247
|
+
/** Title that identifies this resource within a project. */
|
|
248
|
+
get title() {
|
|
249
|
+
return this._title;
|
|
250
|
+
}
|
|
251
|
+
/** Sets the title that identifies this resource within a project. */
|
|
252
|
+
set title(title) {
|
|
253
|
+
this._title = title;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Returns the project this resource belongs to.
|
|
257
|
+
*/
|
|
258
|
+
getProject() {
|
|
259
|
+
return this._project;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Adds a message to this resource, merging resource attributes with any overrides.
|
|
263
|
+
*
|
|
264
|
+
* @returns This resource, for chaining.
|
|
265
|
+
*/
|
|
266
|
+
add(key, value, attributes, notes) {
|
|
267
|
+
const merged = { ...this.attributes, ...attributes };
|
|
268
|
+
const msg = MsgMessage.create({
|
|
269
|
+
key,
|
|
270
|
+
value,
|
|
271
|
+
attributes: merged,
|
|
272
|
+
notes
|
|
273
|
+
});
|
|
274
|
+
this.set(key, msg);
|
|
275
|
+
return this;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Builds a translated copy of this resource from translation data.
|
|
279
|
+
*
|
|
280
|
+
* Messages present in the translation replace the source; missing keys keep
|
|
281
|
+
* the source message. Notes are carried over from the source.
|
|
282
|
+
*
|
|
283
|
+
* @throws {TypeError} When the translation title does not match this resource.
|
|
284
|
+
*/
|
|
285
|
+
translate(data) {
|
|
286
|
+
const { title, attributes, messages } = data;
|
|
287
|
+
if (title !== this.title) {
|
|
288
|
+
throw new TypeError("Title of resource and translations do not match.");
|
|
289
|
+
}
|
|
290
|
+
const translated = _MsgResource.create({
|
|
291
|
+
title,
|
|
292
|
+
// preserve the source resource's format unless the translation overrides it
|
|
293
|
+
attributes: this.preserveFormat(attributes, this.attributes.format),
|
|
294
|
+
notes: this.notes
|
|
295
|
+
// transfer the notes
|
|
296
|
+
}, this._project);
|
|
297
|
+
this.forEach((msg) => {
|
|
298
|
+
translated.set(msg.key, msg);
|
|
299
|
+
});
|
|
300
|
+
messages?.forEach((messageData) => {
|
|
301
|
+
const { key, value, attributes: attributes2 } = messageData;
|
|
302
|
+
const msg = MsgMessage.create({
|
|
303
|
+
key,
|
|
304
|
+
value,
|
|
305
|
+
// preserve the source message's format unless the translation overrides it
|
|
306
|
+
attributes: this.preserveFormat(attributes2, this.get(key)?.attributes.format)
|
|
307
|
+
});
|
|
308
|
+
const notes = this.get(key)?.notes || [];
|
|
309
|
+
notes.forEach((note) => {
|
|
310
|
+
msg.addNote(note);
|
|
311
|
+
});
|
|
312
|
+
translated.set(key, msg);
|
|
313
|
+
});
|
|
314
|
+
return translated;
|
|
315
|
+
}
|
|
316
|
+
preserveFormat(attributes, format) {
|
|
317
|
+
if (format === void 0 || attributes?.format !== void 0) {
|
|
318
|
+
return attributes;
|
|
319
|
+
}
|
|
320
|
+
return { ...attributes, format };
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Loads and applies translations for a locale via the project loader.
|
|
324
|
+
*
|
|
325
|
+
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
326
|
+
* copy without calling the loader.
|
|
327
|
+
*
|
|
328
|
+
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
329
|
+
*/
|
|
330
|
+
async getTranslation(lang) {
|
|
331
|
+
if (!lang) {
|
|
332
|
+
return _MsgResource.create(this.getData(), this.getProject());
|
|
333
|
+
}
|
|
334
|
+
const project = this._project;
|
|
335
|
+
const pseudoLocale = project.locales.pseudoLocale;
|
|
336
|
+
if (lang === pseudoLocale) {
|
|
337
|
+
const pseudolocalizedData = {
|
|
338
|
+
title: this.title,
|
|
339
|
+
attributes: { ...this.attributes, lang: pseudoLocale },
|
|
340
|
+
notes: this.notes.length > 0 ? this.notes : void 0,
|
|
341
|
+
messages: []
|
|
342
|
+
};
|
|
343
|
+
this.forEach((msg) => {
|
|
344
|
+
pseudolocalizedData.messages.push({
|
|
345
|
+
key: msg.key,
|
|
346
|
+
value: this.pseudoLocalizeMF2(msg.value),
|
|
347
|
+
attributes: { ...this.attributes, lang: pseudoLocale }
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
return this.translate(pseudolocalizedData);
|
|
351
|
+
}
|
|
352
|
+
const languageChain = project.getTargetLocale(lang);
|
|
353
|
+
if (!languageChain) {
|
|
354
|
+
throw new Error("Unsupported locale for resource.");
|
|
355
|
+
}
|
|
356
|
+
if (languageChain.length == 0) {
|
|
357
|
+
throw new Error(`Empty language chain for locale: ${lang}`);
|
|
358
|
+
}
|
|
359
|
+
let translated = this;
|
|
360
|
+
for (let i = 0; i < languageChain.length; i++) {
|
|
361
|
+
const lang2 = languageChain[i];
|
|
362
|
+
if (lang2 && project._locales.targetLocales[lang2]) {
|
|
363
|
+
translated = await translated._project._loader(project.project.name, translated.title, lang2).then((data) => translated.translate(data));
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return translated;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Returns a plain data object for this resource and its messages.
|
|
370
|
+
*
|
|
371
|
+
* Attributes that match the resource or project defaults are omitted where
|
|
372
|
+
* possible to keep the output compact.
|
|
373
|
+
*
|
|
374
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
375
|
+
*/
|
|
376
|
+
getData(stripNotes = false) {
|
|
377
|
+
const resourceFormat = this.resolveFormat(this.attributes);
|
|
378
|
+
const projectFormat = this._project.format;
|
|
379
|
+
const messages = [];
|
|
380
|
+
this.forEach((msg) => {
|
|
381
|
+
if (this.hasMatchingAttributes(msg)) {
|
|
382
|
+
const data = {
|
|
383
|
+
key: msg.key,
|
|
384
|
+
value: msg.value,
|
|
385
|
+
notes: !stripNotes && msg.notes.length > 0 ? msg.notes : void 0
|
|
386
|
+
};
|
|
387
|
+
messages.push(data);
|
|
388
|
+
} else {
|
|
389
|
+
const data = msg.getData(stripNotes);
|
|
390
|
+
if (data.attributes && this.resolveFormat(data.attributes) === resourceFormat) {
|
|
391
|
+
const { format, ...rest } = data.attributes;
|
|
392
|
+
data.attributes = rest;
|
|
393
|
+
}
|
|
394
|
+
messages.push(data);
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
const attributes = { ...this.attributes };
|
|
398
|
+
if (this.resolveFormat(attributes) === projectFormat) {
|
|
399
|
+
delete attributes.format;
|
|
400
|
+
}
|
|
401
|
+
return {
|
|
402
|
+
title: this.title,
|
|
403
|
+
attributes,
|
|
404
|
+
notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0,
|
|
405
|
+
messages
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Serializes this resource to a formatted JSON string.
|
|
410
|
+
*
|
|
411
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
412
|
+
*/
|
|
413
|
+
toJSON(stripNotes = false) {
|
|
414
|
+
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
// src/classes/MsgProject/MsgProject.ts
|
|
419
|
+
var defaultProjectSettings = {
|
|
420
|
+
name: "messages",
|
|
421
|
+
version: 1,
|
|
422
|
+
format: MSG_DEFAULT_FORMAT
|
|
423
|
+
};
|
|
424
|
+
var defaultLocalesSettings = {
|
|
425
|
+
sourceLocale: "en",
|
|
426
|
+
pseudoLocale: "en-XA",
|
|
427
|
+
targetLocales: {
|
|
428
|
+
en: ["en"]
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
var MsgProject = class _MsgProject {
|
|
432
|
+
/** Project name, version, and default format. */
|
|
433
|
+
_project;
|
|
434
|
+
/** Source, pseudo, and target locale settings. */
|
|
435
|
+
_locales;
|
|
436
|
+
/** Function used to load translations for a locale. */
|
|
437
|
+
_loader;
|
|
438
|
+
/**
|
|
439
|
+
* Builds a project from {@link MsgProjectData}.
|
|
440
|
+
*/
|
|
441
|
+
static create(data) {
|
|
442
|
+
const { project, locales, loader } = data;
|
|
443
|
+
return new _MsgProject(project, locales, loader);
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Creates a project from its settings and loader. Prefer {@link MsgProject.create}.
|
|
447
|
+
*/
|
|
448
|
+
constructor(projectSettings, localesSettings, loader) {
|
|
449
|
+
this._project = { ...defaultProjectSettings, ...projectSettings };
|
|
450
|
+
this._locales = { ...defaultLocalesSettings, ...localesSettings };
|
|
451
|
+
this._loader = loader;
|
|
452
|
+
}
|
|
453
|
+
/** Project name, version, and default format. */
|
|
454
|
+
get project() {
|
|
455
|
+
return this._project;
|
|
456
|
+
}
|
|
457
|
+
/** Source, pseudo, and target locale settings. */
|
|
458
|
+
get locales() {
|
|
459
|
+
return this._locales;
|
|
460
|
+
}
|
|
461
|
+
/** Function used to load translations for a locale. */
|
|
462
|
+
get loader() {
|
|
463
|
+
return this._loader;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* The project-wide default message format. Resources (and, through them,
|
|
467
|
+
* messages) inherit this value unless they specify their own.
|
|
468
|
+
*/
|
|
469
|
+
get format() {
|
|
470
|
+
return this._project.format ?? MSG_DEFAULT_FORMAT;
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Returns the translation load chain for a locale, or `undefined` if unsupported.
|
|
474
|
+
*/
|
|
475
|
+
getTargetLocale(locale) {
|
|
476
|
+
return this._locales.targetLocales[locale];
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
480
|
+
0 && (module.exports = {
|
|
481
|
+
DEFAULT_ATTRIBUTES,
|
|
482
|
+
MSG_DEFAULT_FORMAT,
|
|
483
|
+
MsgMessage,
|
|
484
|
+
MsgProject,
|
|
485
|
+
MsgResource
|
|
486
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, MsgAttributes, MsgFormat, MsgInterface, MsgNote, NoteTypes } from './classes/MsgInterface/MsgInterface.cjs';
|
|
2
|
+
export { MsgMessage, MsgMessageData } from './classes/MsgMessage/MsgMessage.cjs';
|
|
3
|
+
export { M as MsgLocalesSettings, a as MsgProject, b as MsgProjectData, c as MsgProjectSettings, MsgResource, MsgResourceData, d as MsgTargetLocalesSettings, e as MsgTranslationLoader } from './classes/MsgResource/MsgResource.cjs';
|
|
4
|
+
import 'messageformat';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, MsgAttributes, MsgFormat, MsgInterface, MsgNote, NoteTypes } from './classes/MsgInterface/MsgInterface.js';
|
|
2
|
+
export { MsgMessage, MsgMessageData } from './classes/MsgMessage/MsgMessage.js';
|
|
3
|
+
export { M as MsgLocalesSettings, a as MsgProject, b as MsgProjectData, c as MsgProjectSettings, MsgResource, MsgResourceData, d as MsgTargetLocalesSettings, e as MsgTranslationLoader } from './classes/MsgResource/MsgResource.js';
|
|
4
|
+
import 'messageformat';
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import "./chunk-ORHP7HGX.mjs";
|
|
2
|
+
import {
|
|
3
|
+
MsgProject
|
|
4
|
+
} from "./chunk-DJFPVXQ5.mjs";
|
|
5
|
+
import {
|
|
6
|
+
MsgResource
|
|
7
|
+
} from "./chunk-IMOOTZIK.mjs";
|
|
8
|
+
import {
|
|
9
|
+
MsgMessage
|
|
10
|
+
} from "./chunk-F3VAAZJZ.mjs";
|
|
11
|
+
import {
|
|
12
|
+
DEFAULT_ATTRIBUTES,
|
|
13
|
+
MSG_DEFAULT_FORMAT
|
|
14
|
+
} from "./chunk-XQVALCKX.mjs";
|
|
15
|
+
export {
|
|
16
|
+
DEFAULT_ATTRIBUTES,
|
|
17
|
+
MSG_DEFAULT_FORMAT,
|
|
18
|
+
MsgMessage,
|
|
19
|
+
MsgProject,
|
|
20
|
+
MsgResource
|
|
21
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
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/lib/index.ts
|
|
21
|
+
var lib_exports = {};
|
|
22
|
+
__export(lib_exports, {
|
|
23
|
+
getLang: () => getLang,
|
|
24
|
+
setLang: () => setLang
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(lib_exports);
|
|
27
|
+
|
|
28
|
+
// src/lib/lang-utils.ts
|
|
29
|
+
var lang;
|
|
30
|
+
function setLang(tag) {
|
|
31
|
+
lang = tag;
|
|
32
|
+
}
|
|
33
|
+
function getLang() {
|
|
34
|
+
return lang;
|
|
35
|
+
}
|
|
36
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
37
|
+
0 && (module.exports = {
|
|
38
|
+
getLang,
|
|
39
|
+
setLang
|
|
40
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { getLang, setLang } from './lang-utils.cjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { getLang, setLang } from './lang-utils.js';
|
|
@@ -0,0 +1,38 @@
|
|
|
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/lib/lang-utils.ts
|
|
21
|
+
var lang_utils_exports = {};
|
|
22
|
+
__export(lang_utils_exports, {
|
|
23
|
+
getLang: () => getLang,
|
|
24
|
+
setLang: () => setLang
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(lang_utils_exports);
|
|
27
|
+
var lang;
|
|
28
|
+
function setLang(tag) {
|
|
29
|
+
lang = tag;
|
|
30
|
+
}
|
|
31
|
+
function getLang() {
|
|
32
|
+
return lang;
|
|
33
|
+
}
|
|
34
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
35
|
+
0 && (module.exports = {
|
|
36
|
+
getLang,
|
|
37
|
+
setLang
|
|
38
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Set value of lang
|
|
3
|
+
*
|
|
4
|
+
* @param tag string
|
|
5
|
+
*/
|
|
6
|
+
declare function setLang(tag: string): void;
|
|
7
|
+
/**
|
|
8
|
+
* Returns value of lang;
|
|
9
|
+
*
|
|
10
|
+
* @returns string | undefined
|
|
11
|
+
*/
|
|
12
|
+
declare function getLang(): string | undefined;
|
|
13
|
+
|
|
14
|
+
export { getLang, setLang };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Set value of lang
|
|
3
|
+
*
|
|
4
|
+
* @param tag string
|
|
5
|
+
*/
|
|
6
|
+
declare function setLang(tag: string): void;
|
|
7
|
+
/**
|
|
8
|
+
* Returns value of lang;
|
|
9
|
+
*
|
|
10
|
+
* @returns string | undefined
|
|
11
|
+
*/
|
|
12
|
+
declare function getLang(): string | undefined;
|
|
13
|
+
|
|
14
|
+
export { getLang, setLang };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@worldware/msg",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Message localization tooling",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Joel Sahleen",
|
|
@@ -46,6 +46,7 @@
|
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"build": "tsup",
|
|
49
|
+
"prepack": "npm run build",
|
|
49
50
|
"test": "vitest run",
|
|
50
51
|
"test:watch": "vitest",
|
|
51
52
|
"coverage": "vitest run --coverage",
|