@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
package/dist/classes/index.d.ts
CHANGED
|
@@ -1,4 +1,210 @@
|
|
|
1
|
-
|
|
2
|
-
export {
|
|
3
|
-
|
|
1
|
+
import { MsgAttributes, MsgNote, MsgInterface, NoteTypes, MsgFormat } from './MsgInterface/MsgInterface.js';
|
|
2
|
+
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT } from './MsgInterface/MsgInterface.js';
|
|
3
|
+
import { MsgMessageData, MsgMessage } from './MsgMessage/MsgMessage.js';
|
|
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 };
|
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-HUWFAJAN.mjs";
|
|
5
8
|
import {
|
|
6
9
|
MsgMessage
|
|
7
|
-
} from "../chunk-
|
|
8
|
-
import {
|
|
9
|
-
MsgProject
|
|
10
|
-
} from "../chunk-NWJ7MVVV.mjs";
|
|
10
|
+
} from "../chunk-OKEV7FY7.mjs";
|
|
11
11
|
import {
|
|
12
12
|
DEFAULT_ATTRIBUTES,
|
|
13
13
|
MSG_DEFAULT_FORMAT
|
package/dist/index.cjs
CHANGED
|
@@ -24,7 +24,9 @@ __export(index_exports, {
|
|
|
24
24
|
MSG_DEFAULT_FORMAT: () => MSG_DEFAULT_FORMAT,
|
|
25
25
|
MsgMessage: () => MsgMessage,
|
|
26
26
|
MsgProject: () => MsgProject,
|
|
27
|
-
MsgResource: () => MsgResource
|
|
27
|
+
MsgResource: () => MsgResource,
|
|
28
|
+
getLang: () => getLang,
|
|
29
|
+
setLang: () => setLang
|
|
28
30
|
});
|
|
29
31
|
module.exports = __toCommonJS(index_exports);
|
|
30
32
|
|
|
@@ -45,33 +47,57 @@ var MsgMessage = class _MsgMessage {
|
|
|
45
47
|
_mf;
|
|
46
48
|
_attributes;
|
|
47
49
|
_notes = [];
|
|
50
|
+
/**
|
|
51
|
+
* Creates a message from its parts. Prefer {@link MsgMessage.create}.
|
|
52
|
+
*/
|
|
48
53
|
constructor(key, value, attributes, notes) {
|
|
49
54
|
this._key = key;
|
|
50
55
|
this._value = value;
|
|
51
56
|
this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
|
|
52
57
|
if (notes) {
|
|
53
|
-
notes.forEach((note) => this.
|
|
58
|
+
notes.forEach((note) => this.setNote(note));
|
|
54
59
|
}
|
|
55
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Builds a message from {@link MsgMessageData}.
|
|
63
|
+
*/
|
|
56
64
|
static create(data) {
|
|
57
65
|
const { key, value, attributes, notes } = data;
|
|
58
66
|
const message = new _MsgMessage(key, value, attributes, notes);
|
|
59
67
|
return message;
|
|
60
68
|
}
|
|
69
|
+
/** Unique key that identifies this message within a resource. */
|
|
61
70
|
get key() {
|
|
62
71
|
return this._key;
|
|
63
72
|
}
|
|
73
|
+
/** The message string, possibly in MessageFormat syntax. */
|
|
64
74
|
get value() {
|
|
65
75
|
return this._value;
|
|
66
76
|
}
|
|
77
|
+
/** Locale and formatting metadata for this message. */
|
|
67
78
|
get attributes() {
|
|
68
79
|
return this._attributes;
|
|
69
80
|
}
|
|
81
|
+
/** Notes attached to this message for translators and tooling. */
|
|
70
82
|
get notes() {
|
|
71
83
|
return this._notes;
|
|
72
84
|
}
|
|
73
|
-
|
|
85
|
+
/**
|
|
86
|
+
* Appends a note to this message.
|
|
87
|
+
*
|
|
88
|
+
* @returns This message, for chaining.
|
|
89
|
+
*/
|
|
90
|
+
addNote(type, content) {
|
|
91
|
+
return this.setNote({ type, content });
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Appends a full note object to this message.
|
|
95
|
+
*
|
|
96
|
+
* @returns This message, for chaining.
|
|
97
|
+
*/
|
|
98
|
+
setNote(note) {
|
|
74
99
|
this.notes.push(note);
|
|
100
|
+
return this;
|
|
75
101
|
}
|
|
76
102
|
/**
|
|
77
103
|
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
@@ -79,24 +105,42 @@ var MsgMessage = class _MsgMessage {
|
|
|
79
105
|
resolveFormat() {
|
|
80
106
|
return this.attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
81
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* Returns a cached MessageFormat formatter for this message's value.
|
|
110
|
+
*/
|
|
82
111
|
getFormatter(options) {
|
|
83
112
|
if (!this._mf) {
|
|
84
113
|
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
114
|
}
|
|
86
115
|
return this._mf;
|
|
87
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* Formats the message with the given placeholder data.
|
|
119
|
+
*
|
|
120
|
+
* For `NONE` format messages, returns the raw value unchanged.
|
|
121
|
+
*/
|
|
88
122
|
format(data, options) {
|
|
89
123
|
if (this.resolveFormat() === "NONE") {
|
|
90
124
|
return this.value;
|
|
91
125
|
}
|
|
92
126
|
return this.getFormatter(options).format(data);
|
|
93
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* Formats the message into structured parts with the given placeholder data.
|
|
130
|
+
*
|
|
131
|
+
* For `NONE` format messages, returns a single text part with the raw value.
|
|
132
|
+
*/
|
|
94
133
|
formatToParts(data, options) {
|
|
95
134
|
if (this.resolveFormat() === "NONE") {
|
|
96
135
|
return [{ type: "text", value: this.value }];
|
|
97
136
|
}
|
|
98
137
|
return this.getFormatter(options).formatToParts(data);
|
|
99
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Returns a plain data object for this message.
|
|
141
|
+
*
|
|
142
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
143
|
+
*/
|
|
100
144
|
getData(stripNotes = false) {
|
|
101
145
|
return {
|
|
102
146
|
key: this.key,
|
|
@@ -105,9 +149,17 @@ var MsgMessage = class _MsgMessage {
|
|
|
105
149
|
notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0
|
|
106
150
|
};
|
|
107
151
|
}
|
|
152
|
+
/**
|
|
153
|
+
* Returns the message value as a string.
|
|
154
|
+
*/
|
|
108
155
|
toString() {
|
|
109
156
|
return this.value;
|
|
110
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* Serializes this message to a formatted JSON string.
|
|
160
|
+
*
|
|
161
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
162
|
+
*/
|
|
111
163
|
toJSON(stripNotes = false) {
|
|
112
164
|
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
113
165
|
}
|
|
@@ -121,6 +173,12 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
121
173
|
_notes = [];
|
|
122
174
|
_title;
|
|
123
175
|
_project;
|
|
176
|
+
/**
|
|
177
|
+
* Builds a resource from {@link MsgResourceData} and an owning project.
|
|
178
|
+
*
|
|
179
|
+
* When `messages` is provided, each entry is added to the resource.
|
|
180
|
+
* When omitted, the resource starts empty.
|
|
181
|
+
*/
|
|
124
182
|
static create(data, project) {
|
|
125
183
|
const { title, attributes, notes, messages } = data;
|
|
126
184
|
const res = new _MsgResource(title, attributes, project, notes);
|
|
@@ -134,23 +192,35 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
134
192
|
}
|
|
135
193
|
return res;
|
|
136
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* Creates a resource bound to a project. Prefer {@link MsgResource.create}.
|
|
197
|
+
*/
|
|
137
198
|
constructor(title, attributes, project, notes) {
|
|
138
199
|
super();
|
|
139
200
|
this._title = title;
|
|
140
201
|
this._attributes = { ...DEFAULT_ATTRIBUTES, format: project.format, ...attributes };
|
|
141
202
|
this._project = project;
|
|
142
203
|
if (notes) {
|
|
143
|
-
notes.forEach((note) => this.
|
|
204
|
+
notes.forEach((note) => this.setNote(note));
|
|
144
205
|
}
|
|
145
206
|
}
|
|
207
|
+
/**
|
|
208
|
+
* Returns true when the message's attributes match this resource's attributes.
|
|
209
|
+
*/
|
|
146
210
|
hasMatchingAttributes(message) {
|
|
147
211
|
const res = this.attributes;
|
|
148
212
|
const msg = message.attributes;
|
|
149
213
|
return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt && this.resolveFormat(res) === this.resolveFormat(msg);
|
|
150
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* Resolves a format from attributes, falling back to the library default.
|
|
217
|
+
*/
|
|
151
218
|
resolveFormat(attributes) {
|
|
152
219
|
return attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
153
220
|
}
|
|
221
|
+
/**
|
|
222
|
+
* Pseudo-localizes an MF2 message by localizing only literal text parts.
|
|
223
|
+
*/
|
|
154
224
|
pseudoLocalizeMF2(source, options) {
|
|
155
225
|
const msg = (0, import_messageformat2.parseMessage)(source);
|
|
156
226
|
(0, import_messageformat2.visit)(msg, {
|
|
@@ -165,30 +235,58 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
165
235
|
});
|
|
166
236
|
return (0, import_messageformat2.stringifyMessage)(msg);
|
|
167
237
|
}
|
|
238
|
+
/** Locale and formatting metadata for this resource. */
|
|
168
239
|
get attributes() {
|
|
169
240
|
return this._attributes;
|
|
170
241
|
}
|
|
242
|
+
/** Replaces the resource's locale and formatting metadata. */
|
|
171
243
|
set attributes(attributes) {
|
|
172
244
|
this._attributes = attributes;
|
|
173
245
|
}
|
|
246
|
+
/** Notes attached to this resource for translators and tooling. */
|
|
174
247
|
get notes() {
|
|
175
248
|
return this._notes;
|
|
176
249
|
}
|
|
250
|
+
/** Replaces the notes attached to this resource. */
|
|
177
251
|
set notes(notes) {
|
|
178
252
|
this._notes = notes;
|
|
179
253
|
}
|
|
180
|
-
|
|
254
|
+
/**
|
|
255
|
+
* Appends a note to this resource.
|
|
256
|
+
*
|
|
257
|
+
* @returns This resource, for chaining.
|
|
258
|
+
*/
|
|
259
|
+
addNote(type, content) {
|
|
260
|
+
return this.setNote({ type, content });
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Appends a full note object to this resource.
|
|
264
|
+
*
|
|
265
|
+
* @returns This resource, for chaining.
|
|
266
|
+
*/
|
|
267
|
+
setNote(note) {
|
|
181
268
|
this.notes.push(note);
|
|
269
|
+
return this;
|
|
182
270
|
}
|
|
271
|
+
/** Title that identifies this resource within a project. */
|
|
183
272
|
get title() {
|
|
184
273
|
return this._title;
|
|
185
274
|
}
|
|
275
|
+
/** Sets the title that identifies this resource within a project. */
|
|
186
276
|
set title(title) {
|
|
187
277
|
this._title = title;
|
|
188
278
|
}
|
|
279
|
+
/**
|
|
280
|
+
* Returns the project this resource belongs to.
|
|
281
|
+
*/
|
|
189
282
|
getProject() {
|
|
190
283
|
return this._project;
|
|
191
284
|
}
|
|
285
|
+
/**
|
|
286
|
+
* Adds a message to this resource, merging resource attributes with any overrides.
|
|
287
|
+
*
|
|
288
|
+
* @returns This resource, for chaining.
|
|
289
|
+
*/
|
|
192
290
|
add(key, value, attributes, notes) {
|
|
193
291
|
const merged = { ...this.attributes, ...attributes };
|
|
194
292
|
const msg = MsgMessage.create({
|
|
@@ -200,6 +298,14 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
200
298
|
this.set(key, msg);
|
|
201
299
|
return this;
|
|
202
300
|
}
|
|
301
|
+
/**
|
|
302
|
+
* Builds a translated copy of this resource from translation data.
|
|
303
|
+
*
|
|
304
|
+
* Messages present in the translation replace the source; missing keys keep
|
|
305
|
+
* the source message. Notes are carried over from the source.
|
|
306
|
+
*
|
|
307
|
+
* @throws {TypeError} When the translation title does not match this resource.
|
|
308
|
+
*/
|
|
203
309
|
translate(data) {
|
|
204
310
|
const { title, attributes, messages } = data;
|
|
205
311
|
if (title !== this.title) {
|
|
@@ -225,7 +331,7 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
225
331
|
});
|
|
226
332
|
const notes = this.get(key)?.notes || [];
|
|
227
333
|
notes.forEach((note) => {
|
|
228
|
-
msg.addNote(note);
|
|
334
|
+
msg.addNote(note.type, note.content);
|
|
229
335
|
});
|
|
230
336
|
translated.set(key, msg);
|
|
231
337
|
});
|
|
@@ -237,10 +343,21 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
237
343
|
}
|
|
238
344
|
return { ...attributes, format };
|
|
239
345
|
}
|
|
240
|
-
|
|
346
|
+
/**
|
|
347
|
+
* Loads and applies translations for a locale via the project loader.
|
|
348
|
+
*
|
|
349
|
+
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
350
|
+
* copy without calling the loader.
|
|
351
|
+
*
|
|
352
|
+
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
353
|
+
*/
|
|
354
|
+
async getTranslation(lang2) {
|
|
355
|
+
if (!lang2) {
|
|
356
|
+
return _MsgResource.create(this.getData(), this.getProject());
|
|
357
|
+
}
|
|
241
358
|
const project = this._project;
|
|
242
359
|
const pseudoLocale = project.locales.pseudoLocale;
|
|
243
|
-
if (
|
|
360
|
+
if (lang2 === pseudoLocale) {
|
|
244
361
|
const pseudolocalizedData = {
|
|
245
362
|
title: this.title,
|
|
246
363
|
attributes: { ...this.attributes, lang: pseudoLocale },
|
|
@@ -256,22 +373,30 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
256
373
|
});
|
|
257
374
|
return this.translate(pseudolocalizedData);
|
|
258
375
|
}
|
|
259
|
-
const languageChain = project.getTargetLocale(
|
|
376
|
+
const languageChain = project.getTargetLocale(lang2);
|
|
260
377
|
if (!languageChain) {
|
|
261
378
|
throw new Error("Unsupported locale for resource.");
|
|
262
379
|
}
|
|
263
380
|
if (languageChain.length == 0) {
|
|
264
|
-
throw new Error(`Empty language chain for locale: ${
|
|
381
|
+
throw new Error(`Empty language chain for locale: ${lang2}`);
|
|
265
382
|
}
|
|
266
383
|
let translated = this;
|
|
267
384
|
for (let i = 0; i < languageChain.length; i++) {
|
|
268
|
-
const
|
|
269
|
-
if (
|
|
270
|
-
translated = await translated._project._loader(project.project.name, translated.title,
|
|
385
|
+
const lang3 = languageChain[i];
|
|
386
|
+
if (lang3 && project._locales.targetLocales[lang3]) {
|
|
387
|
+
translated = await translated._project._loader(project.project.name, translated.title, lang3).then((data) => translated.translate(data));
|
|
271
388
|
}
|
|
272
389
|
}
|
|
273
390
|
return translated;
|
|
274
391
|
}
|
|
392
|
+
/**
|
|
393
|
+
* Returns a plain data object for this resource and its messages.
|
|
394
|
+
*
|
|
395
|
+
* Attributes that match the resource or project defaults are omitted where
|
|
396
|
+
* possible to keep the output compact.
|
|
397
|
+
*
|
|
398
|
+
* @param stripNotes - When true, notes are omitted from the result.
|
|
399
|
+
*/
|
|
275
400
|
getData(stripNotes = false) {
|
|
276
401
|
const resourceFormat = this.resolveFormat(this.attributes);
|
|
277
402
|
const projectFormat = this._project.format;
|
|
@@ -304,6 +429,11 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
304
429
|
messages
|
|
305
430
|
};
|
|
306
431
|
}
|
|
432
|
+
/**
|
|
433
|
+
* Serializes this resource to a formatted JSON string.
|
|
434
|
+
*
|
|
435
|
+
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
436
|
+
*/
|
|
307
437
|
toJSON(stripNotes = false) {
|
|
308
438
|
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
309
439
|
}
|
|
@@ -323,24 +453,36 @@ var defaultLocalesSettings = {
|
|
|
323
453
|
}
|
|
324
454
|
};
|
|
325
455
|
var MsgProject = class _MsgProject {
|
|
456
|
+
/** Project name, version, and default format. */
|
|
326
457
|
_project;
|
|
458
|
+
/** Source, pseudo, and target locale settings. */
|
|
327
459
|
_locales;
|
|
460
|
+
/** Function used to load translations for a locale. */
|
|
328
461
|
_loader;
|
|
462
|
+
/**
|
|
463
|
+
* Builds a project from {@link MsgProjectData}.
|
|
464
|
+
*/
|
|
329
465
|
static create(data) {
|
|
330
466
|
const { project, locales, loader } = data;
|
|
331
467
|
return new _MsgProject(project, locales, loader);
|
|
332
468
|
}
|
|
469
|
+
/**
|
|
470
|
+
* Creates a project from its settings and loader. Prefer {@link MsgProject.create}.
|
|
471
|
+
*/
|
|
333
472
|
constructor(projectSettings, localesSettings, loader) {
|
|
334
473
|
this._project = { ...defaultProjectSettings, ...projectSettings };
|
|
335
474
|
this._locales = { ...defaultLocalesSettings, ...localesSettings };
|
|
336
475
|
this._loader = loader;
|
|
337
476
|
}
|
|
477
|
+
/** Project name, version, and default format. */
|
|
338
478
|
get project() {
|
|
339
479
|
return this._project;
|
|
340
480
|
}
|
|
481
|
+
/** Source, pseudo, and target locale settings. */
|
|
341
482
|
get locales() {
|
|
342
483
|
return this._locales;
|
|
343
484
|
}
|
|
485
|
+
/** Function used to load translations for a locale. */
|
|
344
486
|
get loader() {
|
|
345
487
|
return this._loader;
|
|
346
488
|
}
|
|
@@ -351,15 +493,29 @@ var MsgProject = class _MsgProject {
|
|
|
351
493
|
get format() {
|
|
352
494
|
return this._project.format ?? MSG_DEFAULT_FORMAT;
|
|
353
495
|
}
|
|
496
|
+
/**
|
|
497
|
+
* Returns the translation load chain for a locale, or `undefined` if unsupported.
|
|
498
|
+
*/
|
|
354
499
|
getTargetLocale(locale) {
|
|
355
500
|
return this._locales.targetLocales[locale];
|
|
356
501
|
}
|
|
357
502
|
};
|
|
503
|
+
|
|
504
|
+
// src/lib/lang-utils.ts
|
|
505
|
+
var lang;
|
|
506
|
+
function setLang(tag) {
|
|
507
|
+
lang = tag;
|
|
508
|
+
}
|
|
509
|
+
function getLang() {
|
|
510
|
+
return lang;
|
|
511
|
+
}
|
|
358
512
|
// Annotate the CommonJS export names for ESM import in node:
|
|
359
513
|
0 && (module.exports = {
|
|
360
514
|
DEFAULT_ATTRIBUTES,
|
|
361
515
|
MSG_DEFAULT_FORMAT,
|
|
362
516
|
MsgMessage,
|
|
363
517
|
MsgProject,
|
|
364
|
-
MsgResource
|
|
518
|
+
MsgResource,
|
|
519
|
+
getLang,
|
|
520
|
+
setLang
|
|
365
521
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, MsgAttributes, MsgFormat, MsgInterface, MsgNote } from './classes/MsgInterface/MsgInterface.cjs';
|
|
1
|
+
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, MsgAttributes, MsgFormat, MsgInterface, MsgNote, NoteTypes } from './classes/MsgInterface/MsgInterface.cjs';
|
|
2
2
|
export { MsgMessage, MsgMessageData } from './classes/MsgMessage/MsgMessage.cjs';
|
|
3
|
-
export {
|
|
3
|
+
export { MsgLocalesSettings, MsgProject, MsgProjectData, MsgProjectSettings, MsgResource, MsgResourceData, MsgTargetLocalesSettings, MsgTranslationLoader } from './classes/index.cjs';
|
|
4
|
+
export { getLang, setLang } from './lib/lang-utils.cjs';
|
|
4
5
|
import 'messageformat';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, MsgAttributes, MsgFormat, MsgInterface, MsgNote } from './classes/MsgInterface/MsgInterface.js';
|
|
1
|
+
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, MsgAttributes, MsgFormat, MsgInterface, MsgNote, NoteTypes } from './classes/MsgInterface/MsgInterface.js';
|
|
2
2
|
export { MsgMessage, MsgMessageData } from './classes/MsgMessage/MsgMessage.js';
|
|
3
|
-
export {
|
|
3
|
+
export { MsgLocalesSettings, MsgProject, MsgProjectData, MsgProjectSettings, MsgResource, MsgResourceData, MsgTargetLocalesSettings, MsgTranslationLoader } from './classes/index.js';
|
|
4
|
+
export { getLang, setLang } from './lib/lang-utils.js';
|
|
4
5
|
import 'messageformat';
|
package/dist/index.mjs
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import "./chunk-ORHP7HGX.mjs";
|
|
2
|
+
import "./chunk-KQTWUIAM.mjs";
|
|
3
|
+
import {
|
|
4
|
+
getLang,
|
|
5
|
+
setLang
|
|
6
|
+
} from "./chunk-PU7ZZ5XH.mjs";
|
|
7
|
+
import {
|
|
8
|
+
MsgProject
|
|
9
|
+
} from "./chunk-DJFPVXQ5.mjs";
|
|
2
10
|
import {
|
|
3
11
|
MsgResource
|
|
4
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-HUWFAJAN.mjs";
|
|
5
13
|
import {
|
|
6
14
|
MsgMessage
|
|
7
|
-
} from "./chunk-
|
|
8
|
-
import {
|
|
9
|
-
MsgProject
|
|
10
|
-
} from "./chunk-NWJ7MVVV.mjs";
|
|
15
|
+
} from "./chunk-OKEV7FY7.mjs";
|
|
11
16
|
import {
|
|
12
17
|
DEFAULT_ATTRIBUTES,
|
|
13
18
|
MSG_DEFAULT_FORMAT
|
|
@@ -17,5 +22,7 @@ export {
|
|
|
17
22
|
MSG_DEFAULT_FORMAT,
|
|
18
23
|
MsgMessage,
|
|
19
24
|
MsgProject,
|
|
20
|
-
MsgResource
|
|
25
|
+
MsgResource,
|
|
26
|
+
getLang,
|
|
27
|
+
setLang
|
|
21
28
|
};
|