@worldware/msg 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1,365 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ DEFAULT_ATTRIBUTES: () => DEFAULT_ATTRIBUTES,
24
+ MSG_DEFAULT_FORMAT: () => MSG_DEFAULT_FORMAT,
25
+ MsgMessage: () => MsgMessage,
26
+ MsgProject: () => MsgProject,
27
+ MsgResource: () => MsgResource
28
+ });
29
+ module.exports = __toCommonJS(index_exports);
30
+
31
+ // src/classes/MsgInterface/MsgInterface.ts
32
+ var MSG_DEFAULT_FORMAT = "MF2";
33
+ var DEFAULT_ATTRIBUTES = {
34
+ lang: "und",
35
+ dir: "auto",
36
+ dnt: false
37
+ };
38
+
39
+ // src/classes/MsgMessage/MsgMessage.ts
40
+ var import_messageformat = require("messageformat");
41
+ var import_icu_messageformat_1 = require("@messageformat/icu-messageformat-1");
42
+ var MsgMessage = class _MsgMessage {
43
+ _key;
44
+ _value;
45
+ _mf;
46
+ _attributes;
47
+ _notes = [];
48
+ constructor(key, value, attributes, notes) {
49
+ this._key = key;
50
+ this._value = value;
51
+ this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
52
+ if (notes) {
53
+ notes.forEach((note) => this.addNote(note));
54
+ }
55
+ }
56
+ static create(data) {
57
+ const { key, value, attributes, notes } = data;
58
+ const message = new _MsgMessage(key, value, attributes, notes);
59
+ return message;
60
+ }
61
+ get key() {
62
+ return this._key;
63
+ }
64
+ get value() {
65
+ return this._value;
66
+ }
67
+ get attributes() {
68
+ return this._attributes;
69
+ }
70
+ get notes() {
71
+ return this._notes;
72
+ }
73
+ addNote(note) {
74
+ this.notes.push(note);
75
+ }
76
+ /**
77
+ * The message's resolved format, defaulting to MF2 when not set or inherited.
78
+ */
79
+ resolveFormat() {
80
+ return this.attributes.format ?? MSG_DEFAULT_FORMAT;
81
+ }
82
+ getFormatter(options) {
83
+ if (!this._mf) {
84
+ this._mf = this.resolveFormat() === "MF1" ? (0, import_icu_messageformat_1.mf1ToMessage)(this.attributes.lang, this.value, options) : new import_messageformat.MessageFormat(this.attributes.lang, this.value, options);
85
+ }
86
+ return this._mf;
87
+ }
88
+ format(data, options) {
89
+ if (this.resolveFormat() === "NONE") {
90
+ return this.value;
91
+ }
92
+ return this.getFormatter(options).format(data);
93
+ }
94
+ formatToParts(data, options) {
95
+ if (this.resolveFormat() === "NONE") {
96
+ return [{ type: "text", value: this.value }];
97
+ }
98
+ return this.getFormatter(options).formatToParts(data);
99
+ }
100
+ getData(stripNotes = false) {
101
+ return {
102
+ key: this.key,
103
+ value: this.value,
104
+ attributes: this.attributes,
105
+ notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0
106
+ };
107
+ }
108
+ toString() {
109
+ return this.value;
110
+ }
111
+ toJSON(stripNotes = false) {
112
+ return JSON.stringify(this.getData(stripNotes), null, 2);
113
+ }
114
+ };
115
+
116
+ // src/classes/MsgResource/MsgResource.ts
117
+ var import_messageformat2 = require("messageformat");
118
+ var import_pseudo_localization = require("pseudo-localization");
119
+ var MsgResource = class _MsgResource extends Map {
120
+ _attributes = {};
121
+ _notes = [];
122
+ _title;
123
+ _project;
124
+ static create(data, project) {
125
+ const { title, attributes, notes, messages } = data;
126
+ const res = new _MsgResource(title, attributes, project, notes);
127
+ if (messages) {
128
+ messages.forEach((messageData) => {
129
+ const { key, value, attributes: attributes2, notes: notes2 } = messageData;
130
+ res.add(key, value, attributes2, notes2);
131
+ });
132
+ } else {
133
+ res.clear();
134
+ }
135
+ return res;
136
+ }
137
+ constructor(title, attributes, project, notes) {
138
+ super();
139
+ this._title = title;
140
+ this._attributes = { ...DEFAULT_ATTRIBUTES, format: project.format, ...attributes };
141
+ this._project = project;
142
+ if (notes) {
143
+ notes.forEach((note) => this.addNote(note));
144
+ }
145
+ }
146
+ hasMatchingAttributes(message) {
147
+ const res = this.attributes;
148
+ const msg = message.attributes;
149
+ return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt && this.resolveFormat(res) === this.resolveFormat(msg);
150
+ }
151
+ resolveFormat(attributes) {
152
+ return attributes.format ?? MSG_DEFAULT_FORMAT;
153
+ }
154
+ pseudoLocalizeMF2(source, options) {
155
+ const msg = (0, import_messageformat2.parseMessage)(source);
156
+ (0, import_messageformat2.visit)(msg, {
157
+ pattern: (pattern) => {
158
+ for (let i = 0; i < pattern.length; i++) {
159
+ const part = pattern[i];
160
+ if (typeof part === "string") {
161
+ pattern[i] = (0, import_pseudo_localization.localize)(part, options);
162
+ }
163
+ }
164
+ }
165
+ });
166
+ return (0, import_messageformat2.stringifyMessage)(msg);
167
+ }
168
+ get attributes() {
169
+ return this._attributes;
170
+ }
171
+ set attributes(attributes) {
172
+ this._attributes = attributes;
173
+ }
174
+ get notes() {
175
+ return this._notes;
176
+ }
177
+ set notes(notes) {
178
+ this._notes = notes;
179
+ }
180
+ addNote(note) {
181
+ this.notes.push(note);
182
+ }
183
+ get title() {
184
+ return this._title;
185
+ }
186
+ set title(title) {
187
+ this._title = title;
188
+ }
189
+ getProject() {
190
+ return this._project;
191
+ }
192
+ add(key, value, attributes, notes) {
193
+ const merged = { ...this.attributes, ...attributes };
194
+ const msg = MsgMessage.create({
195
+ key,
196
+ value,
197
+ attributes: merged,
198
+ notes
199
+ });
200
+ this.set(key, msg);
201
+ return this;
202
+ }
203
+ translate(data) {
204
+ const { title, attributes, messages } = data;
205
+ if (title !== this.title) {
206
+ throw new TypeError("Title of resource and translations do not match.");
207
+ }
208
+ const translated = _MsgResource.create({
209
+ title,
210
+ // preserve the source resource's format unless the translation overrides it
211
+ attributes: this.preserveFormat(attributes, this.attributes.format),
212
+ notes: this.notes
213
+ // transfer the notes
214
+ }, this._project);
215
+ this.forEach((msg) => {
216
+ translated.set(msg.key, msg);
217
+ });
218
+ messages?.forEach((messageData) => {
219
+ const { key, value, attributes: attributes2 } = messageData;
220
+ const msg = MsgMessage.create({
221
+ key,
222
+ value,
223
+ // preserve the source message's format unless the translation overrides it
224
+ attributes: this.preserveFormat(attributes2, this.get(key)?.attributes.format)
225
+ });
226
+ const notes = this.get(key)?.notes || [];
227
+ notes.forEach((note) => {
228
+ msg.addNote(note);
229
+ });
230
+ translated.set(key, msg);
231
+ });
232
+ return translated;
233
+ }
234
+ preserveFormat(attributes, format) {
235
+ if (format === void 0 || attributes?.format !== void 0) {
236
+ return attributes;
237
+ }
238
+ return { ...attributes, format };
239
+ }
240
+ async getTranslation(lang) {
241
+ const project = this._project;
242
+ const pseudoLocale = project.locales.pseudoLocale;
243
+ if (lang === pseudoLocale) {
244
+ const pseudolocalizedData = {
245
+ title: this.title,
246
+ attributes: { ...this.attributes, lang: pseudoLocale },
247
+ notes: this.notes.length > 0 ? this.notes : void 0,
248
+ messages: []
249
+ };
250
+ this.forEach((msg) => {
251
+ pseudolocalizedData.messages.push({
252
+ key: msg.key,
253
+ value: this.pseudoLocalizeMF2(msg.value),
254
+ attributes: { ...this.attributes, lang: pseudoLocale }
255
+ });
256
+ });
257
+ return this.translate(pseudolocalizedData);
258
+ }
259
+ const languageChain = project.getTargetLocale(lang);
260
+ if (!languageChain) {
261
+ throw new Error("Unsupported locale for resource.");
262
+ }
263
+ if (languageChain.length == 0) {
264
+ throw new Error(`Empty language chain for locale: ${lang}`);
265
+ }
266
+ let translated = this;
267
+ for (let i = 0; i < languageChain.length; i++) {
268
+ const lang2 = languageChain[i];
269
+ if (lang2 && project._locales.targetLocales[lang2]) {
270
+ translated = await translated._project._loader(project.project.name, translated.title, lang2).then((data) => translated.translate(data));
271
+ }
272
+ }
273
+ return translated;
274
+ }
275
+ getData(stripNotes = false) {
276
+ const resourceFormat = this.resolveFormat(this.attributes);
277
+ const projectFormat = this._project.format;
278
+ const messages = [];
279
+ this.forEach((msg) => {
280
+ if (this.hasMatchingAttributes(msg)) {
281
+ const data = {
282
+ key: msg.key,
283
+ value: msg.value,
284
+ notes: !stripNotes && msg.notes.length > 0 ? msg.notes : void 0
285
+ };
286
+ messages.push(data);
287
+ } else {
288
+ const data = msg.getData(stripNotes);
289
+ if (data.attributes && this.resolveFormat(data.attributes) === resourceFormat) {
290
+ const { format, ...rest } = data.attributes;
291
+ data.attributes = rest;
292
+ }
293
+ messages.push(data);
294
+ }
295
+ });
296
+ const attributes = { ...this.attributes };
297
+ if (this.resolveFormat(attributes) === projectFormat) {
298
+ delete attributes.format;
299
+ }
300
+ return {
301
+ title: this.title,
302
+ attributes,
303
+ notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0,
304
+ messages
305
+ };
306
+ }
307
+ toJSON(stripNotes = false) {
308
+ return JSON.stringify(this.getData(stripNotes), null, 2);
309
+ }
310
+ };
311
+
312
+ // src/classes/MsgProject/MsgProject.ts
313
+ var defaultProjectSettings = {
314
+ name: "messages",
315
+ version: 1,
316
+ format: MSG_DEFAULT_FORMAT
317
+ };
318
+ var defaultLocalesSettings = {
319
+ sourceLocale: "en",
320
+ pseudoLocale: "en-XA",
321
+ targetLocales: {
322
+ en: ["en"]
323
+ }
324
+ };
325
+ var MsgProject = class _MsgProject {
326
+ _project;
327
+ _locales;
328
+ _loader;
329
+ static create(data) {
330
+ const { project, locales, loader } = data;
331
+ return new _MsgProject(project, locales, loader);
332
+ }
333
+ constructor(projectSettings, localesSettings, loader) {
334
+ this._project = { ...defaultProjectSettings, ...projectSettings };
335
+ this._locales = { ...defaultLocalesSettings, ...localesSettings };
336
+ this._loader = loader;
337
+ }
338
+ get project() {
339
+ return this._project;
340
+ }
341
+ get locales() {
342
+ return this._locales;
343
+ }
344
+ get loader() {
345
+ return this._loader;
346
+ }
347
+ /**
348
+ * The project-wide default message format. Resources (and, through them,
349
+ * messages) inherit this value unless they specify their own.
350
+ */
351
+ get format() {
352
+ return this._project.format ?? MSG_DEFAULT_FORMAT;
353
+ }
354
+ getTargetLocale(locale) {
355
+ return this._locales.targetLocales[locale];
356
+ }
357
+ };
358
+ // Annotate the CommonJS export names for ESM import in node:
359
+ 0 && (module.exports = {
360
+ DEFAULT_ATTRIBUTES,
361
+ MSG_DEFAULT_FORMAT,
362
+ MsgMessage,
363
+ MsgProject,
364
+ MsgResource
365
+ });
@@ -0,0 +1,4 @@
1
+ export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, MsgAttributes, MsgFormat, MsgInterface, MsgNote } from './classes/MsgInterface/MsgInterface.cjs';
2
+ export { MsgMessage, MsgMessageData } from './classes/MsgMessage/MsgMessage.cjs';
3
+ export { M as MsgProject, a as MsgProjectData, MsgResource, MsgResourceData, b as MsgTranslationLoader } from './classes/MsgResource/MsgResource.cjs';
4
+ import 'messageformat';
@@ -0,0 +1,4 @@
1
+ export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, MsgAttributes, MsgFormat, MsgInterface, MsgNote } from './classes/MsgInterface/MsgInterface.js';
2
+ export { MsgMessage, MsgMessageData } from './classes/MsgMessage/MsgMessage.js';
3
+ export { M as MsgProject, a as MsgProjectData, MsgResource, MsgResourceData, b as MsgTranslationLoader } from './classes/MsgResource/MsgResource.js';
4
+ import 'messageformat';
package/dist/index.mjs ADDED
@@ -0,0 +1,21 @@
1
+ import "./chunk-ORHP7HGX.mjs";
2
+ import {
3
+ MsgResource
4
+ } from "./chunk-X6CRKSNU.mjs";
5
+ import {
6
+ MsgMessage
7
+ } from "./chunk-HCR2UNSE.mjs";
8
+ import {
9
+ MsgProject
10
+ } from "./chunk-NWJ7MVVV.mjs";
11
+ import {
12
+ DEFAULT_ATTRIBUTES,
13
+ MSG_DEFAULT_FORMAT
14
+ } from "./chunk-XQVALCKX.mjs";
15
+ export {
16
+ DEFAULT_ATTRIBUTES,
17
+ MSG_DEFAULT_FORMAT,
18
+ MsgMessage,
19
+ MsgProject,
20
+ MsgResource
21
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@worldware/msg",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "Message localization tooling",
5
5
  "license": "MIT",
6
6
  "author": "Joel Sahleen",
@@ -46,6 +46,7 @@
46
46
  },
47
47
  "scripts": {
48
48
  "build": "tsup",
49
+ "prepack": "npm run build",
49
50
  "test": "vitest run",
50
51
  "test:watch": "vitest",
51
52
  "coverage": "vitest run --coverage",