@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,40 +1,99 @@
|
|
|
1
1
|
import * as messageformat from 'messageformat';
|
|
2
2
|
import { MessageFormatOptions } from 'messageformat';
|
|
3
|
-
import { MsgInterface, MsgAttributes, MsgNote } from '../MsgInterface/MsgInterface.cjs';
|
|
3
|
+
import { MsgInterface, MsgAttributes, MsgNote, NoteTypes } from '../MsgInterface/MsgInterface.cjs';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Plain data used to create a {@link MsgMessage}.
|
|
7
|
+
*/
|
|
5
8
|
type MsgMessageData = {
|
|
9
|
+
/** Unique key that identifies this message within a resource. */
|
|
6
10
|
key: string;
|
|
11
|
+
/** The message string, possibly in MessageFormat syntax. */
|
|
7
12
|
value: string;
|
|
13
|
+
/** Optional locale and formatting metadata. */
|
|
8
14
|
attributes?: MsgAttributes;
|
|
15
|
+
/** Optional notes for translators and tooling. */
|
|
9
16
|
notes?: MsgNote[];
|
|
10
17
|
};
|
|
18
|
+
/**
|
|
19
|
+
* A single localizable message with a key, value, attributes, and notes.
|
|
20
|
+
*
|
|
21
|
+
* Supports MessageFormat 1, MessageFormat 2, and unformatted (`NONE`) strings.
|
|
22
|
+
*/
|
|
11
23
|
declare class MsgMessage implements MsgInterface {
|
|
12
24
|
private _key;
|
|
13
25
|
private _value;
|
|
14
26
|
private _mf?;
|
|
15
27
|
private _attributes;
|
|
16
28
|
private _notes;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a message from its parts. Prefer {@link MsgMessage.create}.
|
|
31
|
+
*/
|
|
17
32
|
private constructor();
|
|
33
|
+
/**
|
|
34
|
+
* Builds a message from {@link MsgMessageData}.
|
|
35
|
+
*/
|
|
18
36
|
static create(data: MsgMessageData): MsgMessage;
|
|
37
|
+
/** Unique key that identifies this message within a resource. */
|
|
19
38
|
get key(): string;
|
|
39
|
+
/** The message string, possibly in MessageFormat syntax. */
|
|
20
40
|
get value(): string;
|
|
41
|
+
/** Locale and formatting metadata for this message. */
|
|
21
42
|
get attributes(): MsgAttributes;
|
|
43
|
+
/** Notes attached to this message for translators and tooling. */
|
|
22
44
|
get notes(): MsgNote[];
|
|
23
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Appends a note to this message.
|
|
47
|
+
*
|
|
48
|
+
* @returns This message, for chaining.
|
|
49
|
+
*/
|
|
50
|
+
addNote(type: NoteTypes, content: string): this;
|
|
51
|
+
/**
|
|
52
|
+
* Appends a full note object to this message.
|
|
53
|
+
*
|
|
54
|
+
* @returns This message, for chaining.
|
|
55
|
+
*/
|
|
56
|
+
private setNote;
|
|
24
57
|
/**
|
|
25
58
|
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
26
59
|
*/
|
|
27
60
|
private resolveFormat;
|
|
61
|
+
/**
|
|
62
|
+
* Returns a cached MessageFormat formatter for this message's value.
|
|
63
|
+
*/
|
|
28
64
|
private getFormatter;
|
|
65
|
+
/**
|
|
66
|
+
* Formats the message with the given placeholder data.
|
|
67
|
+
*
|
|
68
|
+
* For `NONE` format messages, returns the raw value unchanged.
|
|
69
|
+
*/
|
|
29
70
|
format(data: Record<string, any>, options?: MessageFormatOptions): string;
|
|
71
|
+
/**
|
|
72
|
+
* Formats the message into structured parts with the given placeholder data.
|
|
73
|
+
*
|
|
74
|
+
* For `NONE` format messages, returns a single text part with the raw value.
|
|
75
|
+
*/
|
|
30
76
|
formatToParts(data: Record<string, any>, options?: MessageFormatOptions): messageformat.MessagePart<never>[];
|
|
77
|
+
/**
|
|
78
|
+
* Returns a plain data object for this message.
|
|
79
|
+
*
|
|
80
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
81
|
+
*/
|
|
31
82
|
getData(stripNotes?: boolean): {
|
|
32
83
|
key: string;
|
|
33
84
|
value: string;
|
|
34
85
|
attributes: MsgAttributes;
|
|
35
86
|
notes: MsgNote[] | undefined;
|
|
36
87
|
};
|
|
88
|
+
/**
|
|
89
|
+
* Returns the message value as a string.
|
|
90
|
+
*/
|
|
37
91
|
toString(): string;
|
|
92
|
+
/**
|
|
93
|
+
* Serializes this message to a formatted JSON string.
|
|
94
|
+
*
|
|
95
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
96
|
+
*/
|
|
38
97
|
toJSON(stripNotes?: boolean): string;
|
|
39
98
|
}
|
|
40
99
|
|
|
@@ -1,40 +1,99 @@
|
|
|
1
1
|
import * as messageformat from 'messageformat';
|
|
2
2
|
import { MessageFormatOptions } from 'messageformat';
|
|
3
|
-
import { MsgInterface, MsgAttributes, MsgNote } from '../MsgInterface/MsgInterface.js';
|
|
3
|
+
import { MsgInterface, MsgAttributes, MsgNote, NoteTypes } from '../MsgInterface/MsgInterface.js';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Plain data used to create a {@link MsgMessage}.
|
|
7
|
+
*/
|
|
5
8
|
type MsgMessageData = {
|
|
9
|
+
/** Unique key that identifies this message within a resource. */
|
|
6
10
|
key: string;
|
|
11
|
+
/** The message string, possibly in MessageFormat syntax. */
|
|
7
12
|
value: string;
|
|
13
|
+
/** Optional locale and formatting metadata. */
|
|
8
14
|
attributes?: MsgAttributes;
|
|
15
|
+
/** Optional notes for translators and tooling. */
|
|
9
16
|
notes?: MsgNote[];
|
|
10
17
|
};
|
|
18
|
+
/**
|
|
19
|
+
* A single localizable message with a key, value, attributes, and notes.
|
|
20
|
+
*
|
|
21
|
+
* Supports MessageFormat 1, MessageFormat 2, and unformatted (`NONE`) strings.
|
|
22
|
+
*/
|
|
11
23
|
declare class MsgMessage implements MsgInterface {
|
|
12
24
|
private _key;
|
|
13
25
|
private _value;
|
|
14
26
|
private _mf?;
|
|
15
27
|
private _attributes;
|
|
16
28
|
private _notes;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a message from its parts. Prefer {@link MsgMessage.create}.
|
|
31
|
+
*/
|
|
17
32
|
private constructor();
|
|
33
|
+
/**
|
|
34
|
+
* Builds a message from {@link MsgMessageData}.
|
|
35
|
+
*/
|
|
18
36
|
static create(data: MsgMessageData): MsgMessage;
|
|
37
|
+
/** Unique key that identifies this message within a resource. */
|
|
19
38
|
get key(): string;
|
|
39
|
+
/** The message string, possibly in MessageFormat syntax. */
|
|
20
40
|
get value(): string;
|
|
41
|
+
/** Locale and formatting metadata for this message. */
|
|
21
42
|
get attributes(): MsgAttributes;
|
|
43
|
+
/** Notes attached to this message for translators and tooling. */
|
|
22
44
|
get notes(): MsgNote[];
|
|
23
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Appends a note to this message.
|
|
47
|
+
*
|
|
48
|
+
* @returns This message, for chaining.
|
|
49
|
+
*/
|
|
50
|
+
addNote(type: NoteTypes, content: string): this;
|
|
51
|
+
/**
|
|
52
|
+
* Appends a full note object to this message.
|
|
53
|
+
*
|
|
54
|
+
* @returns This message, for chaining.
|
|
55
|
+
*/
|
|
56
|
+
private setNote;
|
|
24
57
|
/**
|
|
25
58
|
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
26
59
|
*/
|
|
27
60
|
private resolveFormat;
|
|
61
|
+
/**
|
|
62
|
+
* Returns a cached MessageFormat formatter for this message's value.
|
|
63
|
+
*/
|
|
28
64
|
private getFormatter;
|
|
65
|
+
/**
|
|
66
|
+
* Formats the message with the given placeholder data.
|
|
67
|
+
*
|
|
68
|
+
* For `NONE` format messages, returns the raw value unchanged.
|
|
69
|
+
*/
|
|
29
70
|
format(data: Record<string, any>, options?: MessageFormatOptions): string;
|
|
71
|
+
/**
|
|
72
|
+
* Formats the message into structured parts with the given placeholder data.
|
|
73
|
+
*
|
|
74
|
+
* For `NONE` format messages, returns a single text part with the raw value.
|
|
75
|
+
*/
|
|
30
76
|
formatToParts(data: Record<string, any>, options?: MessageFormatOptions): messageformat.MessagePart<never>[];
|
|
77
|
+
/**
|
|
78
|
+
* Returns a plain data object for this message.
|
|
79
|
+
*
|
|
80
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
81
|
+
*/
|
|
31
82
|
getData(stripNotes?: boolean): {
|
|
32
83
|
key: string;
|
|
33
84
|
value: string;
|
|
34
85
|
attributes: MsgAttributes;
|
|
35
86
|
notes: MsgNote[] | undefined;
|
|
36
87
|
};
|
|
88
|
+
/**
|
|
89
|
+
* Returns the message value as a string.
|
|
90
|
+
*/
|
|
37
91
|
toString(): string;
|
|
92
|
+
/**
|
|
93
|
+
* Serializes this message to a formatted JSON string.
|
|
94
|
+
*
|
|
95
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
96
|
+
*/
|
|
38
97
|
toJSON(stripNotes?: boolean): string;
|
|
39
98
|
}
|
|
40
99
|
|
|
@@ -41,24 +41,36 @@ var defaultLocalesSettings = {
|
|
|
41
41
|
}
|
|
42
42
|
};
|
|
43
43
|
var MsgProject = class _MsgProject {
|
|
44
|
+
/** Project name, version, and default format. */
|
|
44
45
|
_project;
|
|
46
|
+
/** Source, pseudo, and target locale settings. */
|
|
45
47
|
_locales;
|
|
48
|
+
/** Function used to load translations for a locale. */
|
|
46
49
|
_loader;
|
|
50
|
+
/**
|
|
51
|
+
* Builds a project from {@link MsgProjectData}.
|
|
52
|
+
*/
|
|
47
53
|
static create(data) {
|
|
48
54
|
const { project, locales, loader } = data;
|
|
49
55
|
return new _MsgProject(project, locales, loader);
|
|
50
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Creates a project from its settings and loader. Prefer {@link MsgProject.create}.
|
|
59
|
+
*/
|
|
51
60
|
constructor(projectSettings, localesSettings, loader) {
|
|
52
61
|
this._project = { ...defaultProjectSettings, ...projectSettings };
|
|
53
62
|
this._locales = { ...defaultLocalesSettings, ...localesSettings };
|
|
54
63
|
this._loader = loader;
|
|
55
64
|
}
|
|
65
|
+
/** Project name, version, and default format. */
|
|
56
66
|
get project() {
|
|
57
67
|
return this._project;
|
|
58
68
|
}
|
|
69
|
+
/** Source, pseudo, and target locale settings. */
|
|
59
70
|
get locales() {
|
|
60
71
|
return this._locales;
|
|
61
72
|
}
|
|
73
|
+
/** Function used to load translations for a locale. */
|
|
62
74
|
get loader() {
|
|
63
75
|
return this._loader;
|
|
64
76
|
}
|
|
@@ -69,6 +81,9 @@ var MsgProject = class _MsgProject {
|
|
|
69
81
|
get format() {
|
|
70
82
|
return this._project.format ?? MSG_DEFAULT_FORMAT;
|
|
71
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* Returns the translation load chain for a locale, or `undefined` if unsupported.
|
|
86
|
+
*/
|
|
72
87
|
getTargetLocale(locale) {
|
|
73
88
|
return this._locales.targetLocales[locale];
|
|
74
89
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { MsgLocalesSettings, MsgProject, MsgProjectData, MsgProjectSettings, MsgTargetLocalesSettings, MsgTranslationLoader } from '../index.cjs';
|
|
2
2
|
import '../MsgInterface/MsgInterface.cjs';
|
|
3
3
|
import '../MsgMessage/MsgMessage.cjs';
|
|
4
4
|
import 'messageformat';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { MsgLocalesSettings, MsgProject, MsgProjectData, MsgProjectSettings, MsgTargetLocalesSettings, MsgTranslationLoader } from '../index.js';
|
|
2
2
|
import '../MsgInterface/MsgInterface.js';
|
|
3
3
|
import '../MsgMessage/MsgMessage.js';
|
|
4
4
|
import 'messageformat';
|
|
@@ -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
|
}
|
|
@@ -119,6 +169,12 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
119
169
|
_notes = [];
|
|
120
170
|
_title;
|
|
121
171
|
_project;
|
|
172
|
+
/**
|
|
173
|
+
* Builds a resource from {@link MsgResourceData} and an owning project.
|
|
174
|
+
*
|
|
175
|
+
* When `messages` is provided, each entry is added to the resource.
|
|
176
|
+
* When omitted, the resource starts empty.
|
|
177
|
+
*/
|
|
122
178
|
static create(data, project) {
|
|
123
179
|
const { title, attributes, notes, messages } = data;
|
|
124
180
|
const res = new _MsgResource(title, attributes, project, notes);
|
|
@@ -132,23 +188,35 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
132
188
|
}
|
|
133
189
|
return res;
|
|
134
190
|
}
|
|
191
|
+
/**
|
|
192
|
+
* Creates a resource bound to a project. Prefer {@link MsgResource.create}.
|
|
193
|
+
*/
|
|
135
194
|
constructor(title, attributes, project, notes) {
|
|
136
195
|
super();
|
|
137
196
|
this._title = title;
|
|
138
197
|
this._attributes = { ...DEFAULT_ATTRIBUTES, format: project.format, ...attributes };
|
|
139
198
|
this._project = project;
|
|
140
199
|
if (notes) {
|
|
141
|
-
notes.forEach((note) => this.
|
|
200
|
+
notes.forEach((note) => this.setNote(note));
|
|
142
201
|
}
|
|
143
202
|
}
|
|
203
|
+
/**
|
|
204
|
+
* Returns true when the message's attributes match this resource's attributes.
|
|
205
|
+
*/
|
|
144
206
|
hasMatchingAttributes(message) {
|
|
145
207
|
const res = this.attributes;
|
|
146
208
|
const msg = message.attributes;
|
|
147
209
|
return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt && this.resolveFormat(res) === this.resolveFormat(msg);
|
|
148
210
|
}
|
|
211
|
+
/**
|
|
212
|
+
* Resolves a format from attributes, falling back to the library default.
|
|
213
|
+
*/
|
|
149
214
|
resolveFormat(attributes) {
|
|
150
215
|
return attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
151
216
|
}
|
|
217
|
+
/**
|
|
218
|
+
* Pseudo-localizes an MF2 message by localizing only literal text parts.
|
|
219
|
+
*/
|
|
152
220
|
pseudoLocalizeMF2(source, options) {
|
|
153
221
|
const msg = (0, import_messageformat2.parseMessage)(source);
|
|
154
222
|
(0, import_messageformat2.visit)(msg, {
|
|
@@ -163,30 +231,58 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
163
231
|
});
|
|
164
232
|
return (0, import_messageformat2.stringifyMessage)(msg);
|
|
165
233
|
}
|
|
234
|
+
/** Locale and formatting metadata for this resource. */
|
|
166
235
|
get attributes() {
|
|
167
236
|
return this._attributes;
|
|
168
237
|
}
|
|
238
|
+
/** Replaces the resource's locale and formatting metadata. */
|
|
169
239
|
set attributes(attributes) {
|
|
170
240
|
this._attributes = attributes;
|
|
171
241
|
}
|
|
242
|
+
/** Notes attached to this resource for translators and tooling. */
|
|
172
243
|
get notes() {
|
|
173
244
|
return this._notes;
|
|
174
245
|
}
|
|
246
|
+
/** Replaces the notes attached to this resource. */
|
|
175
247
|
set notes(notes) {
|
|
176
248
|
this._notes = notes;
|
|
177
249
|
}
|
|
178
|
-
|
|
250
|
+
/**
|
|
251
|
+
* Appends a note to this resource.
|
|
252
|
+
*
|
|
253
|
+
* @returns This resource, for chaining.
|
|
254
|
+
*/
|
|
255
|
+
addNote(type, content) {
|
|
256
|
+
return this.setNote({ type, content });
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Appends a full note object to this resource.
|
|
260
|
+
*
|
|
261
|
+
* @returns This resource, for chaining.
|
|
262
|
+
*/
|
|
263
|
+
setNote(note) {
|
|
179
264
|
this.notes.push(note);
|
|
265
|
+
return this;
|
|
180
266
|
}
|
|
267
|
+
/** Title that identifies this resource within a project. */
|
|
181
268
|
get title() {
|
|
182
269
|
return this._title;
|
|
183
270
|
}
|
|
271
|
+
/** Sets the title that identifies this resource within a project. */
|
|
184
272
|
set title(title) {
|
|
185
273
|
this._title = title;
|
|
186
274
|
}
|
|
275
|
+
/**
|
|
276
|
+
* Returns the project this resource belongs to.
|
|
277
|
+
*/
|
|
187
278
|
getProject() {
|
|
188
279
|
return this._project;
|
|
189
280
|
}
|
|
281
|
+
/**
|
|
282
|
+
* Adds a message to this resource, merging resource attributes with any overrides.
|
|
283
|
+
*
|
|
284
|
+
* @returns This resource, for chaining.
|
|
285
|
+
*/
|
|
190
286
|
add(key, value, attributes, notes) {
|
|
191
287
|
const merged = { ...this.attributes, ...attributes };
|
|
192
288
|
const msg = MsgMessage.create({
|
|
@@ -198,6 +294,14 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
198
294
|
this.set(key, msg);
|
|
199
295
|
return this;
|
|
200
296
|
}
|
|
297
|
+
/**
|
|
298
|
+
* Builds a translated copy of this resource from translation data.
|
|
299
|
+
*
|
|
300
|
+
* Messages present in the translation replace the source; missing keys keep
|
|
301
|
+
* the source message. Notes are carried over from the source.
|
|
302
|
+
*
|
|
303
|
+
* @throws {TypeError} When the translation title does not match this resource.
|
|
304
|
+
*/
|
|
201
305
|
translate(data) {
|
|
202
306
|
const { title, attributes, messages } = data;
|
|
203
307
|
if (title !== this.title) {
|
|
@@ -223,7 +327,7 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
223
327
|
});
|
|
224
328
|
const notes = this.get(key)?.notes || [];
|
|
225
329
|
notes.forEach((note) => {
|
|
226
|
-
msg.addNote(note);
|
|
330
|
+
msg.addNote(note.type, note.content);
|
|
227
331
|
});
|
|
228
332
|
translated.set(key, msg);
|
|
229
333
|
});
|
|
@@ -235,7 +339,18 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
235
339
|
}
|
|
236
340
|
return { ...attributes, format };
|
|
237
341
|
}
|
|
342
|
+
/**
|
|
343
|
+
* Loads and applies translations for a locale via the project loader.
|
|
344
|
+
*
|
|
345
|
+
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
346
|
+
* copy without calling the loader.
|
|
347
|
+
*
|
|
348
|
+
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
349
|
+
*/
|
|
238
350
|
async getTranslation(lang) {
|
|
351
|
+
if (!lang) {
|
|
352
|
+
return _MsgResource.create(this.getData(), this.getProject());
|
|
353
|
+
}
|
|
239
354
|
const project = this._project;
|
|
240
355
|
const pseudoLocale = project.locales.pseudoLocale;
|
|
241
356
|
if (lang === pseudoLocale) {
|
|
@@ -270,6 +385,14 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
270
385
|
}
|
|
271
386
|
return translated;
|
|
272
387
|
}
|
|
388
|
+
/**
|
|
389
|
+
* Returns a plain data object for this resource and its messages.
|
|
390
|
+
*
|
|
391
|
+
* Attributes that match the resource or project defaults are omitted where
|
|
392
|
+
* possible to keep the output compact.
|
|
393
|
+
*
|
|
394
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
395
|
+
*/
|
|
273
396
|
getData(stripNotes = false) {
|
|
274
397
|
const resourceFormat = this.resolveFormat(this.attributes);
|
|
275
398
|
const projectFormat = this._project.format;
|
|
@@ -302,6 +425,11 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
302
425
|
messages
|
|
303
426
|
};
|
|
304
427
|
}
|
|
428
|
+
/**
|
|
429
|
+
* Serializes this resource to a formatted JSON string.
|
|
430
|
+
*
|
|
431
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
432
|
+
*/
|
|
305
433
|
toJSON(stripNotes = false) {
|
|
306
434
|
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
307
435
|
}
|
|
@@ -1,79 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import '../MsgMessage/MsgMessage.cjs';
|
|
2
|
+
import '../MsgInterface/MsgInterface.cjs';
|
|
3
|
+
export { MsgResource, MsgResourceData } from '../index.cjs';
|
|
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 };
|