@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
|
@@ -1,79 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import '../MsgMessage/MsgMessage.js';
|
|
2
|
+
import '../MsgInterface/MsgInterface.js';
|
|
3
|
+
export { MsgResource, MsgResourceData } from '../index.js';
|
|
3
4
|
import 'messageformat';
|
|
4
|
-
|
|
5
|
-
type MsgResourceData = {
|
|
6
|
-
title: string;
|
|
7
|
-
attributes: MsgAttributes;
|
|
8
|
-
notes?: MsgNote[];
|
|
9
|
-
messages?: MsgMessageData[];
|
|
10
|
-
};
|
|
11
|
-
declare class MsgResource extends Map<string, MsgMessage> implements MsgInterface {
|
|
12
|
-
private _attributes;
|
|
13
|
-
private _notes;
|
|
14
|
-
private _title;
|
|
15
|
-
private _project;
|
|
16
|
-
static create(data: MsgResourceData, project: MsgProject): MsgResource;
|
|
17
|
-
private constructor();
|
|
18
|
-
private hasMatchingAttributes;
|
|
19
|
-
private resolveFormat;
|
|
20
|
-
private pseudoLocalizeMF2;
|
|
21
|
-
get attributes(): MsgAttributes;
|
|
22
|
-
set attributes(attributes: MsgAttributes);
|
|
23
|
-
get notes(): MsgNote[];
|
|
24
|
-
set notes(notes: MsgNote[]);
|
|
25
|
-
addNote(note: MsgNote): void;
|
|
26
|
-
get title(): string;
|
|
27
|
-
set title(title: string);
|
|
28
|
-
getProject(): MsgProject;
|
|
29
|
-
add(key: string, value: string, attributes?: MsgAttributes, notes?: MsgNote[]): this;
|
|
30
|
-
translate(data: MsgResourceData): MsgResource;
|
|
31
|
-
/**
|
|
32
|
-
* Merge a `format` into an attribute set unless one is already present,
|
|
33
|
-
* so translations inherit the source's format without overriding an
|
|
34
|
-
* explicitly translated one. Returns the input unchanged when there is
|
|
35
|
-
* no format to preserve.
|
|
36
|
-
*/
|
|
37
|
-
private preserveFormat;
|
|
38
|
-
getTranslation(lang: string): Promise<MsgResource>;
|
|
39
|
-
getData(stripNotes?: boolean): MsgResourceData;
|
|
40
|
-
toJSON(stripNotes?: boolean): string;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
type MsgProjectSettings = {
|
|
44
|
-
name: string;
|
|
45
|
-
version?: number;
|
|
46
|
-
format?: MsgFormat;
|
|
47
|
-
};
|
|
48
|
-
type MsgTargetLocalesSettings = {
|
|
49
|
-
[key: string]: string[];
|
|
50
|
-
};
|
|
51
|
-
type MsgLocalesSettings = {
|
|
52
|
-
sourceLocale: string;
|
|
53
|
-
pseudoLocale: string;
|
|
54
|
-
targetLocales: MsgTargetLocalesSettings;
|
|
55
|
-
};
|
|
56
|
-
type MsgTranslationLoader = (project: string, title: string, lang: string) => Promise<MsgResourceData>;
|
|
57
|
-
type MsgProjectData = {
|
|
58
|
-
project: MsgProjectSettings;
|
|
59
|
-
locales: MsgLocalesSettings;
|
|
60
|
-
loader: MsgTranslationLoader;
|
|
61
|
-
};
|
|
62
|
-
declare class MsgProject {
|
|
63
|
-
_project: MsgProjectSettings;
|
|
64
|
-
_locales: MsgLocalesSettings;
|
|
65
|
-
_loader: MsgTranslationLoader;
|
|
66
|
-
static create(data: MsgProjectData): MsgProject;
|
|
67
|
-
private constructor();
|
|
68
|
-
get project(): MsgProjectSettings;
|
|
69
|
-
get locales(): MsgLocalesSettings;
|
|
70
|
-
get loader(): MsgTranslationLoader;
|
|
71
|
-
/**
|
|
72
|
-
* The project-wide default message format. Resources (and, through them,
|
|
73
|
-
* messages) inherit this value unless they specify their own.
|
|
74
|
-
*/
|
|
75
|
-
get format(): MsgFormat;
|
|
76
|
-
getTargetLocale(locale: string): string[] | undefined;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export { MsgProject as M, MsgResource, type MsgResourceData, type MsgProjectData as a, type MsgTranslationLoader as b };
|
package/dist/classes/index.cjs
CHANGED
|
@@ -45,33 +45,57 @@ var MsgMessage = class _MsgMessage {
|
|
|
45
45
|
_mf;
|
|
46
46
|
_attributes;
|
|
47
47
|
_notes = [];
|
|
48
|
+
/**
|
|
49
|
+
* Creates a message from its parts. Prefer {@link MsgMessage.create}.
|
|
50
|
+
*/
|
|
48
51
|
constructor(key, value, attributes, notes) {
|
|
49
52
|
this._key = key;
|
|
50
53
|
this._value = value;
|
|
51
54
|
this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
|
|
52
55
|
if (notes) {
|
|
53
|
-
notes.forEach((note) => this.
|
|
56
|
+
notes.forEach((note) => this.setNote(note));
|
|
54
57
|
}
|
|
55
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Builds a message from {@link MsgMessageData}.
|
|
61
|
+
*/
|
|
56
62
|
static create(data) {
|
|
57
63
|
const { key, value, attributes, notes } = data;
|
|
58
64
|
const message = new _MsgMessage(key, value, attributes, notes);
|
|
59
65
|
return message;
|
|
60
66
|
}
|
|
67
|
+
/** Unique key that identifies this message within a resource. */
|
|
61
68
|
get key() {
|
|
62
69
|
return this._key;
|
|
63
70
|
}
|
|
71
|
+
/** The message string, possibly in MessageFormat syntax. */
|
|
64
72
|
get value() {
|
|
65
73
|
return this._value;
|
|
66
74
|
}
|
|
75
|
+
/** Locale and formatting metadata for this message. */
|
|
67
76
|
get attributes() {
|
|
68
77
|
return this._attributes;
|
|
69
78
|
}
|
|
79
|
+
/** Notes attached to this message for translators and tooling. */
|
|
70
80
|
get notes() {
|
|
71
81
|
return this._notes;
|
|
72
82
|
}
|
|
73
|
-
|
|
83
|
+
/**
|
|
84
|
+
* Appends a note to this message.
|
|
85
|
+
*
|
|
86
|
+
* @returns This message, for chaining.
|
|
87
|
+
*/
|
|
88
|
+
addNote(type, content) {
|
|
89
|
+
return this.setNote({ type, content });
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Appends a full note object to this message.
|
|
93
|
+
*
|
|
94
|
+
* @returns This message, for chaining.
|
|
95
|
+
*/
|
|
96
|
+
setNote(note) {
|
|
74
97
|
this.notes.push(note);
|
|
98
|
+
return this;
|
|
75
99
|
}
|
|
76
100
|
/**
|
|
77
101
|
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
@@ -79,24 +103,42 @@ var MsgMessage = class _MsgMessage {
|
|
|
79
103
|
resolveFormat() {
|
|
80
104
|
return this.attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
81
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Returns a cached MessageFormat formatter for this message's value.
|
|
108
|
+
*/
|
|
82
109
|
getFormatter(options) {
|
|
83
110
|
if (!this._mf) {
|
|
84
111
|
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);
|
|
85
112
|
}
|
|
86
113
|
return this._mf;
|
|
87
114
|
}
|
|
115
|
+
/**
|
|
116
|
+
* Formats the message with the given placeholder data.
|
|
117
|
+
*
|
|
118
|
+
* For `NONE` format messages, returns the raw value unchanged.
|
|
119
|
+
*/
|
|
88
120
|
format(data, options) {
|
|
89
121
|
if (this.resolveFormat() === "NONE") {
|
|
90
122
|
return this.value;
|
|
91
123
|
}
|
|
92
124
|
return this.getFormatter(options).format(data);
|
|
93
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Formats the message into structured parts with the given placeholder data.
|
|
128
|
+
*
|
|
129
|
+
* For `NONE` format messages, returns a single text part with the raw value.
|
|
130
|
+
*/
|
|
94
131
|
formatToParts(data, options) {
|
|
95
132
|
if (this.resolveFormat() === "NONE") {
|
|
96
133
|
return [{ type: "text", value: this.value }];
|
|
97
134
|
}
|
|
98
135
|
return this.getFormatter(options).formatToParts(data);
|
|
99
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* Returns a plain data object for this message.
|
|
139
|
+
*
|
|
140
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
141
|
+
*/
|
|
100
142
|
getData(stripNotes = false) {
|
|
101
143
|
return {
|
|
102
144
|
key: this.key,
|
|
@@ -105,9 +147,17 @@ var MsgMessage = class _MsgMessage {
|
|
|
105
147
|
notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0
|
|
106
148
|
};
|
|
107
149
|
}
|
|
150
|
+
/**
|
|
151
|
+
* Returns the message value as a string.
|
|
152
|
+
*/
|
|
108
153
|
toString() {
|
|
109
154
|
return this.value;
|
|
110
155
|
}
|
|
156
|
+
/**
|
|
157
|
+
* Serializes this message to a formatted JSON string.
|
|
158
|
+
*
|
|
159
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
160
|
+
*/
|
|
111
161
|
toJSON(stripNotes = false) {
|
|
112
162
|
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
113
163
|
}
|
|
@@ -121,6 +171,12 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
121
171
|
_notes = [];
|
|
122
172
|
_title;
|
|
123
173
|
_project;
|
|
174
|
+
/**
|
|
175
|
+
* Builds a resource from {@link MsgResourceData} and an owning project.
|
|
176
|
+
*
|
|
177
|
+
* When `messages` is provided, each entry is added to the resource.
|
|
178
|
+
* When omitted, the resource starts empty.
|
|
179
|
+
*/
|
|
124
180
|
static create(data, project) {
|
|
125
181
|
const { title, attributes, notes, messages } = data;
|
|
126
182
|
const res = new _MsgResource(title, attributes, project, notes);
|
|
@@ -134,23 +190,35 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
134
190
|
}
|
|
135
191
|
return res;
|
|
136
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* Creates a resource bound to a project. Prefer {@link MsgResource.create}.
|
|
195
|
+
*/
|
|
137
196
|
constructor(title, attributes, project, notes) {
|
|
138
197
|
super();
|
|
139
198
|
this._title = title;
|
|
140
199
|
this._attributes = { ...DEFAULT_ATTRIBUTES, format: project.format, ...attributes };
|
|
141
200
|
this._project = project;
|
|
142
201
|
if (notes) {
|
|
143
|
-
notes.forEach((note) => this.
|
|
202
|
+
notes.forEach((note) => this.setNote(note));
|
|
144
203
|
}
|
|
145
204
|
}
|
|
205
|
+
/**
|
|
206
|
+
* Returns true when the message's attributes match this resource's attributes.
|
|
207
|
+
*/
|
|
146
208
|
hasMatchingAttributes(message) {
|
|
147
209
|
const res = this.attributes;
|
|
148
210
|
const msg = message.attributes;
|
|
149
211
|
return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt && this.resolveFormat(res) === this.resolveFormat(msg);
|
|
150
212
|
}
|
|
213
|
+
/**
|
|
214
|
+
* Resolves a format from attributes, falling back to the library default.
|
|
215
|
+
*/
|
|
151
216
|
resolveFormat(attributes) {
|
|
152
217
|
return attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
153
218
|
}
|
|
219
|
+
/**
|
|
220
|
+
* Pseudo-localizes an MF2 message by localizing only literal text parts.
|
|
221
|
+
*/
|
|
154
222
|
pseudoLocalizeMF2(source, options) {
|
|
155
223
|
const msg = (0, import_messageformat2.parseMessage)(source);
|
|
156
224
|
(0, import_messageformat2.visit)(msg, {
|
|
@@ -165,30 +233,58 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
165
233
|
});
|
|
166
234
|
return (0, import_messageformat2.stringifyMessage)(msg);
|
|
167
235
|
}
|
|
236
|
+
/** Locale and formatting metadata for this resource. */
|
|
168
237
|
get attributes() {
|
|
169
238
|
return this._attributes;
|
|
170
239
|
}
|
|
240
|
+
/** Replaces the resource's locale and formatting metadata. */
|
|
171
241
|
set attributes(attributes) {
|
|
172
242
|
this._attributes = attributes;
|
|
173
243
|
}
|
|
244
|
+
/** Notes attached to this resource for translators and tooling. */
|
|
174
245
|
get notes() {
|
|
175
246
|
return this._notes;
|
|
176
247
|
}
|
|
248
|
+
/** Replaces the notes attached to this resource. */
|
|
177
249
|
set notes(notes) {
|
|
178
250
|
this._notes = notes;
|
|
179
251
|
}
|
|
180
|
-
|
|
252
|
+
/**
|
|
253
|
+
* Appends a note to this resource.
|
|
254
|
+
*
|
|
255
|
+
* @returns This resource, for chaining.
|
|
256
|
+
*/
|
|
257
|
+
addNote(type, content) {
|
|
258
|
+
return this.setNote({ type, content });
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Appends a full note object to this resource.
|
|
262
|
+
*
|
|
263
|
+
* @returns This resource, for chaining.
|
|
264
|
+
*/
|
|
265
|
+
setNote(note) {
|
|
181
266
|
this.notes.push(note);
|
|
267
|
+
return this;
|
|
182
268
|
}
|
|
269
|
+
/** Title that identifies this resource within a project. */
|
|
183
270
|
get title() {
|
|
184
271
|
return this._title;
|
|
185
272
|
}
|
|
273
|
+
/** Sets the title that identifies this resource within a project. */
|
|
186
274
|
set title(title) {
|
|
187
275
|
this._title = title;
|
|
188
276
|
}
|
|
277
|
+
/**
|
|
278
|
+
* Returns the project this resource belongs to.
|
|
279
|
+
*/
|
|
189
280
|
getProject() {
|
|
190
281
|
return this._project;
|
|
191
282
|
}
|
|
283
|
+
/**
|
|
284
|
+
* Adds a message to this resource, merging resource attributes with any overrides.
|
|
285
|
+
*
|
|
286
|
+
* @returns This resource, for chaining.
|
|
287
|
+
*/
|
|
192
288
|
add(key, value, attributes, notes) {
|
|
193
289
|
const merged = { ...this.attributes, ...attributes };
|
|
194
290
|
const msg = MsgMessage.create({
|
|
@@ -200,6 +296,14 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
200
296
|
this.set(key, msg);
|
|
201
297
|
return this;
|
|
202
298
|
}
|
|
299
|
+
/**
|
|
300
|
+
* Builds a translated copy of this resource from translation data.
|
|
301
|
+
*
|
|
302
|
+
* Messages present in the translation replace the source; missing keys keep
|
|
303
|
+
* the source message. Notes are carried over from the source.
|
|
304
|
+
*
|
|
305
|
+
* @throws {TypeError} When the translation title does not match this resource.
|
|
306
|
+
*/
|
|
203
307
|
translate(data) {
|
|
204
308
|
const { title, attributes, messages } = data;
|
|
205
309
|
if (title !== this.title) {
|
|
@@ -225,7 +329,7 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
225
329
|
});
|
|
226
330
|
const notes = this.get(key)?.notes || [];
|
|
227
331
|
notes.forEach((note) => {
|
|
228
|
-
msg.addNote(note);
|
|
332
|
+
msg.addNote(note.type, note.content);
|
|
229
333
|
});
|
|
230
334
|
translated.set(key, msg);
|
|
231
335
|
});
|
|
@@ -237,7 +341,18 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
237
341
|
}
|
|
238
342
|
return { ...attributes, format };
|
|
239
343
|
}
|
|
344
|
+
/**
|
|
345
|
+
* Loads and applies translations for a locale via the project loader.
|
|
346
|
+
*
|
|
347
|
+
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
348
|
+
* copy without calling the loader.
|
|
349
|
+
*
|
|
350
|
+
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
351
|
+
*/
|
|
240
352
|
async getTranslation(lang) {
|
|
353
|
+
if (!lang) {
|
|
354
|
+
return _MsgResource.create(this.getData(), this.getProject());
|
|
355
|
+
}
|
|
241
356
|
const project = this._project;
|
|
242
357
|
const pseudoLocale = project.locales.pseudoLocale;
|
|
243
358
|
if (lang === pseudoLocale) {
|
|
@@ -272,6 +387,14 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
272
387
|
}
|
|
273
388
|
return translated;
|
|
274
389
|
}
|
|
390
|
+
/**
|
|
391
|
+
* Returns a plain data object for this resource and its messages.
|
|
392
|
+
*
|
|
393
|
+
* Attributes that match the resource or project defaults are omitted where
|
|
394
|
+
* possible to keep the output compact.
|
|
395
|
+
*
|
|
396
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
397
|
+
*/
|
|
275
398
|
getData(stripNotes = false) {
|
|
276
399
|
const resourceFormat = this.resolveFormat(this.attributes);
|
|
277
400
|
const projectFormat = this._project.format;
|
|
@@ -304,6 +427,11 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
304
427
|
messages
|
|
305
428
|
};
|
|
306
429
|
}
|
|
430
|
+
/**
|
|
431
|
+
* Serializes this resource to a formatted JSON string.
|
|
432
|
+
*
|
|
433
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
434
|
+
*/
|
|
307
435
|
toJSON(stripNotes = false) {
|
|
308
436
|
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
309
437
|
}
|
|
@@ -323,24 +451,36 @@ var defaultLocalesSettings = {
|
|
|
323
451
|
}
|
|
324
452
|
};
|
|
325
453
|
var MsgProject = class _MsgProject {
|
|
454
|
+
/** Project name, version, and default format. */
|
|
326
455
|
_project;
|
|
456
|
+
/** Source, pseudo, and target locale settings. */
|
|
327
457
|
_locales;
|
|
458
|
+
/** Function used to load translations for a locale. */
|
|
328
459
|
_loader;
|
|
460
|
+
/**
|
|
461
|
+
* Builds a project from {@link MsgProjectData}.
|
|
462
|
+
*/
|
|
329
463
|
static create(data) {
|
|
330
464
|
const { project, locales, loader } = data;
|
|
331
465
|
return new _MsgProject(project, locales, loader);
|
|
332
466
|
}
|
|
467
|
+
/**
|
|
468
|
+
* Creates a project from its settings and loader. Prefer {@link MsgProject.create}.
|
|
469
|
+
*/
|
|
333
470
|
constructor(projectSettings, localesSettings, loader) {
|
|
334
471
|
this._project = { ...defaultProjectSettings, ...projectSettings };
|
|
335
472
|
this._locales = { ...defaultLocalesSettings, ...localesSettings };
|
|
336
473
|
this._loader = loader;
|
|
337
474
|
}
|
|
475
|
+
/** Project name, version, and default format. */
|
|
338
476
|
get project() {
|
|
339
477
|
return this._project;
|
|
340
478
|
}
|
|
479
|
+
/** Source, pseudo, and target locale settings. */
|
|
341
480
|
get locales() {
|
|
342
481
|
return this._locales;
|
|
343
482
|
}
|
|
483
|
+
/** Function used to load translations for a locale. */
|
|
344
484
|
get loader() {
|
|
345
485
|
return this._loader;
|
|
346
486
|
}
|
|
@@ -351,6 +491,9 @@ var MsgProject = class _MsgProject {
|
|
|
351
491
|
get format() {
|
|
352
492
|
return this._project.format ?? MSG_DEFAULT_FORMAT;
|
|
353
493
|
}
|
|
494
|
+
/**
|
|
495
|
+
* Returns the translation load chain for a locale, or `undefined` if unsupported.
|
|
496
|
+
*/
|
|
354
497
|
getTargetLocale(locale) {
|
|
355
498
|
return this._locales.targetLocales[locale];
|
|
356
499
|
}
|
package/dist/classes/index.d.cts
CHANGED
|
@@ -1,4 +1,210 @@
|
|
|
1
|
-
|
|
2
|
-
export {
|
|
3
|
-
|
|
1
|
+
import { MsgAttributes, MsgNote, MsgInterface, NoteTypes, MsgFormat } from './MsgInterface/MsgInterface.cjs';
|
|
2
|
+
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT } from './MsgInterface/MsgInterface.cjs';
|
|
3
|
+
import { MsgMessageData, MsgMessage } from './MsgMessage/MsgMessage.cjs';
|
|
4
4
|
import 'messageformat';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Plain data used to create or describe a {@link MsgResource}.
|
|
8
|
+
*/
|
|
9
|
+
type MsgResourceData = {
|
|
10
|
+
/** Title that identifies this resource within a project. */
|
|
11
|
+
title: string;
|
|
12
|
+
/** Locale and formatting metadata for the resource. */
|
|
13
|
+
attributes: MsgAttributes;
|
|
14
|
+
/** Optional notes for translators and tooling. */
|
|
15
|
+
notes?: MsgNote[];
|
|
16
|
+
/** Optional list of messages in this resource. */
|
|
17
|
+
messages?: MsgMessageData[];
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* A named collection of messages belonging to a {@link MsgProject}.
|
|
21
|
+
*
|
|
22
|
+
* Resources inherit format defaults from their project, support translation
|
|
23
|
+
* loading (including pseudo-localization), and can serialize to plain data.
|
|
24
|
+
*/
|
|
25
|
+
declare class MsgResource extends Map<string, MsgMessage> implements MsgInterface {
|
|
26
|
+
private _attributes;
|
|
27
|
+
private _notes;
|
|
28
|
+
private _title;
|
|
29
|
+
private _project;
|
|
30
|
+
/**
|
|
31
|
+
* Builds a resource from {@link MsgResourceData} and an owning project.
|
|
32
|
+
*
|
|
33
|
+
* When `messages` is provided, each entry is added to the resource.
|
|
34
|
+
* When omitted, the resource starts empty.
|
|
35
|
+
*/
|
|
36
|
+
static create(data: MsgResourceData, project: MsgProject): MsgResource;
|
|
37
|
+
/**
|
|
38
|
+
* Creates a resource bound to a project. Prefer {@link MsgResource.create}.
|
|
39
|
+
*/
|
|
40
|
+
private constructor();
|
|
41
|
+
/**
|
|
42
|
+
* Returns true when the message's attributes match this resource's attributes.
|
|
43
|
+
*/
|
|
44
|
+
private hasMatchingAttributes;
|
|
45
|
+
/**
|
|
46
|
+
* Resolves a format from attributes, falling back to the library default.
|
|
47
|
+
*/
|
|
48
|
+
private resolveFormat;
|
|
49
|
+
/**
|
|
50
|
+
* Pseudo-localizes an MF2 message by localizing only literal text parts.
|
|
51
|
+
*/
|
|
52
|
+
private pseudoLocalizeMF2;
|
|
53
|
+
/** Locale and formatting metadata for this resource. */
|
|
54
|
+
get attributes(): MsgAttributes;
|
|
55
|
+
/** Replaces the resource's locale and formatting metadata. */
|
|
56
|
+
set attributes(attributes: MsgAttributes);
|
|
57
|
+
/** Notes attached to this resource for translators and tooling. */
|
|
58
|
+
get notes(): MsgNote[];
|
|
59
|
+
/** Replaces the notes attached to this resource. */
|
|
60
|
+
set notes(notes: MsgNote[]);
|
|
61
|
+
/**
|
|
62
|
+
* Appends a note to this resource.
|
|
63
|
+
*
|
|
64
|
+
* @returns This resource, for chaining.
|
|
65
|
+
*/
|
|
66
|
+
addNote(type: NoteTypes, content: string): this;
|
|
67
|
+
/**
|
|
68
|
+
* Appends a full note object to this resource.
|
|
69
|
+
*
|
|
70
|
+
* @returns This resource, for chaining.
|
|
71
|
+
*/
|
|
72
|
+
private setNote;
|
|
73
|
+
/** Title that identifies this resource within a project. */
|
|
74
|
+
get title(): string;
|
|
75
|
+
/** Sets the title that identifies this resource within a project. */
|
|
76
|
+
set title(title: string);
|
|
77
|
+
/**
|
|
78
|
+
* Returns the project this resource belongs to.
|
|
79
|
+
*/
|
|
80
|
+
getProject(): MsgProject;
|
|
81
|
+
/**
|
|
82
|
+
* Adds a message to this resource, merging resource attributes with any overrides.
|
|
83
|
+
*
|
|
84
|
+
* @returns This resource, for chaining.
|
|
85
|
+
*/
|
|
86
|
+
add(key: string, value: string, attributes?: MsgAttributes, notes?: MsgNote[]): this;
|
|
87
|
+
/**
|
|
88
|
+
* Builds a translated copy of this resource from translation data.
|
|
89
|
+
*
|
|
90
|
+
* Messages present in the translation replace the source; missing keys keep
|
|
91
|
+
* the source message. Notes are carried over from the source.
|
|
92
|
+
*
|
|
93
|
+
* @throws {TypeError} When the translation title does not match this resource.
|
|
94
|
+
*/
|
|
95
|
+
translate(data: MsgResourceData): MsgResource;
|
|
96
|
+
/**
|
|
97
|
+
* Merge a `format` into an attribute set unless one is already present,
|
|
98
|
+
* so translations inherit the source's format without overriding an
|
|
99
|
+
* explicitly translated one. Returns the input unchanged when there is
|
|
100
|
+
* no format to preserve.
|
|
101
|
+
*/
|
|
102
|
+
private preserveFormat;
|
|
103
|
+
/**
|
|
104
|
+
* Loads and applies translations for a locale via the project loader.
|
|
105
|
+
*
|
|
106
|
+
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
107
|
+
* copy without calling the loader.
|
|
108
|
+
*
|
|
109
|
+
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
110
|
+
*/
|
|
111
|
+
getTranslation(lang?: string): Promise<MsgResource>;
|
|
112
|
+
/**
|
|
113
|
+
* Returns a plain data object for this resource and its messages.
|
|
114
|
+
*
|
|
115
|
+
* Attributes that match the resource or project defaults are omitted where
|
|
116
|
+
* possible to keep the output compact.
|
|
117
|
+
*
|
|
118
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
119
|
+
*/
|
|
120
|
+
getData(stripNotes?: boolean): MsgResourceData;
|
|
121
|
+
/**
|
|
122
|
+
* Serializes this resource to a formatted JSON string.
|
|
123
|
+
*
|
|
124
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
125
|
+
*/
|
|
126
|
+
toJSON(stripNotes?: boolean): string;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Project-level settings such as name, version, and default message format.
|
|
131
|
+
*/
|
|
132
|
+
type MsgProjectSettings = {
|
|
133
|
+
/** Display name of the localization project. */
|
|
134
|
+
name: string;
|
|
135
|
+
/** Optional project version number. */
|
|
136
|
+
version?: number;
|
|
137
|
+
/** Default message format inherited by resources and messages. */
|
|
138
|
+
format?: MsgFormat;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Map of a requested locale to the chain of locales used to load translations.
|
|
142
|
+
*/
|
|
143
|
+
type MsgTargetLocalesSettings = {
|
|
144
|
+
[key: string]: string[];
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* Locale configuration for a project: source, pseudo, and target locales.
|
|
148
|
+
*/
|
|
149
|
+
type MsgLocalesSettings = {
|
|
150
|
+
/** Locale of the source (authoring) strings. */
|
|
151
|
+
sourceLocale: string;
|
|
152
|
+
/** Locale used for pseudo-localization during development. */
|
|
153
|
+
pseudoLocale: string;
|
|
154
|
+
/** Target locales and their fallback load chains. */
|
|
155
|
+
targetLocales: MsgTargetLocalesSettings;
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* Async function that loads translated resource data for a project, title, and language.
|
|
159
|
+
*/
|
|
160
|
+
type MsgTranslationLoader = (project: string, title: string, lang: string) => Promise<MsgResourceData>;
|
|
161
|
+
/**
|
|
162
|
+
* Plain data used to create a {@link MsgProject}.
|
|
163
|
+
*/
|
|
164
|
+
type MsgProjectData = {
|
|
165
|
+
/** Project name, version, and default format. */
|
|
166
|
+
project: MsgProjectSettings;
|
|
167
|
+
/** Source, pseudo, and target locale settings. */
|
|
168
|
+
locales: MsgLocalesSettings;
|
|
169
|
+
/** Function used to load translations for a locale. */
|
|
170
|
+
loader: MsgTranslationLoader;
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* A localization project that holds settings, locales, and a translation loader.
|
|
174
|
+
*
|
|
175
|
+
* Resources are created against a project so they can inherit format defaults
|
|
176
|
+
* and load translations through the project's loader.
|
|
177
|
+
*/
|
|
178
|
+
declare class MsgProject {
|
|
179
|
+
/** Project name, version, and default format. */
|
|
180
|
+
_project: MsgProjectSettings;
|
|
181
|
+
/** Source, pseudo, and target locale settings. */
|
|
182
|
+
_locales: MsgLocalesSettings;
|
|
183
|
+
/** Function used to load translations for a locale. */
|
|
184
|
+
_loader: MsgTranslationLoader;
|
|
185
|
+
/**
|
|
186
|
+
* Builds a project from {@link MsgProjectData}.
|
|
187
|
+
*/
|
|
188
|
+
static create(data: MsgProjectData): MsgProject;
|
|
189
|
+
/**
|
|
190
|
+
* Creates a project from its settings and loader. Prefer {@link MsgProject.create}.
|
|
191
|
+
*/
|
|
192
|
+
private constructor();
|
|
193
|
+
/** Project name, version, and default format. */
|
|
194
|
+
get project(): MsgProjectSettings;
|
|
195
|
+
/** Source, pseudo, and target locale settings. */
|
|
196
|
+
get locales(): MsgLocalesSettings;
|
|
197
|
+
/** Function used to load translations for a locale. */
|
|
198
|
+
get loader(): MsgTranslationLoader;
|
|
199
|
+
/**
|
|
200
|
+
* The project-wide default message format. Resources (and, through them,
|
|
201
|
+
* messages) inherit this value unless they specify their own.
|
|
202
|
+
*/
|
|
203
|
+
get format(): MsgFormat;
|
|
204
|
+
/**
|
|
205
|
+
* Returns the translation load chain for a locale, or `undefined` if unsupported.
|
|
206
|
+
*/
|
|
207
|
+
getTargetLocale(locale: string): string[] | undefined;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export { MsgAttributes, MsgFormat, MsgInterface, type MsgLocalesSettings, MsgMessage, MsgMessageData, MsgNote, MsgProject, type MsgProjectData, type MsgProjectSettings, MsgResource, type MsgResourceData, type MsgTargetLocalesSettings, type MsgTranslationLoader, NoteTypes };
|