@worldware/msg 0.9.0 → 0.10.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.
@@ -1,201 +1,4 @@
1
- import { MsgAttributes, MsgNote, MsgInterface, MsgFormat } from '../MsgInterface/MsgInterface.js';
2
- import { MsgMessageData, MsgMessage } from '../MsgMessage/MsgMessage.js';
1
+ import '../MsgMessage/MsgMessage.js';
2
+ import '../MsgInterface/MsgInterface.js';
3
+ export { MsgResource, MsgResourceData } from '../index.js';
3
4
  import 'messageformat';
4
-
5
- /**
6
- * Plain data used to create or describe a {@link MsgResource}.
7
- */
8
- type MsgResourceData = {
9
- /** Title that identifies this resource within a project. */
10
- title: string;
11
- /** Locale and formatting metadata for the resource. */
12
- attributes: MsgAttributes;
13
- /** Optional notes for translators and tooling. */
14
- notes?: MsgNote[];
15
- /** Optional list of messages in this resource. */
16
- messages?: MsgMessageData[];
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
- */
24
- declare class MsgResource extends Map<string, MsgMessage> implements MsgInterface {
25
- private _attributes;
26
- private _notes;
27
- private _title;
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
- */
35
- static create(data: MsgResourceData, project: MsgProject): MsgResource;
36
- /**
37
- * Creates a resource bound to a project. Prefer {@link MsgResource.create}.
38
- */
39
- private constructor();
40
- /**
41
- * Returns true when the message's attributes match this resource's attributes.
42
- */
43
- private hasMatchingAttributes;
44
- /**
45
- * Resolves a format from attributes, falling back to the library default.
46
- */
47
- private resolveFormat;
48
- /**
49
- * Pseudo-localizes an MF2 message by localizing only literal text parts.
50
- */
51
- private pseudoLocalizeMF2;
52
- /** Locale and formatting metadata for this resource. */
53
- get attributes(): MsgAttributes;
54
- /** Replaces the resource's locale and formatting metadata. */
55
- set attributes(attributes: MsgAttributes);
56
- /** Notes attached to this resource for translators and tooling. */
57
- get notes(): MsgNote[];
58
- /** Replaces the notes attached to this resource. */
59
- set notes(notes: MsgNote[]);
60
- /**
61
- * Appends a note to this resource.
62
- */
63
- addNote(note: MsgNote): void;
64
- /** Title that identifies this resource within a project. */
65
- get title(): string;
66
- /** Sets the title that identifies this resource within a project. */
67
- set title(title: string);
68
- /**
69
- * Returns the project this resource belongs to.
70
- */
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
- */
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
- */
86
- translate(data: MsgResourceData): MsgResource;
87
- /**
88
- * Merge a `format` into an attribute set unless one is already present,
89
- * so translations inherit the source's format without overriding an
90
- * explicitly translated one. Returns the input unchanged when there is
91
- * no format to preserve.
92
- */
93
- private preserveFormat;
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
- */
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
- */
117
- toJSON(stripNotes?: boolean): string;
118
- }
119
-
120
- /**
121
- * Project-level settings such as name, version, and default message format.
122
- */
123
- type MsgProjectSettings = {
124
- /** Display name of the localization project. */
125
- name: string;
126
- /** Optional project version number. */
127
- version?: number;
128
- /** Default message format inherited by resources and messages. */
129
- format?: MsgFormat;
130
- };
131
- /**
132
- * Map of a requested locale to the chain of locales used to load translations.
133
- */
134
- type MsgTargetLocalesSettings = {
135
- [key: string]: string[];
136
- };
137
- /**
138
- * Locale configuration for a project: source, pseudo, and target locales.
139
- */
140
- type MsgLocalesSettings = {
141
- /** Locale of the source (authoring) strings. */
142
- sourceLocale: string;
143
- /** Locale used for pseudo-localization during development. */
144
- pseudoLocale: string;
145
- /** Target locales and their fallback load chains. */
146
- targetLocales: MsgTargetLocalesSettings;
147
- };
148
- /**
149
- * Async function that loads translated resource data for a project, title, and language.
150
- */
151
- type MsgTranslationLoader = (project: string, title: string, lang: string) => Promise<MsgResourceData>;
152
- /**
153
- * Plain data used to create a {@link MsgProject}.
154
- */
155
- type MsgProjectData = {
156
- /** Project name, version, and default format. */
157
- project: MsgProjectSettings;
158
- /** Source, pseudo, and target locale settings. */
159
- locales: MsgLocalesSettings;
160
- /** Function used to load translations for a locale. */
161
- loader: MsgTranslationLoader;
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
- */
169
- declare class MsgProject {
170
- /** Project name, version, and default format. */
171
- _project: MsgProjectSettings;
172
- /** Source, pseudo, and target locale settings. */
173
- _locales: MsgLocalesSettings;
174
- /** Function used to load translations for a locale. */
175
- _loader: MsgTranslationLoader;
176
- /**
177
- * Builds a project from {@link MsgProjectData}.
178
- */
179
- static create(data: MsgProjectData): MsgProject;
180
- /**
181
- * Creates a project from its settings and loader. Prefer {@link MsgProject.create}.
182
- */
183
- private constructor();
184
- /** Project name, version, and default format. */
185
- get project(): MsgProjectSettings;
186
- /** Source, pseudo, and target locale settings. */
187
- get locales(): MsgLocalesSettings;
188
- /** Function used to load translations for a locale. */
189
- get loader(): MsgTranslationLoader;
190
- /**
191
- * The project-wide default message format. Resources (and, through them,
192
- * messages) inherit this value unless they specify their own.
193
- */
194
- get format(): MsgFormat;
195
- /**
196
- * Returns the translation load chain for a locale, or `undefined` if unsupported.
197
- */
198
- getTargetLocale(locale: string): string[] | undefined;
199
- }
200
-
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 };
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  MsgResource
3
- } from "../../chunk-IMOOTZIK.mjs";
4
- import "../../chunk-F3VAAZJZ.mjs";
3
+ } from "../../chunk-HUWFAJAN.mjs";
4
+ import "../../chunk-OKEV7FY7.mjs";
5
5
  import "../../chunk-XQVALCKX.mjs";
6
6
  export {
7
7
  MsgResource
@@ -53,7 +53,7 @@ var MsgMessage = class _MsgMessage {
53
53
  this._value = value;
54
54
  this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
55
55
  if (notes) {
56
- notes.forEach((note) => this.addNote(note));
56
+ notes.forEach((note) => this.setNote(note));
57
57
  }
58
58
  }
59
59
  /**
@@ -82,9 +82,20 @@ var MsgMessage = class _MsgMessage {
82
82
  }
83
83
  /**
84
84
  * Appends a note to this message.
85
+ *
86
+ * @returns This message, for chaining.
85
87
  */
86
- addNote(note) {
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) {
87
97
  this.notes.push(note);
98
+ return this;
88
99
  }
89
100
  /**
90
101
  * The message's resolved format, defaulting to MF2 when not set or inherited.
@@ -188,7 +199,7 @@ var MsgResource = class _MsgResource extends Map {
188
199
  this._attributes = { ...DEFAULT_ATTRIBUTES, format: project.format, ...attributes };
189
200
  this._project = project;
190
201
  if (notes) {
191
- notes.forEach((note) => this.addNote(note));
202
+ notes.forEach((note) => this.setNote(note));
192
203
  }
193
204
  }
194
205
  /**
@@ -240,9 +251,20 @@ var MsgResource = class _MsgResource extends Map {
240
251
  }
241
252
  /**
242
253
  * Appends a note to this resource.
254
+ *
255
+ * @returns This resource, for chaining.
243
256
  */
244
- addNote(note) {
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) {
245
266
  this.notes.push(note);
267
+ return this;
246
268
  }
247
269
  /** Title that identifies this resource within a project. */
248
270
  get title() {
@@ -307,7 +329,7 @@ var MsgResource = class _MsgResource extends Map {
307
329
  });
308
330
  const notes = this.get(key)?.notes || [];
309
331
  notes.forEach((note) => {
310
- msg.addNote(note);
332
+ msg.addNote(note.type, note.content);
311
333
  });
312
334
  translated.set(key, msg);
313
335
  });
@@ -1,4 +1,210 @@
1
- export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, MsgAttributes, MsgFormat, MsgInterface, MsgNote, NoteTypes } from './MsgInterface/MsgInterface.cjs';
2
- export { MsgMessage, MsgMessageData } from './MsgMessage/MsgMessage.cjs';
3
- export { M as MsgLocalesSettings, a as MsgProject, b as MsgProjectData, c as MsgProjectSettings, MsgResource, MsgResourceData, d as MsgTargetLocalesSettings, e as MsgTranslationLoader } from './MsgResource/MsgResource.cjs';
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 };