@worldware/msg 0.7.1 → 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.
Files changed (31) hide show
  1. package/README.md +54 -10
  2. package/dist/{chunk-WUDKNZV2.mjs → chunk-HCR2UNSE.mjs} +22 -8
  3. package/dist/{chunk-XS43NAP2.mjs → chunk-NWJ7MVVV.mjs} +13 -1
  4. package/dist/{chunk-2QUOYESW.mjs → chunk-X6CRKSNU.mjs} +32 -9
  5. package/dist/{chunk-QWBDIKQK.mjs → chunk-XQVALCKX.mjs} +2 -0
  6. package/dist/classes/MsgInterface/MsgInterface.cjs +5 -2
  7. package/dist/classes/MsgInterface/MsgInterface.d.cts +15 -1
  8. package/dist/classes/MsgInterface/MsgInterface.d.ts +15 -1
  9. package/dist/classes/MsgInterface/MsgInterface.mjs +5 -3
  10. package/dist/classes/MsgMessage/MsgMessage.cjs +20 -6
  11. package/dist/classes/MsgMessage/MsgMessage.d.cts +6 -1
  12. package/dist/classes/MsgMessage/MsgMessage.d.ts +6 -1
  13. package/dist/classes/MsgMessage/MsgMessage.mjs +2 -2
  14. package/dist/classes/MsgProject/MsgProject.cjs +14 -1
  15. package/dist/classes/MsgProject/MsgProject.d.cts +3 -64
  16. package/dist/classes/MsgProject/MsgProject.d.ts +3 -64
  17. package/dist/classes/MsgProject/MsgProject.mjs +2 -1
  18. package/dist/classes/MsgResource/MsgResource.cjs +48 -12
  19. package/dist/classes/MsgResource/MsgResource.d.cts +78 -3
  20. package/dist/classes/MsgResource/MsgResource.d.ts +78 -3
  21. package/dist/classes/MsgResource/MsgResource.mjs +3 -3
  22. package/dist/classes/index.cjs +62 -16
  23. package/dist/classes/index.d.cts +2 -2
  24. package/dist/classes/index.d.ts +2 -2
  25. package/dist/classes/index.mjs +12 -7
  26. package/dist/index.cjs +62 -16
  27. package/dist/index.d.cts +2 -2
  28. package/dist/index.d.ts +2 -2
  29. package/dist/index.mjs +12 -7
  30. package/package.json +8 -3
  31. /package/dist/{chunk-ZKG5QRBZ.mjs → chunk-ORHP7HGX.mjs} +0 -0
package/dist/index.cjs CHANGED
@@ -20,16 +20,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ DEFAULT_ATTRIBUTES: () => DEFAULT_ATTRIBUTES,
24
+ MSG_DEFAULT_FORMAT: () => MSG_DEFAULT_FORMAT,
23
25
  MsgMessage: () => MsgMessage,
24
26
  MsgProject: () => MsgProject,
25
27
  MsgResource: () => MsgResource
26
28
  });
27
29
  module.exports = __toCommonJS(index_exports);
28
30
 
29
- // src/classes/MsgMessage/MsgMessage.ts
30
- var import_messageformat = require("messageformat");
31
-
32
31
  // src/classes/MsgInterface/MsgInterface.ts
32
+ var MSG_DEFAULT_FORMAT = "MF2";
33
33
  var DEFAULT_ATTRIBUTES = {
34
34
  lang: "und",
35
35
  dir: "auto",
@@ -37,6 +37,8 @@ var DEFAULT_ATTRIBUTES = {
37
37
  };
38
38
 
39
39
  // src/classes/MsgMessage/MsgMessage.ts
40
+ var import_messageformat = require("messageformat");
41
+ var import_icu_messageformat_1 = require("@messageformat/icu-messageformat-1");
40
42
  var MsgMessage = class _MsgMessage {
41
43
  _key;
42
44
  _value;
@@ -71,17 +73,29 @@ var MsgMessage = class _MsgMessage {
71
73
  addNote(note) {
72
74
  this.notes.push(note);
73
75
  }
74
- format(data, options) {
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) {
75
83
  if (!this._mf) {
76
- this._mf = new import_messageformat.MessageFormat(this.attributes.lang, this.value, options);
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);
77
85
  }
78
- return this._mf.format(data);
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);
79
93
  }
80
94
  formatToParts(data, options) {
81
- if (!this._mf) {
82
- this._mf = new import_messageformat.MessageFormat(this.attributes.lang, this.value, options);
95
+ if (this.resolveFormat() === "NONE") {
96
+ return [{ type: "text", value: this.value }];
83
97
  }
84
- return this._mf?.formatToParts(data);
98
+ return this.getFormatter(options).formatToParts(data);
85
99
  }
86
100
  getData(stripNotes = false) {
87
101
  return {
@@ -123,7 +137,7 @@ var MsgResource = class _MsgResource extends Map {
123
137
  constructor(title, attributes, project, notes) {
124
138
  super();
125
139
  this._title = title;
126
- this._attributes = { ...DEFAULT_ATTRIBUTES, ...attributes };
140
+ this._attributes = { ...DEFAULT_ATTRIBUTES, format: project.format, ...attributes };
127
141
  this._project = project;
128
142
  if (notes) {
129
143
  notes.forEach((note) => this.addNote(note));
@@ -132,7 +146,10 @@ var MsgResource = class _MsgResource extends Map {
132
146
  hasMatchingAttributes(message) {
133
147
  const res = this.attributes;
134
148
  const msg = message.attributes;
135
- return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt;
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;
136
153
  }
137
154
  pseudoLocalizeMF2(source, options) {
138
155
  const msg = (0, import_messageformat2.parseMessage)(source);
@@ -190,7 +207,8 @@ var MsgResource = class _MsgResource extends Map {
190
207
  }
191
208
  const translated = _MsgResource.create({
192
209
  title,
193
- attributes,
210
+ // preserve the source resource's format unless the translation overrides it
211
+ attributes: this.preserveFormat(attributes, this.attributes.format),
194
212
  notes: this.notes
195
213
  // transfer the notes
196
214
  }, this._project);
@@ -202,7 +220,8 @@ var MsgResource = class _MsgResource extends Map {
202
220
  const msg = MsgMessage.create({
203
221
  key,
204
222
  value,
205
- attributes: attributes2
223
+ // preserve the source message's format unless the translation overrides it
224
+ attributes: this.preserveFormat(attributes2, this.get(key)?.attributes.format)
206
225
  });
207
226
  const notes = this.get(key)?.notes || [];
208
227
  notes.forEach((note) => {
@@ -212,6 +231,12 @@ var MsgResource = class _MsgResource extends Map {
212
231
  });
213
232
  return translated;
214
233
  }
234
+ preserveFormat(attributes, format) {
235
+ if (format === void 0 || attributes?.format !== void 0) {
236
+ return attributes;
237
+ }
238
+ return { ...attributes, format };
239
+ }
215
240
  async getTranslation(lang) {
216
241
  const project = this._project;
217
242
  const pseudoLocale = project.locales.pseudoLocale;
@@ -248,6 +273,8 @@ var MsgResource = class _MsgResource extends Map {
248
273
  return translated;
249
274
  }
250
275
  getData(stripNotes = false) {
276
+ const resourceFormat = this.resolveFormat(this.attributes);
277
+ const projectFormat = this._project.format;
251
278
  const messages = [];
252
279
  this.forEach((msg) => {
253
280
  if (this.hasMatchingAttributes(msg)) {
@@ -258,12 +285,21 @@ var MsgResource = class _MsgResource extends Map {
258
285
  };
259
286
  messages.push(data);
260
287
  } else {
261
- messages.push(msg.getData(stripNotes));
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);
262
294
  }
263
295
  });
296
+ const attributes = { ...this.attributes };
297
+ if (this.resolveFormat(attributes) === projectFormat) {
298
+ delete attributes.format;
299
+ }
264
300
  return {
265
301
  title: this.title,
266
- attributes: this.attributes,
302
+ attributes,
267
303
  notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0,
268
304
  messages
269
305
  };
@@ -276,7 +312,8 @@ var MsgResource = class _MsgResource extends Map {
276
312
  // src/classes/MsgProject/MsgProject.ts
277
313
  var defaultProjectSettings = {
278
314
  name: "messages",
279
- version: 1
315
+ version: 1,
316
+ format: MSG_DEFAULT_FORMAT
280
317
  };
281
318
  var defaultLocalesSettings = {
282
319
  sourceLocale: "en",
@@ -307,12 +344,21 @@ var MsgProject = class _MsgProject {
307
344
  get loader() {
308
345
  return this._loader;
309
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
+ }
310
354
  getTargetLocale(locale) {
311
355
  return this._locales.targetLocales[locale];
312
356
  }
313
357
  };
314
358
  // Annotate the CommonJS export names for ESM import in node:
315
359
  0 && (module.exports = {
360
+ DEFAULT_ATTRIBUTES,
361
+ MSG_DEFAULT_FORMAT,
316
362
  MsgMessage,
317
363
  MsgProject,
318
364
  MsgResource
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
+ export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, MsgAttributes, MsgFormat, MsgInterface, MsgNote } from './classes/MsgInterface/MsgInterface.cjs';
1
2
  export { MsgMessage, MsgMessageData } from './classes/MsgMessage/MsgMessage.cjs';
2
- export { MsgProject, MsgProjectData, a as MsgResource, M as MsgResourceData, MsgTranslationLoader } from './classes/MsgProject/MsgProject.cjs';
3
+ export { M as MsgProject, a as MsgProjectData, MsgResource, MsgResourceData, b as MsgTranslationLoader } from './classes/MsgResource/MsgResource.cjs';
3
4
  import 'messageformat';
4
- import './classes/MsgInterface/MsgInterface.cjs';
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
+ export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, MsgAttributes, MsgFormat, MsgInterface, MsgNote } from './classes/MsgInterface/MsgInterface.js';
1
2
  export { MsgMessage, MsgMessageData } from './classes/MsgMessage/MsgMessage.js';
2
- export { MsgProject, MsgProjectData, a as MsgResource, M as MsgResourceData, MsgTranslationLoader } from './classes/MsgProject/MsgProject.js';
3
+ export { M as MsgProject, a as MsgProjectData, MsgResource, MsgResourceData, b as MsgTranslationLoader } from './classes/MsgResource/MsgResource.js';
3
4
  import 'messageformat';
4
- import './classes/MsgInterface/MsgInterface.js';
package/dist/index.mjs CHANGED
@@ -1,15 +1,20 @@
1
- import "./chunk-ZKG5QRBZ.mjs";
2
- import {
3
- MsgProject
4
- } from "./chunk-XS43NAP2.mjs";
1
+ import "./chunk-ORHP7HGX.mjs";
5
2
  import {
6
3
  MsgResource
7
- } from "./chunk-2QUOYESW.mjs";
4
+ } from "./chunk-X6CRKSNU.mjs";
8
5
  import {
9
6
  MsgMessage
10
- } from "./chunk-WUDKNZV2.mjs";
11
- import "./chunk-QWBDIKQK.mjs";
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";
12
15
  export {
16
+ DEFAULT_ATTRIBUTES,
17
+ MSG_DEFAULT_FORMAT,
13
18
  MsgMessage,
14
19
  MsgProject,
15
20
  MsgResource
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@worldware/msg",
3
- "version": "0.7.1",
3
+ "version": "0.8.1",
4
4
  "description": "Message localization tooling",
5
5
  "license": "MIT",
6
6
  "author": "Joel Sahleen",
@@ -33,18 +33,23 @@
33
33
  "devDependencies": {
34
34
  "@types/typescript": "^0.4.29",
35
35
  "@vitest/coverage-v8": "^4.0.15",
36
+ "semantic-release": "^25.0.8",
36
37
  "tsup": "^8.5.1",
38
+ "typedoc": "^0.28.20",
37
39
  "typescript": "^5.9.3",
38
40
  "vitest": "^4.0.15"
39
41
  },
40
42
  "dependencies": {
41
- "messageformat": "4.0.0-10",
43
+ "@messageformat/icu-messageformat-1": "^0.12.0",
44
+ "messageformat": "^4.0.0",
42
45
  "pseudo-localization": "^2.4.0"
43
46
  },
44
47
  "scripts": {
45
48
  "build": "tsup",
49
+ "prepack": "npm run build",
46
50
  "test": "vitest run",
47
51
  "test:watch": "vitest",
48
- "coverage": "vitest run --coverage"
52
+ "coverage": "vitest run --coverage",
53
+ "docs": "typedoc src/index.ts"
49
54
  }
50
55
  }
File without changes