@worldware/msg 0.7.1 → 0.8.1
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 +54 -10
- package/dist/{chunk-WUDKNZV2.mjs → chunk-HCR2UNSE.mjs} +22 -8
- package/dist/{chunk-XS43NAP2.mjs → chunk-NWJ7MVVV.mjs} +13 -1
- package/dist/{chunk-2QUOYESW.mjs → chunk-X6CRKSNU.mjs} +32 -9
- package/dist/{chunk-QWBDIKQK.mjs → chunk-XQVALCKX.mjs} +2 -0
- package/dist/classes/MsgInterface/MsgInterface.cjs +5 -2
- package/dist/classes/MsgInterface/MsgInterface.d.cts +15 -1
- package/dist/classes/MsgInterface/MsgInterface.d.ts +15 -1
- package/dist/classes/MsgInterface/MsgInterface.mjs +5 -3
- package/dist/classes/MsgMessage/MsgMessage.cjs +20 -6
- package/dist/classes/MsgMessage/MsgMessage.d.cts +6 -1
- package/dist/classes/MsgMessage/MsgMessage.d.ts +6 -1
- package/dist/classes/MsgMessage/MsgMessage.mjs +2 -2
- package/dist/classes/MsgProject/MsgProject.cjs +14 -1
- package/dist/classes/MsgProject/MsgProject.d.cts +3 -64
- package/dist/classes/MsgProject/MsgProject.d.ts +3 -64
- package/dist/classes/MsgProject/MsgProject.mjs +2 -1
- package/dist/classes/MsgResource/MsgResource.cjs +48 -12
- package/dist/classes/MsgResource/MsgResource.d.cts +78 -3
- package/dist/classes/MsgResource/MsgResource.d.ts +78 -3
- package/dist/classes/MsgResource/MsgResource.mjs +3 -3
- package/dist/classes/index.cjs +62 -16
- package/dist/classes/index.d.cts +2 -2
- package/dist/classes/index.d.ts +2 -2
- package/dist/classes/index.mjs +12 -7
- package/dist/index.cjs +62 -16
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +12 -7
- package/package.json +8 -3
- /package/dist/{chunk-ZKG5QRBZ.mjs → chunk-ORHP7HGX.mjs} +0 -0
|
@@ -1,65 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
1
|
+
export { M as MsgProject, a as MsgProjectData, b as MsgTranslationLoader } from '../MsgResource/MsgResource.js';
|
|
2
|
+
import '../MsgInterface/MsgInterface.js';
|
|
3
|
+
import '../MsgMessage/MsgMessage.js';
|
|
3
4
|
import 'messageformat';
|
|
4
|
-
|
|
5
|
-
type MsgProjectSettings = {
|
|
6
|
-
name: string;
|
|
7
|
-
version?: number;
|
|
8
|
-
};
|
|
9
|
-
type MsgTargetLocalesSettings = {
|
|
10
|
-
[key: string]: string[];
|
|
11
|
-
};
|
|
12
|
-
type MsgLocalesSettings = {
|
|
13
|
-
sourceLocale: string;
|
|
14
|
-
pseudoLocale: string;
|
|
15
|
-
targetLocales: MsgTargetLocalesSettings;
|
|
16
|
-
};
|
|
17
|
-
type MsgTranslationLoader = (project: string, title: string, lang: string) => Promise<MsgResourceData>;
|
|
18
|
-
type MsgProjectData = {
|
|
19
|
-
project: MsgProjectSettings;
|
|
20
|
-
locales: MsgLocalesSettings;
|
|
21
|
-
loader: MsgTranslationLoader;
|
|
22
|
-
};
|
|
23
|
-
declare class MsgProject {
|
|
24
|
-
_project: MsgProjectSettings;
|
|
25
|
-
_locales: MsgLocalesSettings;
|
|
26
|
-
_loader: MsgTranslationLoader;
|
|
27
|
-
static create(data: MsgProjectData): MsgProject;
|
|
28
|
-
private constructor();
|
|
29
|
-
get project(): MsgProjectSettings;
|
|
30
|
-
get locales(): MsgLocalesSettings;
|
|
31
|
-
get loader(): MsgTranslationLoader;
|
|
32
|
-
getTargetLocale(locale: string): string[] | undefined;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
type MsgResourceData = {
|
|
36
|
-
title: string;
|
|
37
|
-
attributes: MsgAttributes;
|
|
38
|
-
notes?: MsgNote[];
|
|
39
|
-
messages?: MsgMessageData[];
|
|
40
|
-
};
|
|
41
|
-
declare class MsgResource extends Map<string, MsgMessage> implements MsgInterface {
|
|
42
|
-
private _attributes;
|
|
43
|
-
private _notes;
|
|
44
|
-
private _title;
|
|
45
|
-
private _project;
|
|
46
|
-
static create(data: MsgResourceData, project: MsgProject): MsgResource;
|
|
47
|
-
private constructor();
|
|
48
|
-
private hasMatchingAttributes;
|
|
49
|
-
private pseudoLocalizeMF2;
|
|
50
|
-
get attributes(): MsgAttributes;
|
|
51
|
-
set attributes(attributes: MsgAttributes);
|
|
52
|
-
get notes(): MsgNote[];
|
|
53
|
-
set notes(notes: MsgNote[]);
|
|
54
|
-
addNote(note: MsgNote): void;
|
|
55
|
-
get title(): string;
|
|
56
|
-
set title(title: string);
|
|
57
|
-
getProject(): MsgProject;
|
|
58
|
-
add(key: string, value: string, attributes?: MsgAttributes, notes?: MsgNote[]): this;
|
|
59
|
-
translate(data: MsgResourceData): MsgResource;
|
|
60
|
-
getTranslation(lang: string): Promise<MsgResource>;
|
|
61
|
-
getData(stripNotes?: boolean): MsgResourceData;
|
|
62
|
-
toJSON(stripNotes?: boolean): string;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export { type MsgResourceData as M, MsgProject, type MsgProjectData, type MsgTranslationLoader, MsgResource as a };
|
|
@@ -28,8 +28,10 @@ var import_pseudo_localization = require("pseudo-localization");
|
|
|
28
28
|
|
|
29
29
|
// src/classes/MsgMessage/MsgMessage.ts
|
|
30
30
|
var import_messageformat = require("messageformat");
|
|
31
|
+
var import_icu_messageformat_1 = require("@messageformat/icu-messageformat-1");
|
|
31
32
|
|
|
32
33
|
// src/classes/MsgInterface/MsgInterface.ts
|
|
34
|
+
var MSG_DEFAULT_FORMAT = "MF2";
|
|
33
35
|
var DEFAULT_ATTRIBUTES = {
|
|
34
36
|
lang: "und",
|
|
35
37
|
dir: "auto",
|
|
@@ -71,17 +73,29 @@ var MsgMessage = class _MsgMessage {
|
|
|
71
73
|
addNote(note) {
|
|
72
74
|
this.notes.push(note);
|
|
73
75
|
}
|
|
74
|
-
|
|
76
|
+
/**
|
|
77
|
+
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
78
|
+
*/
|
|
79
|
+
resolveFormat() {
|
|
80
|
+
return this.attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
81
|
+
}
|
|
82
|
+
getFormatter(options) {
|
|
75
83
|
if (!this._mf) {
|
|
76
|
-
this._mf = new import_messageformat.MessageFormat(this.attributes.lang, this.value, options);
|
|
84
|
+
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
|
+
}
|
|
86
|
+
return this._mf;
|
|
87
|
+
}
|
|
88
|
+
format(data, options) {
|
|
89
|
+
if (this.resolveFormat() === "NONE") {
|
|
90
|
+
return this.value;
|
|
77
91
|
}
|
|
78
|
-
return this.
|
|
92
|
+
return this.getFormatter(options).format(data);
|
|
79
93
|
}
|
|
80
94
|
formatToParts(data, options) {
|
|
81
|
-
if (
|
|
82
|
-
|
|
95
|
+
if (this.resolveFormat() === "NONE") {
|
|
96
|
+
return [{ type: "text", value: this.value }];
|
|
83
97
|
}
|
|
84
|
-
return this.
|
|
98
|
+
return this.getFormatter(options).formatToParts(data);
|
|
85
99
|
}
|
|
86
100
|
getData(stripNotes = false) {
|
|
87
101
|
return {
|
|
@@ -121,7 +135,7 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
121
135
|
constructor(title, attributes, project, notes) {
|
|
122
136
|
super();
|
|
123
137
|
this._title = title;
|
|
124
|
-
this._attributes = { ...DEFAULT_ATTRIBUTES, ...attributes };
|
|
138
|
+
this._attributes = { ...DEFAULT_ATTRIBUTES, format: project.format, ...attributes };
|
|
125
139
|
this._project = project;
|
|
126
140
|
if (notes) {
|
|
127
141
|
notes.forEach((note) => this.addNote(note));
|
|
@@ -130,7 +144,10 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
130
144
|
hasMatchingAttributes(message) {
|
|
131
145
|
const res = this.attributes;
|
|
132
146
|
const msg = message.attributes;
|
|
133
|
-
return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt;
|
|
147
|
+
return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt && this.resolveFormat(res) === this.resolveFormat(msg);
|
|
148
|
+
}
|
|
149
|
+
resolveFormat(attributes) {
|
|
150
|
+
return attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
134
151
|
}
|
|
135
152
|
pseudoLocalizeMF2(source, options) {
|
|
136
153
|
const msg = (0, import_messageformat2.parseMessage)(source);
|
|
@@ -188,7 +205,8 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
188
205
|
}
|
|
189
206
|
const translated = _MsgResource.create({
|
|
190
207
|
title,
|
|
191
|
-
|
|
208
|
+
// preserve the source resource's format unless the translation overrides it
|
|
209
|
+
attributes: this.preserveFormat(attributes, this.attributes.format),
|
|
192
210
|
notes: this.notes
|
|
193
211
|
// transfer the notes
|
|
194
212
|
}, this._project);
|
|
@@ -200,7 +218,8 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
200
218
|
const msg = MsgMessage.create({
|
|
201
219
|
key,
|
|
202
220
|
value,
|
|
203
|
-
|
|
221
|
+
// preserve the source message's format unless the translation overrides it
|
|
222
|
+
attributes: this.preserveFormat(attributes2, this.get(key)?.attributes.format)
|
|
204
223
|
});
|
|
205
224
|
const notes = this.get(key)?.notes || [];
|
|
206
225
|
notes.forEach((note) => {
|
|
@@ -210,6 +229,12 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
210
229
|
});
|
|
211
230
|
return translated;
|
|
212
231
|
}
|
|
232
|
+
preserveFormat(attributes, format) {
|
|
233
|
+
if (format === void 0 || attributes?.format !== void 0) {
|
|
234
|
+
return attributes;
|
|
235
|
+
}
|
|
236
|
+
return { ...attributes, format };
|
|
237
|
+
}
|
|
213
238
|
async getTranslation(lang) {
|
|
214
239
|
const project = this._project;
|
|
215
240
|
const pseudoLocale = project.locales.pseudoLocale;
|
|
@@ -246,6 +271,8 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
246
271
|
return translated;
|
|
247
272
|
}
|
|
248
273
|
getData(stripNotes = false) {
|
|
274
|
+
const resourceFormat = this.resolveFormat(this.attributes);
|
|
275
|
+
const projectFormat = this._project.format;
|
|
249
276
|
const messages = [];
|
|
250
277
|
this.forEach((msg) => {
|
|
251
278
|
if (this.hasMatchingAttributes(msg)) {
|
|
@@ -256,12 +283,21 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
256
283
|
};
|
|
257
284
|
messages.push(data);
|
|
258
285
|
} else {
|
|
259
|
-
|
|
286
|
+
const data = msg.getData(stripNotes);
|
|
287
|
+
if (data.attributes && this.resolveFormat(data.attributes) === resourceFormat) {
|
|
288
|
+
const { format, ...rest } = data.attributes;
|
|
289
|
+
data.attributes = rest;
|
|
290
|
+
}
|
|
291
|
+
messages.push(data);
|
|
260
292
|
}
|
|
261
293
|
});
|
|
294
|
+
const attributes = { ...this.attributes };
|
|
295
|
+
if (this.resolveFormat(attributes) === projectFormat) {
|
|
296
|
+
delete attributes.format;
|
|
297
|
+
}
|
|
262
298
|
return {
|
|
263
299
|
title: this.title,
|
|
264
|
-
attributes
|
|
300
|
+
attributes,
|
|
265
301
|
notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0,
|
|
266
302
|
messages
|
|
267
303
|
};
|
|
@@ -1,4 +1,79 @@
|
|
|
1
|
-
import '../
|
|
2
|
-
import '../
|
|
3
|
-
export { a as MsgResource, M as MsgResourceData } from '../MsgProject/MsgProject.cjs';
|
|
1
|
+
import { MsgAttributes, MsgNote, MsgInterface, MsgFormat } from '../MsgInterface/MsgInterface.cjs';
|
|
2
|
+
import { MsgMessageData, MsgMessage } from '../MsgMessage/MsgMessage.cjs';
|
|
4
3
|
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 };
|
|
@@ -1,4 +1,79 @@
|
|
|
1
|
-
import '../
|
|
2
|
-
import '../
|
|
3
|
-
export { a as MsgResource, M as MsgResourceData } from '../MsgProject/MsgProject.js';
|
|
1
|
+
import { MsgAttributes, MsgNote, MsgInterface, MsgFormat } from '../MsgInterface/MsgInterface.js';
|
|
2
|
+
import { MsgMessageData, MsgMessage } from '../MsgMessage/MsgMessage.js';
|
|
4
3
|
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 };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MsgResource
|
|
3
|
-
} from "../../chunk-
|
|
4
|
-
import "../../chunk-
|
|
5
|
-
import "../../chunk-
|
|
3
|
+
} from "../../chunk-X6CRKSNU.mjs";
|
|
4
|
+
import "../../chunk-HCR2UNSE.mjs";
|
|
5
|
+
import "../../chunk-XQVALCKX.mjs";
|
|
6
6
|
export {
|
|
7
7
|
MsgResource
|
|
8
8
|
};
|
package/dist/classes/index.cjs
CHANGED
|
@@ -20,16 +20,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/classes/index.ts
|
|
21
21
|
var classes_exports = {};
|
|
22
22
|
__export(classes_exports, {
|
|
23
|
+
DEFAULT_ATTRIBUTES: () => DEFAULT_ATTRIBUTES,
|
|
24
|
+
MSG_DEFAULT_FORMAT: () => MSG_DEFAULT_FORMAT,
|
|
23
25
|
MsgMessage: () => MsgMessage,
|
|
24
26
|
MsgProject: () => MsgProject,
|
|
25
27
|
MsgResource: () => MsgResource
|
|
26
28
|
});
|
|
27
29
|
module.exports = __toCommonJS(classes_exports);
|
|
28
30
|
|
|
29
|
-
// src/classes/MsgMessage/MsgMessage.ts
|
|
30
|
-
var import_messageformat = require("messageformat");
|
|
31
|
-
|
|
32
31
|
// src/classes/MsgInterface/MsgInterface.ts
|
|
32
|
+
var MSG_DEFAULT_FORMAT = "MF2";
|
|
33
33
|
var DEFAULT_ATTRIBUTES = {
|
|
34
34
|
lang: "und",
|
|
35
35
|
dir: "auto",
|
|
@@ -37,6 +37,8 @@ var DEFAULT_ATTRIBUTES = {
|
|
|
37
37
|
};
|
|
38
38
|
|
|
39
39
|
// src/classes/MsgMessage/MsgMessage.ts
|
|
40
|
+
var import_messageformat = require("messageformat");
|
|
41
|
+
var import_icu_messageformat_1 = require("@messageformat/icu-messageformat-1");
|
|
40
42
|
var MsgMessage = class _MsgMessage {
|
|
41
43
|
_key;
|
|
42
44
|
_value;
|
|
@@ -71,17 +73,29 @@ var MsgMessage = class _MsgMessage {
|
|
|
71
73
|
addNote(note) {
|
|
72
74
|
this.notes.push(note);
|
|
73
75
|
}
|
|
74
|
-
|
|
76
|
+
/**
|
|
77
|
+
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
78
|
+
*/
|
|
79
|
+
resolveFormat() {
|
|
80
|
+
return this.attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
81
|
+
}
|
|
82
|
+
getFormatter(options) {
|
|
75
83
|
if (!this._mf) {
|
|
76
|
-
this._mf = new import_messageformat.MessageFormat(this.attributes.lang, this.value, options);
|
|
84
|
+
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);
|
|
77
85
|
}
|
|
78
|
-
return this._mf
|
|
86
|
+
return this._mf;
|
|
87
|
+
}
|
|
88
|
+
format(data, options) {
|
|
89
|
+
if (this.resolveFormat() === "NONE") {
|
|
90
|
+
return this.value;
|
|
91
|
+
}
|
|
92
|
+
return this.getFormatter(options).format(data);
|
|
79
93
|
}
|
|
80
94
|
formatToParts(data, options) {
|
|
81
|
-
if (
|
|
82
|
-
|
|
95
|
+
if (this.resolveFormat() === "NONE") {
|
|
96
|
+
return [{ type: "text", value: this.value }];
|
|
83
97
|
}
|
|
84
|
-
return this.
|
|
98
|
+
return this.getFormatter(options).formatToParts(data);
|
|
85
99
|
}
|
|
86
100
|
getData(stripNotes = false) {
|
|
87
101
|
return {
|
|
@@ -123,7 +137,7 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
123
137
|
constructor(title, attributes, project, notes) {
|
|
124
138
|
super();
|
|
125
139
|
this._title = title;
|
|
126
|
-
this._attributes = { ...DEFAULT_ATTRIBUTES, ...attributes };
|
|
140
|
+
this._attributes = { ...DEFAULT_ATTRIBUTES, format: project.format, ...attributes };
|
|
127
141
|
this._project = project;
|
|
128
142
|
if (notes) {
|
|
129
143
|
notes.forEach((note) => this.addNote(note));
|
|
@@ -132,7 +146,10 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
132
146
|
hasMatchingAttributes(message) {
|
|
133
147
|
const res = this.attributes;
|
|
134
148
|
const msg = message.attributes;
|
|
135
|
-
return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt;
|
|
149
|
+
return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt && this.resolveFormat(res) === this.resolveFormat(msg);
|
|
150
|
+
}
|
|
151
|
+
resolveFormat(attributes) {
|
|
152
|
+
return attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
136
153
|
}
|
|
137
154
|
pseudoLocalizeMF2(source, options) {
|
|
138
155
|
const msg = (0, import_messageformat2.parseMessage)(source);
|
|
@@ -190,7 +207,8 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
190
207
|
}
|
|
191
208
|
const translated = _MsgResource.create({
|
|
192
209
|
title,
|
|
193
|
-
|
|
210
|
+
// preserve the source resource's format unless the translation overrides it
|
|
211
|
+
attributes: this.preserveFormat(attributes, this.attributes.format),
|
|
194
212
|
notes: this.notes
|
|
195
213
|
// transfer the notes
|
|
196
214
|
}, this._project);
|
|
@@ -202,7 +220,8 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
202
220
|
const msg = MsgMessage.create({
|
|
203
221
|
key,
|
|
204
222
|
value,
|
|
205
|
-
|
|
223
|
+
// preserve the source message's format unless the translation overrides it
|
|
224
|
+
attributes: this.preserveFormat(attributes2, this.get(key)?.attributes.format)
|
|
206
225
|
});
|
|
207
226
|
const notes = this.get(key)?.notes || [];
|
|
208
227
|
notes.forEach((note) => {
|
|
@@ -212,6 +231,12 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
212
231
|
});
|
|
213
232
|
return translated;
|
|
214
233
|
}
|
|
234
|
+
preserveFormat(attributes, format) {
|
|
235
|
+
if (format === void 0 || attributes?.format !== void 0) {
|
|
236
|
+
return attributes;
|
|
237
|
+
}
|
|
238
|
+
return { ...attributes, format };
|
|
239
|
+
}
|
|
215
240
|
async getTranslation(lang) {
|
|
216
241
|
const project = this._project;
|
|
217
242
|
const pseudoLocale = project.locales.pseudoLocale;
|
|
@@ -248,6 +273,8 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
248
273
|
return translated;
|
|
249
274
|
}
|
|
250
275
|
getData(stripNotes = false) {
|
|
276
|
+
const resourceFormat = this.resolveFormat(this.attributes);
|
|
277
|
+
const projectFormat = this._project.format;
|
|
251
278
|
const messages = [];
|
|
252
279
|
this.forEach((msg) => {
|
|
253
280
|
if (this.hasMatchingAttributes(msg)) {
|
|
@@ -258,12 +285,21 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
258
285
|
};
|
|
259
286
|
messages.push(data);
|
|
260
287
|
} else {
|
|
261
|
-
|
|
288
|
+
const data = msg.getData(stripNotes);
|
|
289
|
+
if (data.attributes && this.resolveFormat(data.attributes) === resourceFormat) {
|
|
290
|
+
const { format, ...rest } = data.attributes;
|
|
291
|
+
data.attributes = rest;
|
|
292
|
+
}
|
|
293
|
+
messages.push(data);
|
|
262
294
|
}
|
|
263
295
|
});
|
|
296
|
+
const attributes = { ...this.attributes };
|
|
297
|
+
if (this.resolveFormat(attributes) === projectFormat) {
|
|
298
|
+
delete attributes.format;
|
|
299
|
+
}
|
|
264
300
|
return {
|
|
265
301
|
title: this.title,
|
|
266
|
-
attributes
|
|
302
|
+
attributes,
|
|
267
303
|
notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0,
|
|
268
304
|
messages
|
|
269
305
|
};
|
|
@@ -276,7 +312,8 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
276
312
|
// src/classes/MsgProject/MsgProject.ts
|
|
277
313
|
var defaultProjectSettings = {
|
|
278
314
|
name: "messages",
|
|
279
|
-
version: 1
|
|
315
|
+
version: 1,
|
|
316
|
+
format: MSG_DEFAULT_FORMAT
|
|
280
317
|
};
|
|
281
318
|
var defaultLocalesSettings = {
|
|
282
319
|
sourceLocale: "en",
|
|
@@ -307,12 +344,21 @@ var MsgProject = class _MsgProject {
|
|
|
307
344
|
get loader() {
|
|
308
345
|
return this._loader;
|
|
309
346
|
}
|
|
347
|
+
/**
|
|
348
|
+
* The project-wide default message format. Resources (and, through them,
|
|
349
|
+
* messages) inherit this value unless they specify their own.
|
|
350
|
+
*/
|
|
351
|
+
get format() {
|
|
352
|
+
return this._project.format ?? MSG_DEFAULT_FORMAT;
|
|
353
|
+
}
|
|
310
354
|
getTargetLocale(locale) {
|
|
311
355
|
return this._locales.targetLocales[locale];
|
|
312
356
|
}
|
|
313
357
|
};
|
|
314
358
|
// Annotate the CommonJS export names for ESM import in node:
|
|
315
359
|
0 && (module.exports = {
|
|
360
|
+
DEFAULT_ATTRIBUTES,
|
|
361
|
+
MSG_DEFAULT_FORMAT,
|
|
316
362
|
MsgMessage,
|
|
317
363
|
MsgProject,
|
|
318
364
|
MsgResource
|
package/dist/classes/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
+
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, MsgAttributes, MsgFormat, MsgInterface, MsgNote } from './MsgInterface/MsgInterface.cjs';
|
|
1
2
|
export { MsgMessage, MsgMessageData } from './MsgMessage/MsgMessage.cjs';
|
|
2
|
-
export { MsgProject,
|
|
3
|
+
export { M as MsgProject, a as MsgProjectData, MsgResource, MsgResourceData, b as MsgTranslationLoader } from './MsgResource/MsgResource.cjs';
|
|
3
4
|
import 'messageformat';
|
|
4
|
-
import './MsgInterface/MsgInterface.cjs';
|
package/dist/classes/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
+
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, MsgAttributes, MsgFormat, MsgInterface, MsgNote } from './MsgInterface/MsgInterface.js';
|
|
1
2
|
export { MsgMessage, MsgMessageData } from './MsgMessage/MsgMessage.js';
|
|
2
|
-
export { MsgProject,
|
|
3
|
+
export { M as MsgProject, a as MsgProjectData, MsgResource, MsgResourceData, b as MsgTranslationLoader } from './MsgResource/MsgResource.js';
|
|
3
4
|
import 'messageformat';
|
|
4
|
-
import './MsgInterface/MsgInterface.js';
|
package/dist/classes/index.mjs
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
|
-
import "../chunk-
|
|
2
|
-
import {
|
|
3
|
-
MsgProject
|
|
4
|
-
} from "../chunk-XS43NAP2.mjs";
|
|
1
|
+
import "../chunk-ORHP7HGX.mjs";
|
|
5
2
|
import {
|
|
6
3
|
MsgResource
|
|
7
|
-
} from "../chunk-
|
|
4
|
+
} from "../chunk-X6CRKSNU.mjs";
|
|
8
5
|
import {
|
|
9
6
|
MsgMessage
|
|
10
|
-
} from "../chunk-
|
|
11
|
-
import
|
|
7
|
+
} from "../chunk-HCR2UNSE.mjs";
|
|
8
|
+
import {
|
|
9
|
+
MsgProject
|
|
10
|
+
} from "../chunk-NWJ7MVVV.mjs";
|
|
11
|
+
import {
|
|
12
|
+
DEFAULT_ATTRIBUTES,
|
|
13
|
+
MSG_DEFAULT_FORMAT
|
|
14
|
+
} from "../chunk-XQVALCKX.mjs";
|
|
12
15
|
export {
|
|
16
|
+
DEFAULT_ATTRIBUTES,
|
|
17
|
+
MSG_DEFAULT_FORMAT,
|
|
13
18
|
MsgMessage,
|
|
14
19
|
MsgProject,
|
|
15
20
|
MsgResource
|