@worldware/msg 0.7.1 → 0.8.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.
@@ -1,319 +0,0 @@
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/classes/index.ts
21
- var classes_exports = {};
22
- __export(classes_exports, {
23
- MsgMessage: () => MsgMessage,
24
- MsgProject: () => MsgProject,
25
- MsgResource: () => MsgResource
26
- });
27
- module.exports = __toCommonJS(classes_exports);
28
-
29
- // src/classes/MsgMessage/MsgMessage.ts
30
- var import_messageformat = require("messageformat");
31
-
32
- // src/classes/MsgInterface/MsgInterface.ts
33
- var DEFAULT_ATTRIBUTES = {
34
- lang: "und",
35
- dir: "auto",
36
- dnt: false
37
- };
38
-
39
- // src/classes/MsgMessage/MsgMessage.ts
40
- var MsgMessage = class _MsgMessage {
41
- _key;
42
- _value;
43
- _mf;
44
- _attributes;
45
- _notes = [];
46
- constructor(key, value, attributes, notes) {
47
- this._key = key;
48
- this._value = value;
49
- this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
50
- if (notes) {
51
- notes.forEach((note) => this.addNote(note));
52
- }
53
- }
54
- static create(data) {
55
- const { key, value, attributes, notes } = data;
56
- const message = new _MsgMessage(key, value, attributes, notes);
57
- return message;
58
- }
59
- get key() {
60
- return this._key;
61
- }
62
- get value() {
63
- return this._value;
64
- }
65
- get attributes() {
66
- return this._attributes;
67
- }
68
- get notes() {
69
- return this._notes;
70
- }
71
- addNote(note) {
72
- this.notes.push(note);
73
- }
74
- format(data, options) {
75
- if (!this._mf) {
76
- this._mf = new import_messageformat.MessageFormat(this.attributes.lang, this.value, options);
77
- }
78
- return this._mf.format(data);
79
- }
80
- formatToParts(data, options) {
81
- if (!this._mf) {
82
- this._mf = new import_messageformat.MessageFormat(this.attributes.lang, this.value, options);
83
- }
84
- return this._mf?.formatToParts(data);
85
- }
86
- getData(stripNotes = false) {
87
- return {
88
- key: this.key,
89
- value: this.value,
90
- attributes: this.attributes,
91
- notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0
92
- };
93
- }
94
- toString() {
95
- return this.value;
96
- }
97
- toJSON(stripNotes = false) {
98
- return JSON.stringify(this.getData(stripNotes), null, 2);
99
- }
100
- };
101
-
102
- // src/classes/MsgResource/MsgResource.ts
103
- var import_messageformat2 = require("messageformat");
104
- var import_pseudo_localization = require("pseudo-localization");
105
- var MsgResource = class _MsgResource extends Map {
106
- _attributes = {};
107
- _notes = [];
108
- _title;
109
- _project;
110
- static create(data, project) {
111
- const { title, attributes, notes, messages } = data;
112
- const res = new _MsgResource(title, attributes, project, notes);
113
- if (messages) {
114
- messages.forEach((messageData) => {
115
- const { key, value, attributes: attributes2, notes: notes2 } = messageData;
116
- res.add(key, value, attributes2, notes2);
117
- });
118
- } else {
119
- res.clear();
120
- }
121
- return res;
122
- }
123
- constructor(title, attributes, project, notes) {
124
- super();
125
- this._title = title;
126
- this._attributes = { ...DEFAULT_ATTRIBUTES, ...attributes };
127
- this._project = project;
128
- if (notes) {
129
- notes.forEach((note) => this.addNote(note));
130
- }
131
- }
132
- hasMatchingAttributes(message) {
133
- const res = this.attributes;
134
- const msg = message.attributes;
135
- return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt;
136
- }
137
- pseudoLocalizeMF2(source, options) {
138
- const msg = (0, import_messageformat2.parseMessage)(source);
139
- (0, import_messageformat2.visit)(msg, {
140
- pattern: (pattern) => {
141
- for (let i = 0; i < pattern.length; i++) {
142
- const part = pattern[i];
143
- if (typeof part === "string") {
144
- pattern[i] = (0, import_pseudo_localization.localize)(part, options);
145
- }
146
- }
147
- }
148
- });
149
- return (0, import_messageformat2.stringifyMessage)(msg);
150
- }
151
- get attributes() {
152
- return this._attributes;
153
- }
154
- set attributes(attributes) {
155
- this._attributes = attributes;
156
- }
157
- get notes() {
158
- return this._notes;
159
- }
160
- set notes(notes) {
161
- this._notes = notes;
162
- }
163
- addNote(note) {
164
- this.notes.push(note);
165
- }
166
- get title() {
167
- return this._title;
168
- }
169
- set title(title) {
170
- this._title = title;
171
- }
172
- getProject() {
173
- return this._project;
174
- }
175
- add(key, value, attributes, notes) {
176
- const merged = { ...this.attributes, ...attributes };
177
- const msg = MsgMessage.create({
178
- key,
179
- value,
180
- attributes: merged,
181
- notes
182
- });
183
- this.set(key, msg);
184
- return this;
185
- }
186
- translate(data) {
187
- const { title, attributes, messages } = data;
188
- if (title !== this.title) {
189
- throw new TypeError("Title of resource and translations do not match.");
190
- }
191
- const translated = _MsgResource.create({
192
- title,
193
- attributes,
194
- notes: this.notes
195
- // transfer the notes
196
- }, this._project);
197
- this.forEach((msg) => {
198
- translated.set(msg.key, msg);
199
- });
200
- messages?.forEach((messageData) => {
201
- const { key, value, attributes: attributes2 } = messageData;
202
- const msg = MsgMessage.create({
203
- key,
204
- value,
205
- attributes: attributes2
206
- });
207
- const notes = this.get(key)?.notes || [];
208
- notes.forEach((note) => {
209
- msg.addNote(note);
210
- });
211
- translated.set(key, msg);
212
- });
213
- return translated;
214
- }
215
- async getTranslation(lang) {
216
- const project = this._project;
217
- const pseudoLocale = project.locales.pseudoLocale;
218
- if (lang === pseudoLocale) {
219
- const pseudolocalizedData = {
220
- title: this.title,
221
- attributes: { ...this.attributes, lang: pseudoLocale },
222
- notes: this.notes.length > 0 ? this.notes : void 0,
223
- messages: []
224
- };
225
- this.forEach((msg) => {
226
- pseudolocalizedData.messages.push({
227
- key: msg.key,
228
- value: this.pseudoLocalizeMF2(msg.value),
229
- attributes: { ...this.attributes, lang: pseudoLocale }
230
- });
231
- });
232
- return this.translate(pseudolocalizedData);
233
- }
234
- const languageChain = project.getTargetLocale(lang);
235
- if (!languageChain) {
236
- throw new Error("Unsupported locale for resource.");
237
- }
238
- if (languageChain.length == 0) {
239
- throw new Error(`Empty language chain for locale: ${lang}`);
240
- }
241
- let translated = this;
242
- for (let i = 0; i < languageChain.length; i++) {
243
- const lang2 = languageChain[i];
244
- if (lang2 && project._locales.targetLocales[lang2]) {
245
- translated = await translated._project._loader(project.project.name, translated.title, lang2).then((data) => translated.translate(data));
246
- }
247
- }
248
- return translated;
249
- }
250
- getData(stripNotes = false) {
251
- const messages = [];
252
- this.forEach((msg) => {
253
- if (this.hasMatchingAttributes(msg)) {
254
- const data = {
255
- key: msg.key,
256
- value: msg.value,
257
- notes: !stripNotes && msg.notes.length > 0 ? msg.notes : void 0
258
- };
259
- messages.push(data);
260
- } else {
261
- messages.push(msg.getData(stripNotes));
262
- }
263
- });
264
- return {
265
- title: this.title,
266
- attributes: this.attributes,
267
- notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0,
268
- messages
269
- };
270
- }
271
- toJSON(stripNotes = false) {
272
- return JSON.stringify(this.getData(stripNotes), null, 2);
273
- }
274
- };
275
-
276
- // src/classes/MsgProject/MsgProject.ts
277
- var defaultProjectSettings = {
278
- name: "messages",
279
- version: 1
280
- };
281
- var defaultLocalesSettings = {
282
- sourceLocale: "en",
283
- pseudoLocale: "en-XA",
284
- targetLocales: {
285
- en: ["en"]
286
- }
287
- };
288
- var MsgProject = class _MsgProject {
289
- _project;
290
- _locales;
291
- _loader;
292
- static create(data) {
293
- const { project, locales, loader } = data;
294
- return new _MsgProject(project, locales, loader);
295
- }
296
- constructor(projectSettings, localesSettings, loader) {
297
- this._project = { ...defaultProjectSettings, ...projectSettings };
298
- this._locales = { ...defaultLocalesSettings, ...localesSettings };
299
- this._loader = loader;
300
- }
301
- get project() {
302
- return this._project;
303
- }
304
- get locales() {
305
- return this._locales;
306
- }
307
- get loader() {
308
- return this._loader;
309
- }
310
- getTargetLocale(locale) {
311
- return this._locales.targetLocales[locale];
312
- }
313
- };
314
- // Annotate the CommonJS export names for ESM import in node:
315
- 0 && (module.exports = {
316
- MsgMessage,
317
- MsgProject,
318
- MsgResource
319
- });
@@ -1,4 +0,0 @@
1
- export { MsgMessage, MsgMessageData } from './MsgMessage/MsgMessage.cjs';
2
- export { MsgProject, MsgProjectData, a as MsgResource, M as MsgResourceData, MsgTranslationLoader } from './MsgProject/MsgProject.cjs';
3
- import 'messageformat';
4
- import './MsgInterface/MsgInterface.cjs';
@@ -1,4 +0,0 @@
1
- export { MsgMessage, MsgMessageData } from './MsgMessage/MsgMessage.js';
2
- export { MsgProject, MsgProjectData, a as MsgResource, M as MsgResourceData, MsgTranslationLoader } from './MsgProject/MsgProject.js';
3
- import 'messageformat';
4
- import './MsgInterface/MsgInterface.js';
@@ -1,16 +0,0 @@
1
- import "../chunk-ZKG5QRBZ.mjs";
2
- import {
3
- MsgProject
4
- } from "../chunk-XS43NAP2.mjs";
5
- import {
6
- MsgResource
7
- } from "../chunk-2QUOYESW.mjs";
8
- import {
9
- MsgMessage
10
- } from "../chunk-WUDKNZV2.mjs";
11
- import "../chunk-QWBDIKQK.mjs";
12
- export {
13
- MsgMessage,
14
- MsgProject,
15
- MsgResource
16
- };
package/dist/index.cjs DELETED
@@ -1,319 +0,0 @@
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
- MsgMessage: () => MsgMessage,
24
- MsgProject: () => MsgProject,
25
- MsgResource: () => MsgResource
26
- });
27
- module.exports = __toCommonJS(index_exports);
28
-
29
- // src/classes/MsgMessage/MsgMessage.ts
30
- var import_messageformat = require("messageformat");
31
-
32
- // src/classes/MsgInterface/MsgInterface.ts
33
- var DEFAULT_ATTRIBUTES = {
34
- lang: "und",
35
- dir: "auto",
36
- dnt: false
37
- };
38
-
39
- // src/classes/MsgMessage/MsgMessage.ts
40
- var MsgMessage = class _MsgMessage {
41
- _key;
42
- _value;
43
- _mf;
44
- _attributes;
45
- _notes = [];
46
- constructor(key, value, attributes, notes) {
47
- this._key = key;
48
- this._value = value;
49
- this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
50
- if (notes) {
51
- notes.forEach((note) => this.addNote(note));
52
- }
53
- }
54
- static create(data) {
55
- const { key, value, attributes, notes } = data;
56
- const message = new _MsgMessage(key, value, attributes, notes);
57
- return message;
58
- }
59
- get key() {
60
- return this._key;
61
- }
62
- get value() {
63
- return this._value;
64
- }
65
- get attributes() {
66
- return this._attributes;
67
- }
68
- get notes() {
69
- return this._notes;
70
- }
71
- addNote(note) {
72
- this.notes.push(note);
73
- }
74
- format(data, options) {
75
- if (!this._mf) {
76
- this._mf = new import_messageformat.MessageFormat(this.attributes.lang, this.value, options);
77
- }
78
- return this._mf.format(data);
79
- }
80
- formatToParts(data, options) {
81
- if (!this._mf) {
82
- this._mf = new import_messageformat.MessageFormat(this.attributes.lang, this.value, options);
83
- }
84
- return this._mf?.formatToParts(data);
85
- }
86
- getData(stripNotes = false) {
87
- return {
88
- key: this.key,
89
- value: this.value,
90
- attributes: this.attributes,
91
- notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0
92
- };
93
- }
94
- toString() {
95
- return this.value;
96
- }
97
- toJSON(stripNotes = false) {
98
- return JSON.stringify(this.getData(stripNotes), null, 2);
99
- }
100
- };
101
-
102
- // src/classes/MsgResource/MsgResource.ts
103
- var import_messageformat2 = require("messageformat");
104
- var import_pseudo_localization = require("pseudo-localization");
105
- var MsgResource = class _MsgResource extends Map {
106
- _attributes = {};
107
- _notes = [];
108
- _title;
109
- _project;
110
- static create(data, project) {
111
- const { title, attributes, notes, messages } = data;
112
- const res = new _MsgResource(title, attributes, project, notes);
113
- if (messages) {
114
- messages.forEach((messageData) => {
115
- const { key, value, attributes: attributes2, notes: notes2 } = messageData;
116
- res.add(key, value, attributes2, notes2);
117
- });
118
- } else {
119
- res.clear();
120
- }
121
- return res;
122
- }
123
- constructor(title, attributes, project, notes) {
124
- super();
125
- this._title = title;
126
- this._attributes = { ...DEFAULT_ATTRIBUTES, ...attributes };
127
- this._project = project;
128
- if (notes) {
129
- notes.forEach((note) => this.addNote(note));
130
- }
131
- }
132
- hasMatchingAttributes(message) {
133
- const res = this.attributes;
134
- const msg = message.attributes;
135
- return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt;
136
- }
137
- pseudoLocalizeMF2(source, options) {
138
- const msg = (0, import_messageformat2.parseMessage)(source);
139
- (0, import_messageformat2.visit)(msg, {
140
- pattern: (pattern) => {
141
- for (let i = 0; i < pattern.length; i++) {
142
- const part = pattern[i];
143
- if (typeof part === "string") {
144
- pattern[i] = (0, import_pseudo_localization.localize)(part, options);
145
- }
146
- }
147
- }
148
- });
149
- return (0, import_messageformat2.stringifyMessage)(msg);
150
- }
151
- get attributes() {
152
- return this._attributes;
153
- }
154
- set attributes(attributes) {
155
- this._attributes = attributes;
156
- }
157
- get notes() {
158
- return this._notes;
159
- }
160
- set notes(notes) {
161
- this._notes = notes;
162
- }
163
- addNote(note) {
164
- this.notes.push(note);
165
- }
166
- get title() {
167
- return this._title;
168
- }
169
- set title(title) {
170
- this._title = title;
171
- }
172
- getProject() {
173
- return this._project;
174
- }
175
- add(key, value, attributes, notes) {
176
- const merged = { ...this.attributes, ...attributes };
177
- const msg = MsgMessage.create({
178
- key,
179
- value,
180
- attributes: merged,
181
- notes
182
- });
183
- this.set(key, msg);
184
- return this;
185
- }
186
- translate(data) {
187
- const { title, attributes, messages } = data;
188
- if (title !== this.title) {
189
- throw new TypeError("Title of resource and translations do not match.");
190
- }
191
- const translated = _MsgResource.create({
192
- title,
193
- attributes,
194
- notes: this.notes
195
- // transfer the notes
196
- }, this._project);
197
- this.forEach((msg) => {
198
- translated.set(msg.key, msg);
199
- });
200
- messages?.forEach((messageData) => {
201
- const { key, value, attributes: attributes2 } = messageData;
202
- const msg = MsgMessage.create({
203
- key,
204
- value,
205
- attributes: attributes2
206
- });
207
- const notes = this.get(key)?.notes || [];
208
- notes.forEach((note) => {
209
- msg.addNote(note);
210
- });
211
- translated.set(key, msg);
212
- });
213
- return translated;
214
- }
215
- async getTranslation(lang) {
216
- const project = this._project;
217
- const pseudoLocale = project.locales.pseudoLocale;
218
- if (lang === pseudoLocale) {
219
- const pseudolocalizedData = {
220
- title: this.title,
221
- attributes: { ...this.attributes, lang: pseudoLocale },
222
- notes: this.notes.length > 0 ? this.notes : void 0,
223
- messages: []
224
- };
225
- this.forEach((msg) => {
226
- pseudolocalizedData.messages.push({
227
- key: msg.key,
228
- value: this.pseudoLocalizeMF2(msg.value),
229
- attributes: { ...this.attributes, lang: pseudoLocale }
230
- });
231
- });
232
- return this.translate(pseudolocalizedData);
233
- }
234
- const languageChain = project.getTargetLocale(lang);
235
- if (!languageChain) {
236
- throw new Error("Unsupported locale for resource.");
237
- }
238
- if (languageChain.length == 0) {
239
- throw new Error(`Empty language chain for locale: ${lang}`);
240
- }
241
- let translated = this;
242
- for (let i = 0; i < languageChain.length; i++) {
243
- const lang2 = languageChain[i];
244
- if (lang2 && project._locales.targetLocales[lang2]) {
245
- translated = await translated._project._loader(project.project.name, translated.title, lang2).then((data) => translated.translate(data));
246
- }
247
- }
248
- return translated;
249
- }
250
- getData(stripNotes = false) {
251
- const messages = [];
252
- this.forEach((msg) => {
253
- if (this.hasMatchingAttributes(msg)) {
254
- const data = {
255
- key: msg.key,
256
- value: msg.value,
257
- notes: !stripNotes && msg.notes.length > 0 ? msg.notes : void 0
258
- };
259
- messages.push(data);
260
- } else {
261
- messages.push(msg.getData(stripNotes));
262
- }
263
- });
264
- return {
265
- title: this.title,
266
- attributes: this.attributes,
267
- notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0,
268
- messages
269
- };
270
- }
271
- toJSON(stripNotes = false) {
272
- return JSON.stringify(this.getData(stripNotes), null, 2);
273
- }
274
- };
275
-
276
- // src/classes/MsgProject/MsgProject.ts
277
- var defaultProjectSettings = {
278
- name: "messages",
279
- version: 1
280
- };
281
- var defaultLocalesSettings = {
282
- sourceLocale: "en",
283
- pseudoLocale: "en-XA",
284
- targetLocales: {
285
- en: ["en"]
286
- }
287
- };
288
- var MsgProject = class _MsgProject {
289
- _project;
290
- _locales;
291
- _loader;
292
- static create(data) {
293
- const { project, locales, loader } = data;
294
- return new _MsgProject(project, locales, loader);
295
- }
296
- constructor(projectSettings, localesSettings, loader) {
297
- this._project = { ...defaultProjectSettings, ...projectSettings };
298
- this._locales = { ...defaultLocalesSettings, ...localesSettings };
299
- this._loader = loader;
300
- }
301
- get project() {
302
- return this._project;
303
- }
304
- get locales() {
305
- return this._locales;
306
- }
307
- get loader() {
308
- return this._loader;
309
- }
310
- getTargetLocale(locale) {
311
- return this._locales.targetLocales[locale];
312
- }
313
- };
314
- // Annotate the CommonJS export names for ESM import in node:
315
- 0 && (module.exports = {
316
- MsgMessage,
317
- MsgProject,
318
- MsgResource
319
- });
package/dist/index.d.cts DELETED
@@ -1,4 +0,0 @@
1
- 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
- import 'messageformat';
4
- import './classes/MsgInterface/MsgInterface.cjs';
package/dist/index.d.ts DELETED
@@ -1,4 +0,0 @@
1
- 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
- import 'messageformat';
4
- import './classes/MsgInterface/MsgInterface.js';
package/dist/index.mjs DELETED
@@ -1,16 +0,0 @@
1
- import "./chunk-ZKG5QRBZ.mjs";
2
- import {
3
- MsgProject
4
- } from "./chunk-XS43NAP2.mjs";
5
- import {
6
- MsgResource
7
- } from "./chunk-2QUOYESW.mjs";
8
- import {
9
- MsgMessage
10
- } from "./chunk-WUDKNZV2.mjs";
11
- import "./chunk-QWBDIKQK.mjs";
12
- export {
13
- MsgMessage,
14
- MsgProject,
15
- MsgResource
16
- };