@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/chunk-HCR2UNSE.mjs +85 -0
- package/dist/chunk-NWJ7MVVV.mjs +54 -0
- package/dist/chunk-ORHP7HGX.mjs +0 -0
- package/dist/chunk-X6CRKSNU.mjs +207 -0
- package/dist/chunk-XQVALCKX.mjs +12 -0
- package/dist/classes/MsgInterface/MsgInterface.cjs +37 -0
- package/dist/classes/MsgInterface/MsgInterface.d.cts +31 -0
- package/dist/classes/MsgInterface/MsgInterface.d.ts +31 -0
- package/dist/classes/MsgInterface/MsgInterface.mjs +8 -0
- package/dist/classes/MsgMessage/MsgMessage.cjs +114 -0
- package/dist/classes/MsgMessage/MsgMessage.d.cts +41 -0
- package/dist/classes/MsgMessage/MsgMessage.d.ts +41 -0
- package/dist/classes/MsgMessage/MsgMessage.mjs +7 -0
- package/dist/classes/MsgProject/MsgProject.cjs +79 -0
- package/dist/classes/MsgProject/MsgProject.d.cts +4 -0
- package/dist/classes/MsgProject/MsgProject.d.ts +4 -0
- package/dist/classes/MsgProject/MsgProject.mjs +7 -0
- package/dist/classes/MsgResource/MsgResource.cjs +312 -0
- package/dist/classes/MsgResource/MsgResource.d.cts +79 -0
- package/dist/classes/MsgResource/MsgResource.d.ts +79 -0
- package/dist/classes/MsgResource/MsgResource.mjs +8 -0
- package/dist/classes/index.cjs +365 -0
- package/dist/classes/index.d.cts +4 -0
- package/dist/classes/index.d.ts +4 -0
- package/dist/classes/index.mjs +21 -0
- package/dist/index.cjs +365 -0
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.mjs +21 -0
- package/package.json +2 -1
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_ATTRIBUTES,
|
|
3
|
+
MSG_DEFAULT_FORMAT
|
|
4
|
+
} from "./chunk-XQVALCKX.mjs";
|
|
5
|
+
|
|
6
|
+
// src/classes/MsgMessage/MsgMessage.ts
|
|
7
|
+
import { MessageFormat } from "messageformat";
|
|
8
|
+
import { mf1ToMessage } from "@messageformat/icu-messageformat-1";
|
|
9
|
+
var MsgMessage = class _MsgMessage {
|
|
10
|
+
_key;
|
|
11
|
+
_value;
|
|
12
|
+
_mf;
|
|
13
|
+
_attributes;
|
|
14
|
+
_notes = [];
|
|
15
|
+
constructor(key, value, attributes, notes) {
|
|
16
|
+
this._key = key;
|
|
17
|
+
this._value = value;
|
|
18
|
+
this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
|
|
19
|
+
if (notes) {
|
|
20
|
+
notes.forEach((note) => this.addNote(note));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
static create(data) {
|
|
24
|
+
const { key, value, attributes, notes } = data;
|
|
25
|
+
const message = new _MsgMessage(key, value, attributes, notes);
|
|
26
|
+
return message;
|
|
27
|
+
}
|
|
28
|
+
get key() {
|
|
29
|
+
return this._key;
|
|
30
|
+
}
|
|
31
|
+
get value() {
|
|
32
|
+
return this._value;
|
|
33
|
+
}
|
|
34
|
+
get attributes() {
|
|
35
|
+
return this._attributes;
|
|
36
|
+
}
|
|
37
|
+
get notes() {
|
|
38
|
+
return this._notes;
|
|
39
|
+
}
|
|
40
|
+
addNote(note) {
|
|
41
|
+
this.notes.push(note);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
45
|
+
*/
|
|
46
|
+
resolveFormat() {
|
|
47
|
+
return this.attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
48
|
+
}
|
|
49
|
+
getFormatter(options) {
|
|
50
|
+
if (!this._mf) {
|
|
51
|
+
this._mf = this.resolveFormat() === "MF1" ? mf1ToMessage(this.attributes.lang, this.value, options) : new MessageFormat(this.attributes.lang, this.value, options);
|
|
52
|
+
}
|
|
53
|
+
return this._mf;
|
|
54
|
+
}
|
|
55
|
+
format(data, options) {
|
|
56
|
+
if (this.resolveFormat() === "NONE") {
|
|
57
|
+
return this.value;
|
|
58
|
+
}
|
|
59
|
+
return this.getFormatter(options).format(data);
|
|
60
|
+
}
|
|
61
|
+
formatToParts(data, options) {
|
|
62
|
+
if (this.resolveFormat() === "NONE") {
|
|
63
|
+
return [{ type: "text", value: this.value }];
|
|
64
|
+
}
|
|
65
|
+
return this.getFormatter(options).formatToParts(data);
|
|
66
|
+
}
|
|
67
|
+
getData(stripNotes = false) {
|
|
68
|
+
return {
|
|
69
|
+
key: this.key,
|
|
70
|
+
value: this.value,
|
|
71
|
+
attributes: this.attributes,
|
|
72
|
+
notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
toString() {
|
|
76
|
+
return this.value;
|
|
77
|
+
}
|
|
78
|
+
toJSON(stripNotes = false) {
|
|
79
|
+
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export {
|
|
84
|
+
MsgMessage
|
|
85
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MSG_DEFAULT_FORMAT
|
|
3
|
+
} from "./chunk-XQVALCKX.mjs";
|
|
4
|
+
|
|
5
|
+
// src/classes/MsgProject/MsgProject.ts
|
|
6
|
+
var defaultProjectSettings = {
|
|
7
|
+
name: "messages",
|
|
8
|
+
version: 1,
|
|
9
|
+
format: MSG_DEFAULT_FORMAT
|
|
10
|
+
};
|
|
11
|
+
var defaultLocalesSettings = {
|
|
12
|
+
sourceLocale: "en",
|
|
13
|
+
pseudoLocale: "en-XA",
|
|
14
|
+
targetLocales: {
|
|
15
|
+
en: ["en"]
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var MsgProject = class _MsgProject {
|
|
19
|
+
_project;
|
|
20
|
+
_locales;
|
|
21
|
+
_loader;
|
|
22
|
+
static create(data) {
|
|
23
|
+
const { project, locales, loader } = data;
|
|
24
|
+
return new _MsgProject(project, locales, loader);
|
|
25
|
+
}
|
|
26
|
+
constructor(projectSettings, localesSettings, loader) {
|
|
27
|
+
this._project = { ...defaultProjectSettings, ...projectSettings };
|
|
28
|
+
this._locales = { ...defaultLocalesSettings, ...localesSettings };
|
|
29
|
+
this._loader = loader;
|
|
30
|
+
}
|
|
31
|
+
get project() {
|
|
32
|
+
return this._project;
|
|
33
|
+
}
|
|
34
|
+
get locales() {
|
|
35
|
+
return this._locales;
|
|
36
|
+
}
|
|
37
|
+
get loader() {
|
|
38
|
+
return this._loader;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The project-wide default message format. Resources (and, through them,
|
|
42
|
+
* messages) inherit this value unless they specify their own.
|
|
43
|
+
*/
|
|
44
|
+
get format() {
|
|
45
|
+
return this._project.format ?? MSG_DEFAULT_FORMAT;
|
|
46
|
+
}
|
|
47
|
+
getTargetLocale(locale) {
|
|
48
|
+
return this._locales.targetLocales[locale];
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export {
|
|
53
|
+
MsgProject
|
|
54
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MsgMessage
|
|
3
|
+
} from "./chunk-HCR2UNSE.mjs";
|
|
4
|
+
import {
|
|
5
|
+
DEFAULT_ATTRIBUTES,
|
|
6
|
+
MSG_DEFAULT_FORMAT
|
|
7
|
+
} from "./chunk-XQVALCKX.mjs";
|
|
8
|
+
|
|
9
|
+
// src/classes/MsgResource/MsgResource.ts
|
|
10
|
+
import { parseMessage, stringifyMessage, visit } from "messageformat";
|
|
11
|
+
import { localize } from "pseudo-localization";
|
|
12
|
+
var MsgResource = class _MsgResource extends Map {
|
|
13
|
+
_attributes = {};
|
|
14
|
+
_notes = [];
|
|
15
|
+
_title;
|
|
16
|
+
_project;
|
|
17
|
+
static create(data, project) {
|
|
18
|
+
const { title, attributes, notes, messages } = data;
|
|
19
|
+
const res = new _MsgResource(title, attributes, project, notes);
|
|
20
|
+
if (messages) {
|
|
21
|
+
messages.forEach((messageData) => {
|
|
22
|
+
const { key, value, attributes: attributes2, notes: notes2 } = messageData;
|
|
23
|
+
res.add(key, value, attributes2, notes2);
|
|
24
|
+
});
|
|
25
|
+
} else {
|
|
26
|
+
res.clear();
|
|
27
|
+
}
|
|
28
|
+
return res;
|
|
29
|
+
}
|
|
30
|
+
constructor(title, attributes, project, notes) {
|
|
31
|
+
super();
|
|
32
|
+
this._title = title;
|
|
33
|
+
this._attributes = { ...DEFAULT_ATTRIBUTES, format: project.format, ...attributes };
|
|
34
|
+
this._project = project;
|
|
35
|
+
if (notes) {
|
|
36
|
+
notes.forEach((note) => this.addNote(note));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
hasMatchingAttributes(message) {
|
|
40
|
+
const res = this.attributes;
|
|
41
|
+
const msg = message.attributes;
|
|
42
|
+
return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt && this.resolveFormat(res) === this.resolveFormat(msg);
|
|
43
|
+
}
|
|
44
|
+
resolveFormat(attributes) {
|
|
45
|
+
return attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
46
|
+
}
|
|
47
|
+
pseudoLocalizeMF2(source, options) {
|
|
48
|
+
const msg = parseMessage(source);
|
|
49
|
+
visit(msg, {
|
|
50
|
+
pattern: (pattern) => {
|
|
51
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
52
|
+
const part = pattern[i];
|
|
53
|
+
if (typeof part === "string") {
|
|
54
|
+
pattern[i] = localize(part, options);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
return stringifyMessage(msg);
|
|
60
|
+
}
|
|
61
|
+
get attributes() {
|
|
62
|
+
return this._attributes;
|
|
63
|
+
}
|
|
64
|
+
set attributes(attributes) {
|
|
65
|
+
this._attributes = attributes;
|
|
66
|
+
}
|
|
67
|
+
get notes() {
|
|
68
|
+
return this._notes;
|
|
69
|
+
}
|
|
70
|
+
set notes(notes) {
|
|
71
|
+
this._notes = notes;
|
|
72
|
+
}
|
|
73
|
+
addNote(note) {
|
|
74
|
+
this.notes.push(note);
|
|
75
|
+
}
|
|
76
|
+
get title() {
|
|
77
|
+
return this._title;
|
|
78
|
+
}
|
|
79
|
+
set title(title) {
|
|
80
|
+
this._title = title;
|
|
81
|
+
}
|
|
82
|
+
getProject() {
|
|
83
|
+
return this._project;
|
|
84
|
+
}
|
|
85
|
+
add(key, value, attributes, notes) {
|
|
86
|
+
const merged = { ...this.attributes, ...attributes };
|
|
87
|
+
const msg = MsgMessage.create({
|
|
88
|
+
key,
|
|
89
|
+
value,
|
|
90
|
+
attributes: merged,
|
|
91
|
+
notes
|
|
92
|
+
});
|
|
93
|
+
this.set(key, msg);
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
translate(data) {
|
|
97
|
+
const { title, attributes, messages } = data;
|
|
98
|
+
if (title !== this.title) {
|
|
99
|
+
throw new TypeError("Title of resource and translations do not match.");
|
|
100
|
+
}
|
|
101
|
+
const translated = _MsgResource.create({
|
|
102
|
+
title,
|
|
103
|
+
// preserve the source resource's format unless the translation overrides it
|
|
104
|
+
attributes: this.preserveFormat(attributes, this.attributes.format),
|
|
105
|
+
notes: this.notes
|
|
106
|
+
// transfer the notes
|
|
107
|
+
}, this._project);
|
|
108
|
+
this.forEach((msg) => {
|
|
109
|
+
translated.set(msg.key, msg);
|
|
110
|
+
});
|
|
111
|
+
messages?.forEach((messageData) => {
|
|
112
|
+
const { key, value, attributes: attributes2 } = messageData;
|
|
113
|
+
const msg = MsgMessage.create({
|
|
114
|
+
key,
|
|
115
|
+
value,
|
|
116
|
+
// preserve the source message's format unless the translation overrides it
|
|
117
|
+
attributes: this.preserveFormat(attributes2, this.get(key)?.attributes.format)
|
|
118
|
+
});
|
|
119
|
+
const notes = this.get(key)?.notes || [];
|
|
120
|
+
notes.forEach((note) => {
|
|
121
|
+
msg.addNote(note);
|
|
122
|
+
});
|
|
123
|
+
translated.set(key, msg);
|
|
124
|
+
});
|
|
125
|
+
return translated;
|
|
126
|
+
}
|
|
127
|
+
preserveFormat(attributes, format) {
|
|
128
|
+
if (format === void 0 || attributes?.format !== void 0) {
|
|
129
|
+
return attributes;
|
|
130
|
+
}
|
|
131
|
+
return { ...attributes, format };
|
|
132
|
+
}
|
|
133
|
+
async getTranslation(lang) {
|
|
134
|
+
const project = this._project;
|
|
135
|
+
const pseudoLocale = project.locales.pseudoLocale;
|
|
136
|
+
if (lang === pseudoLocale) {
|
|
137
|
+
const pseudolocalizedData = {
|
|
138
|
+
title: this.title,
|
|
139
|
+
attributes: { ...this.attributes, lang: pseudoLocale },
|
|
140
|
+
notes: this.notes.length > 0 ? this.notes : void 0,
|
|
141
|
+
messages: []
|
|
142
|
+
};
|
|
143
|
+
this.forEach((msg) => {
|
|
144
|
+
pseudolocalizedData.messages.push({
|
|
145
|
+
key: msg.key,
|
|
146
|
+
value: this.pseudoLocalizeMF2(msg.value),
|
|
147
|
+
attributes: { ...this.attributes, lang: pseudoLocale }
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
return this.translate(pseudolocalizedData);
|
|
151
|
+
}
|
|
152
|
+
const languageChain = project.getTargetLocale(lang);
|
|
153
|
+
if (!languageChain) {
|
|
154
|
+
throw new Error("Unsupported locale for resource.");
|
|
155
|
+
}
|
|
156
|
+
if (languageChain.length == 0) {
|
|
157
|
+
throw new Error(`Empty language chain for locale: ${lang}`);
|
|
158
|
+
}
|
|
159
|
+
let translated = this;
|
|
160
|
+
for (let i = 0; i < languageChain.length; i++) {
|
|
161
|
+
const lang2 = languageChain[i];
|
|
162
|
+
if (lang2 && project._locales.targetLocales[lang2]) {
|
|
163
|
+
translated = await translated._project._loader(project.project.name, translated.title, lang2).then((data) => translated.translate(data));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return translated;
|
|
167
|
+
}
|
|
168
|
+
getData(stripNotes = false) {
|
|
169
|
+
const resourceFormat = this.resolveFormat(this.attributes);
|
|
170
|
+
const projectFormat = this._project.format;
|
|
171
|
+
const messages = [];
|
|
172
|
+
this.forEach((msg) => {
|
|
173
|
+
if (this.hasMatchingAttributes(msg)) {
|
|
174
|
+
const data = {
|
|
175
|
+
key: msg.key,
|
|
176
|
+
value: msg.value,
|
|
177
|
+
notes: !stripNotes && msg.notes.length > 0 ? msg.notes : void 0
|
|
178
|
+
};
|
|
179
|
+
messages.push(data);
|
|
180
|
+
} else {
|
|
181
|
+
const data = msg.getData(stripNotes);
|
|
182
|
+
if (data.attributes && this.resolveFormat(data.attributes) === resourceFormat) {
|
|
183
|
+
const { format, ...rest } = data.attributes;
|
|
184
|
+
data.attributes = rest;
|
|
185
|
+
}
|
|
186
|
+
messages.push(data);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
const attributes = { ...this.attributes };
|
|
190
|
+
if (this.resolveFormat(attributes) === projectFormat) {
|
|
191
|
+
delete attributes.format;
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
title: this.title,
|
|
195
|
+
attributes,
|
|
196
|
+
notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0,
|
|
197
|
+
messages
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
toJSON(stripNotes = false) {
|
|
201
|
+
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
export {
|
|
206
|
+
MsgResource
|
|
207
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
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/MsgInterface/MsgInterface.ts
|
|
21
|
+
var MsgInterface_exports = {};
|
|
22
|
+
__export(MsgInterface_exports, {
|
|
23
|
+
DEFAULT_ATTRIBUTES: () => DEFAULT_ATTRIBUTES,
|
|
24
|
+
MSG_DEFAULT_FORMAT: () => MSG_DEFAULT_FORMAT
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(MsgInterface_exports);
|
|
27
|
+
var MSG_DEFAULT_FORMAT = "MF2";
|
|
28
|
+
var DEFAULT_ATTRIBUTES = {
|
|
29
|
+
lang: "und",
|
|
30
|
+
dir: "auto",
|
|
31
|
+
dnt: false
|
|
32
|
+
};
|
|
33
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
34
|
+
0 && (module.exports = {
|
|
35
|
+
DEFAULT_ATTRIBUTES,
|
|
36
|
+
MSG_DEFAULT_FORMAT
|
|
37
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
type NoteTypes = 'DESCRIPTION' | 'AUTHORSHIP' | 'PARAMETERS' | 'CONTEXT' | 'COMMENT';
|
|
2
|
+
type MsgNote = {
|
|
3
|
+
type: NoteTypes;
|
|
4
|
+
content: string;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* The message formatting syntax a string is written in.
|
|
8
|
+
*
|
|
9
|
+
* - `MF1` — ICU MessageFormat 1 (formatted via `@messageformat/icu-messageformat-1`).
|
|
10
|
+
* - `MF2` — Unicode MessageFormat 2 (the default, for backwards compatibility).
|
|
11
|
+
* - `NONE` — not a formatted message; the raw string is returned as-is.
|
|
12
|
+
*/
|
|
13
|
+
type MsgFormat = 'MF1' | 'MF2' | 'NONE';
|
|
14
|
+
/**
|
|
15
|
+
* The format applied when a message does not specify (or inherit) one.
|
|
16
|
+
* Kept at MF2 for backwards compatibility.
|
|
17
|
+
*/
|
|
18
|
+
declare const MSG_DEFAULT_FORMAT: MsgFormat;
|
|
19
|
+
type MsgAttributes = {
|
|
20
|
+
lang?: string;
|
|
21
|
+
dir?: string;
|
|
22
|
+
dnt?: boolean;
|
|
23
|
+
format?: MsgFormat;
|
|
24
|
+
};
|
|
25
|
+
declare const DEFAULT_ATTRIBUTES: MsgAttributes;
|
|
26
|
+
interface MsgInterface {
|
|
27
|
+
attributes: MsgAttributes;
|
|
28
|
+
notes: MsgNote[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, type MsgAttributes, type MsgFormat, type MsgInterface, type MsgNote };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
type NoteTypes = 'DESCRIPTION' | 'AUTHORSHIP' | 'PARAMETERS' | 'CONTEXT' | 'COMMENT';
|
|
2
|
+
type MsgNote = {
|
|
3
|
+
type: NoteTypes;
|
|
4
|
+
content: string;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* The message formatting syntax a string is written in.
|
|
8
|
+
*
|
|
9
|
+
* - `MF1` — ICU MessageFormat 1 (formatted via `@messageformat/icu-messageformat-1`).
|
|
10
|
+
* - `MF2` — Unicode MessageFormat 2 (the default, for backwards compatibility).
|
|
11
|
+
* - `NONE` — not a formatted message; the raw string is returned as-is.
|
|
12
|
+
*/
|
|
13
|
+
type MsgFormat = 'MF1' | 'MF2' | 'NONE';
|
|
14
|
+
/**
|
|
15
|
+
* The format applied when a message does not specify (or inherit) one.
|
|
16
|
+
* Kept at MF2 for backwards compatibility.
|
|
17
|
+
*/
|
|
18
|
+
declare const MSG_DEFAULT_FORMAT: MsgFormat;
|
|
19
|
+
type MsgAttributes = {
|
|
20
|
+
lang?: string;
|
|
21
|
+
dir?: string;
|
|
22
|
+
dnt?: boolean;
|
|
23
|
+
format?: MsgFormat;
|
|
24
|
+
};
|
|
25
|
+
declare const DEFAULT_ATTRIBUTES: MsgAttributes;
|
|
26
|
+
interface MsgInterface {
|
|
27
|
+
attributes: MsgAttributes;
|
|
28
|
+
notes: MsgNote[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, type MsgAttributes, type MsgFormat, type MsgInterface, type MsgNote };
|
|
@@ -0,0 +1,114 @@
|
|
|
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/MsgMessage/MsgMessage.ts
|
|
21
|
+
var MsgMessage_exports = {};
|
|
22
|
+
__export(MsgMessage_exports, {
|
|
23
|
+
MsgMessage: () => MsgMessage
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(MsgMessage_exports);
|
|
26
|
+
var import_messageformat = require("messageformat");
|
|
27
|
+
var import_icu_messageformat_1 = require("@messageformat/icu-messageformat-1");
|
|
28
|
+
|
|
29
|
+
// src/classes/MsgInterface/MsgInterface.ts
|
|
30
|
+
var MSG_DEFAULT_FORMAT = "MF2";
|
|
31
|
+
var DEFAULT_ATTRIBUTES = {
|
|
32
|
+
lang: "und",
|
|
33
|
+
dir: "auto",
|
|
34
|
+
dnt: false
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// src/classes/MsgMessage/MsgMessage.ts
|
|
38
|
+
var MsgMessage = class _MsgMessage {
|
|
39
|
+
_key;
|
|
40
|
+
_value;
|
|
41
|
+
_mf;
|
|
42
|
+
_attributes;
|
|
43
|
+
_notes = [];
|
|
44
|
+
constructor(key, value, attributes, notes) {
|
|
45
|
+
this._key = key;
|
|
46
|
+
this._value = value;
|
|
47
|
+
this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
|
|
48
|
+
if (notes) {
|
|
49
|
+
notes.forEach((note) => this.addNote(note));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
static create(data) {
|
|
53
|
+
const { key, value, attributes, notes } = data;
|
|
54
|
+
const message = new _MsgMessage(key, value, attributes, notes);
|
|
55
|
+
return message;
|
|
56
|
+
}
|
|
57
|
+
get key() {
|
|
58
|
+
return this._key;
|
|
59
|
+
}
|
|
60
|
+
get value() {
|
|
61
|
+
return this._value;
|
|
62
|
+
}
|
|
63
|
+
get attributes() {
|
|
64
|
+
return this._attributes;
|
|
65
|
+
}
|
|
66
|
+
get notes() {
|
|
67
|
+
return this._notes;
|
|
68
|
+
}
|
|
69
|
+
addNote(note) {
|
|
70
|
+
this.notes.push(note);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
74
|
+
*/
|
|
75
|
+
resolveFormat() {
|
|
76
|
+
return this.attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
77
|
+
}
|
|
78
|
+
getFormatter(options) {
|
|
79
|
+
if (!this._mf) {
|
|
80
|
+
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);
|
|
81
|
+
}
|
|
82
|
+
return this._mf;
|
|
83
|
+
}
|
|
84
|
+
format(data, options) {
|
|
85
|
+
if (this.resolveFormat() === "NONE") {
|
|
86
|
+
return this.value;
|
|
87
|
+
}
|
|
88
|
+
return this.getFormatter(options).format(data);
|
|
89
|
+
}
|
|
90
|
+
formatToParts(data, options) {
|
|
91
|
+
if (this.resolveFormat() === "NONE") {
|
|
92
|
+
return [{ type: "text", value: this.value }];
|
|
93
|
+
}
|
|
94
|
+
return this.getFormatter(options).formatToParts(data);
|
|
95
|
+
}
|
|
96
|
+
getData(stripNotes = false) {
|
|
97
|
+
return {
|
|
98
|
+
key: this.key,
|
|
99
|
+
value: this.value,
|
|
100
|
+
attributes: this.attributes,
|
|
101
|
+
notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
toString() {
|
|
105
|
+
return this.value;
|
|
106
|
+
}
|
|
107
|
+
toJSON(stripNotes = false) {
|
|
108
|
+
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
112
|
+
0 && (module.exports = {
|
|
113
|
+
MsgMessage
|
|
114
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import * as messageformat from 'messageformat';
|
|
2
|
+
import { MessageFormatOptions } from 'messageformat';
|
|
3
|
+
import { MsgInterface, MsgAttributes, MsgNote } from '../MsgInterface/MsgInterface.cjs';
|
|
4
|
+
|
|
5
|
+
type MsgMessageData = {
|
|
6
|
+
key: string;
|
|
7
|
+
value: string;
|
|
8
|
+
attributes?: MsgAttributes;
|
|
9
|
+
notes?: MsgNote[];
|
|
10
|
+
};
|
|
11
|
+
declare class MsgMessage implements MsgInterface {
|
|
12
|
+
private _key;
|
|
13
|
+
private _value;
|
|
14
|
+
private _mf?;
|
|
15
|
+
private _attributes;
|
|
16
|
+
private _notes;
|
|
17
|
+
private constructor();
|
|
18
|
+
static create(data: MsgMessageData): MsgMessage;
|
|
19
|
+
get key(): string;
|
|
20
|
+
get value(): string;
|
|
21
|
+
get attributes(): MsgAttributes;
|
|
22
|
+
get notes(): MsgNote[];
|
|
23
|
+
addNote(note: MsgNote): void;
|
|
24
|
+
/**
|
|
25
|
+
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
26
|
+
*/
|
|
27
|
+
private resolveFormat;
|
|
28
|
+
private getFormatter;
|
|
29
|
+
format(data: Record<string, any>, options?: MessageFormatOptions): string;
|
|
30
|
+
formatToParts(data: Record<string, any>, options?: MessageFormatOptions): messageformat.MessagePart<never>[];
|
|
31
|
+
getData(stripNotes?: boolean): {
|
|
32
|
+
key: string;
|
|
33
|
+
value: string;
|
|
34
|
+
attributes: MsgAttributes;
|
|
35
|
+
notes: MsgNote[] | undefined;
|
|
36
|
+
};
|
|
37
|
+
toString(): string;
|
|
38
|
+
toJSON(stripNotes?: boolean): string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { MsgMessage, type MsgMessageData };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import * as messageformat from 'messageformat';
|
|
2
|
+
import { MessageFormatOptions } from 'messageformat';
|
|
3
|
+
import { MsgInterface, MsgAttributes, MsgNote } from '../MsgInterface/MsgInterface.js';
|
|
4
|
+
|
|
5
|
+
type MsgMessageData = {
|
|
6
|
+
key: string;
|
|
7
|
+
value: string;
|
|
8
|
+
attributes?: MsgAttributes;
|
|
9
|
+
notes?: MsgNote[];
|
|
10
|
+
};
|
|
11
|
+
declare class MsgMessage implements MsgInterface {
|
|
12
|
+
private _key;
|
|
13
|
+
private _value;
|
|
14
|
+
private _mf?;
|
|
15
|
+
private _attributes;
|
|
16
|
+
private _notes;
|
|
17
|
+
private constructor();
|
|
18
|
+
static create(data: MsgMessageData): MsgMessage;
|
|
19
|
+
get key(): string;
|
|
20
|
+
get value(): string;
|
|
21
|
+
get attributes(): MsgAttributes;
|
|
22
|
+
get notes(): MsgNote[];
|
|
23
|
+
addNote(note: MsgNote): void;
|
|
24
|
+
/**
|
|
25
|
+
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
26
|
+
*/
|
|
27
|
+
private resolveFormat;
|
|
28
|
+
private getFormatter;
|
|
29
|
+
format(data: Record<string, any>, options?: MessageFormatOptions): string;
|
|
30
|
+
formatToParts(data: Record<string, any>, options?: MessageFormatOptions): messageformat.MessagePart<never>[];
|
|
31
|
+
getData(stripNotes?: boolean): {
|
|
32
|
+
key: string;
|
|
33
|
+
value: string;
|
|
34
|
+
attributes: MsgAttributes;
|
|
35
|
+
notes: MsgNote[] | undefined;
|
|
36
|
+
};
|
|
37
|
+
toString(): string;
|
|
38
|
+
toJSON(stripNotes?: boolean): string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { MsgMessage, type MsgMessageData };
|