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