@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.
- package/README.md +54 -10
- package/dist/{chunk-WUDKNZV2.mjs → chunk-HCR2UNSE.mjs} +22 -8
- package/dist/{chunk-XS43NAP2.mjs → chunk-NWJ7MVVV.mjs} +13 -1
- package/dist/{chunk-2QUOYESW.mjs → chunk-X6CRKSNU.mjs} +32 -9
- package/dist/{chunk-QWBDIKQK.mjs → chunk-XQVALCKX.mjs} +2 -0
- package/dist/classes/MsgInterface/MsgInterface.cjs +5 -2
- package/dist/classes/MsgInterface/MsgInterface.d.cts +15 -1
- package/dist/classes/MsgInterface/MsgInterface.d.ts +15 -1
- package/dist/classes/MsgInterface/MsgInterface.mjs +5 -3
- package/dist/classes/MsgMessage/MsgMessage.cjs +20 -6
- package/dist/classes/MsgMessage/MsgMessage.d.cts +6 -1
- package/dist/classes/MsgMessage/MsgMessage.d.ts +6 -1
- package/dist/classes/MsgMessage/MsgMessage.mjs +2 -2
- package/dist/classes/MsgProject/MsgProject.cjs +14 -1
- package/dist/classes/MsgProject/MsgProject.d.cts +3 -64
- package/dist/classes/MsgProject/MsgProject.d.ts +3 -64
- package/dist/classes/MsgProject/MsgProject.mjs +2 -1
- package/dist/classes/MsgResource/MsgResource.cjs +48 -12
- package/dist/classes/MsgResource/MsgResource.d.cts +78 -3
- package/dist/classes/MsgResource/MsgResource.d.ts +78 -3
- package/dist/classes/MsgResource/MsgResource.mjs +3 -3
- package/dist/classes/index.cjs +62 -16
- package/dist/classes/index.d.cts +2 -2
- package/dist/classes/index.d.ts +2 -2
- package/dist/classes/index.mjs +12 -7
- package/dist/index.cjs +62 -16
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +12 -7
- package/package.json +8 -3
- /package/dist/{chunk-ZKG5QRBZ.mjs → chunk-ORHP7HGX.mjs} +0 -0
package/README.md
CHANGED
|
@@ -4,12 +4,13 @@ A TypeScript library for managing internationalization (i18n) messages with supp
|
|
|
4
4
|
|
|
5
5
|
## Overview
|
|
6
6
|
|
|
7
|
-
`msg` provides a structured approach to managing translatable messages in your application. It integrates with [MessageFormat 2](https://messageformat.unicode.org/) (MF2) for
|
|
7
|
+
`msg` provides a structured approach to managing translatable messages in your application. It integrates with [MessageFormat 2](https://messageformat.unicode.org/) (MF2) and [ICU MessageFormat 1](https://messageformat.github.io/) (MF1) for message formatting and supports:
|
|
8
8
|
|
|
9
9
|
- **Message Management**: Organize messages into resources with keys and values
|
|
10
10
|
- **Translation Loading**: Load translations from external sources via customizable loaders
|
|
11
11
|
- **Pseudo Localization**: Request a pseudolocalized resource for UI testing via `getTranslation(pseudoLocale)`
|
|
12
|
-
- **Message Formatting**: Format messages with parameters using MessageFormat 2 (MF2) syntax
|
|
12
|
+
- **Message Formatting**: Format messages with parameters using MessageFormat 2 (MF2) or MessageFormat 1 (MF1) syntax, or pass strings through unformatted
|
|
13
|
+
- **Configurable Format**: Choose `MF1`, `MF2`, or `NONE` per project, resource, or message via an inheritable `format` attribute (defaults to `MF2`)
|
|
13
14
|
- **Attributes & Notes**: Attach metadata (language, direction, do-not-translate flags) and notes to messages
|
|
14
15
|
- **Project Configuration**: Configure projects with locale settings and translation loaders
|
|
15
16
|
|
|
@@ -25,6 +26,7 @@ npm install @worldware/msg
|
|
|
25
26
|
|
|
26
27
|
A project configuration that defines:
|
|
27
28
|
- Project name and version
|
|
29
|
+
- The default message `format` (`MF1` | `MF2` | `NONE`, defaults to `MF2`) inherited by resources and messages
|
|
28
30
|
- Source and target locales (with language fallback chains)
|
|
29
31
|
- Pseudo locale (for pseudolocalized output via `getTranslation`)
|
|
30
32
|
- A translation loader function
|
|
@@ -41,10 +43,10 @@ A collection of messages (extends `Map<string, MsgMessage>`) representing a reso
|
|
|
41
43
|
|
|
42
44
|
An individual message with:
|
|
43
45
|
- A key (identifier)
|
|
44
|
-
- A value (the message text,
|
|
45
|
-
- Attributes (lang, dir, dnt)
|
|
46
|
+
- A value (the message text, in MF2, MF1, or plain syntax depending on its `format`)
|
|
47
|
+
- Attributes (lang, dir, dnt, format)
|
|
46
48
|
- Notes
|
|
47
|
-
- Formatting methods
|
|
49
|
+
- Formatting methods that honor the resolved `format` (MF2, MF1, or NONE)
|
|
48
50
|
|
|
49
51
|
## Usage
|
|
50
52
|
|
|
@@ -128,6 +130,42 @@ const formatted = greetingMsg?.format({ name: 'Alice' });
|
|
|
128
130
|
// Result: "Hello, Alice!"
|
|
129
131
|
```
|
|
130
132
|
|
|
133
|
+
### Message Formats (MF1, MF2, NONE)
|
|
134
|
+
|
|
135
|
+
Every message is formatted according to its resolved `format` attribute:
|
|
136
|
+
|
|
137
|
+
- `MF2` (default) — [Unicode MessageFormat 2](https://messageformat.unicode.org/) syntax, e.g. `Hello, {$name}!`.
|
|
138
|
+
- `MF1` — [ICU MessageFormat 1](https://messageformat.github.io/) syntax, e.g. `{count, plural, one {# file} other {# files}}`, formatted via [`@messageformat/icu-messageformat-1`](https://www.npmjs.com/package/@messageformat/icu-messageformat-1).
|
|
139
|
+
- `NONE` — the value is returned verbatim, with no parsing or interpolation.
|
|
140
|
+
|
|
141
|
+
The `format` is inheritable: a resource inherits its project's `format` unless it sets its own, and a message inherits its resource's `format` unless it sets its own. The default is `MF2`, so existing code keeps working unchanged. Use a TypeScript union (`'MF1' | 'MF2' | 'NONE'`) — there is no enum.
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
import { MsgProject, MsgResource } from '@worldware/msg';
|
|
145
|
+
|
|
146
|
+
// A project whose messages are MF1 by default
|
|
147
|
+
const project = MsgProject.create({
|
|
148
|
+
project: { name: 'legacy-app', version: 1, format: 'MF1' },
|
|
149
|
+
locales: { sourceLocale: 'en', pseudoLocale: 'en-XA', targetLocales: { en: ['en'] } },
|
|
150
|
+
loader
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
const resource = MsgResource.create({
|
|
154
|
+
title: 'Files',
|
|
155
|
+
attributes: { lang: 'en', dir: 'ltr' } // inherits format: 'MF1' from the project
|
|
156
|
+
}, project);
|
|
157
|
+
|
|
158
|
+
resource.add('files', '{count, plural, one {# file} other {# files}}'); // MF1 (inherited)
|
|
159
|
+
resource.add('brand', 'msg {version}', { format: 'NONE' }); // passed through
|
|
160
|
+
resource.add('hi', 'Hello, {$name}!', { format: 'MF2' }); // MF2 (override)
|
|
161
|
+
|
|
162
|
+
resource.get('files')?.format({ count: 2 }); // "2 files"
|
|
163
|
+
resource.get('brand')?.format({ version: 1 }); // "msg {version}"
|
|
164
|
+
resource.get('hi')?.format({ name: 'Ada' }); // "Hello, Ada!"
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
When serializing, an inherited `format` is omitted to keep output compact: a resource omits `format` when it equals the project's, and a message omits `format` when it equals its resource's.
|
|
168
|
+
|
|
131
169
|
### Loading Translations
|
|
132
170
|
|
|
133
171
|
```typescript
|
|
@@ -214,9 +252,10 @@ const data = resource.getData();
|
|
|
214
252
|
- `create(data: MsgProjectData): MsgProject` - Create a new project instance
|
|
215
253
|
|
|
216
254
|
**Properties:**
|
|
217
|
-
- `project: MsgProjectSettings` - Project name and
|
|
255
|
+
- `project: MsgProjectSettings` - Project name, version, and default `format`
|
|
218
256
|
- `locales: MsgLocalesSettings` - Locale configuration
|
|
219
257
|
- `loader: MsgTranslationLoader` - Translation loader function
|
|
258
|
+
- `format: MsgFormat` - The project-wide default format (`'MF1' | 'MF2' | 'NONE'`), defaulting to `'MF2'`; resources (and, through them, messages) inherit this value unless they specify their own
|
|
220
259
|
|
|
221
260
|
**Methods:**
|
|
222
261
|
- `getTargetLocale(locale: string): string[] | undefined` - Returns the language fallback chain (array of locale codes) for the specified locale, or `undefined` if the locale is not configured in `targetLocales`
|
|
@@ -231,7 +270,7 @@ const data = resource.getData();
|
|
|
231
270
|
- `translate(data: MsgResourceData): MsgResource` - Create a translated version
|
|
232
271
|
- `getTranslation(lang: string): Promise<MsgResource>` - Load and apply translations. When `lang` matches the project's `pseudoLocale`, returns a resource with pseudolocalized message values instead of loading from the loader.
|
|
233
272
|
- `getProject(): MsgProject` - Returns the project instance associated with the resource
|
|
234
|
-
- `getData(stripNotes?: boolean): MsgResourceData` - Get resource data. Message objects in the output omit `attributes` when they match the resource's attributes (to avoid redundancy)
|
|
273
|
+
- `getData(stripNotes?: boolean): MsgResourceData` - Get resource data. Message objects in the output omit `attributes` when they match the resource's attributes (to avoid redundancy). The resource's `format` is omitted when it equals the project's, and a message's `format` is omitted when it equals the resource's
|
|
235
274
|
- `toJSON(stripNotes?: boolean): string` - Serialize to JSON
|
|
236
275
|
|
|
237
276
|
**Properties:**
|
|
@@ -245,8 +284,8 @@ const data = resource.getData();
|
|
|
245
284
|
- `create(data: MsgMessageData): MsgMessage` - Create a new message
|
|
246
285
|
|
|
247
286
|
**Methods:**
|
|
248
|
-
- `format(data: Record<string, any>, options?: MessageFormatOptions): string` - Format the message
|
|
249
|
-
- `formatToParts(data: Record<string, any>, options?: MessageFormatOptions): MessagePart[]` - Format to parts
|
|
287
|
+
- `format(data: Record<string, any>, options?: MessageFormatOptions): string` - Format the message according to its resolved `format`: `MF2` uses MessageFormat 2, `MF1` compiles via `@messageformat/icu-messageformat-1`, and `NONE` returns the raw value
|
|
288
|
+
- `formatToParts(data: Record<string, any>, options?: MessageFormatOptions): MessagePart[]` - Format to parts (for `NONE`, a single `{ type: 'text', value }` part)
|
|
250
289
|
- `addNote(note: MsgNote): void` - Add a note
|
|
251
290
|
- `getData(stripNotes?: boolean): MsgMessageData` - Get message data
|
|
252
291
|
- `toJSON(stripNotes?: boolean): string` - Serialize to JSON
|
|
@@ -254,9 +293,14 @@ const data = resource.getData();
|
|
|
254
293
|
**Properties:**
|
|
255
294
|
- `key: string` - Message key
|
|
256
295
|
- `value: string` - Message value
|
|
257
|
-
- `attributes: MsgAttributes` - Message attributes (lang, dir, dnt)
|
|
296
|
+
- `attributes: MsgAttributes` - Message attributes (lang, dir, dnt, format)
|
|
258
297
|
- `notes: MsgNote[]` - Message notes
|
|
259
298
|
|
|
299
|
+
### Types
|
|
300
|
+
|
|
301
|
+
- `MsgFormat` - `'MF1' | 'MF2' | 'NONE'`; the formatting syntax for a message.
|
|
302
|
+
- `MsgAttributes` - `{ lang?: string; dir?: string; dnt?: boolean; format?: MsgFormat }`.
|
|
303
|
+
|
|
260
304
|
## Development
|
|
261
305
|
|
|
262
306
|
```bash
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
|
-
DEFAULT_ATTRIBUTES
|
|
3
|
-
|
|
2
|
+
DEFAULT_ATTRIBUTES,
|
|
3
|
+
MSG_DEFAULT_FORMAT
|
|
4
|
+
} from "./chunk-XQVALCKX.mjs";
|
|
4
5
|
|
|
5
6
|
// src/classes/MsgMessage/MsgMessage.ts
|
|
6
7
|
import { MessageFormat } from "messageformat";
|
|
8
|
+
import { mf1ToMessage } from "@messageformat/icu-messageformat-1";
|
|
7
9
|
var MsgMessage = class _MsgMessage {
|
|
8
10
|
_key;
|
|
9
11
|
_value;
|
|
@@ -38,17 +40,29 @@ var MsgMessage = class _MsgMessage {
|
|
|
38
40
|
addNote(note) {
|
|
39
41
|
this.notes.push(note);
|
|
40
42
|
}
|
|
41
|
-
|
|
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) {
|
|
42
50
|
if (!this._mf) {
|
|
43
|
-
this._mf = new MessageFormat(this.attributes.lang, this.value, options);
|
|
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;
|
|
44
58
|
}
|
|
45
|
-
return this.
|
|
59
|
+
return this.getFormatter(options).format(data);
|
|
46
60
|
}
|
|
47
61
|
formatToParts(data, options) {
|
|
48
|
-
if (
|
|
49
|
-
|
|
62
|
+
if (this.resolveFormat() === "NONE") {
|
|
63
|
+
return [{ type: "text", value: this.value }];
|
|
50
64
|
}
|
|
51
|
-
return this.
|
|
65
|
+
return this.getFormatter(options).formatToParts(data);
|
|
52
66
|
}
|
|
53
67
|
getData(stripNotes = false) {
|
|
54
68
|
return {
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MSG_DEFAULT_FORMAT
|
|
3
|
+
} from "./chunk-XQVALCKX.mjs";
|
|
4
|
+
|
|
1
5
|
// src/classes/MsgProject/MsgProject.ts
|
|
2
6
|
var defaultProjectSettings = {
|
|
3
7
|
name: "messages",
|
|
4
|
-
version: 1
|
|
8
|
+
version: 1,
|
|
9
|
+
format: MSG_DEFAULT_FORMAT
|
|
5
10
|
};
|
|
6
11
|
var defaultLocalesSettings = {
|
|
7
12
|
sourceLocale: "en",
|
|
@@ -32,6 +37,13 @@ var MsgProject = class _MsgProject {
|
|
|
32
37
|
get loader() {
|
|
33
38
|
return this._loader;
|
|
34
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
|
+
}
|
|
35
47
|
getTargetLocale(locale) {
|
|
36
48
|
return this._locales.targetLocales[locale];
|
|
37
49
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MsgMessage
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-HCR2UNSE.mjs";
|
|
4
4
|
import {
|
|
5
|
-
DEFAULT_ATTRIBUTES
|
|
6
|
-
|
|
5
|
+
DEFAULT_ATTRIBUTES,
|
|
6
|
+
MSG_DEFAULT_FORMAT
|
|
7
|
+
} from "./chunk-XQVALCKX.mjs";
|
|
7
8
|
|
|
8
9
|
// src/classes/MsgResource/MsgResource.ts
|
|
9
10
|
import { parseMessage, stringifyMessage, visit } from "messageformat";
|
|
@@ -29,7 +30,7 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
29
30
|
constructor(title, attributes, project, notes) {
|
|
30
31
|
super();
|
|
31
32
|
this._title = title;
|
|
32
|
-
this._attributes = { ...DEFAULT_ATTRIBUTES, ...attributes };
|
|
33
|
+
this._attributes = { ...DEFAULT_ATTRIBUTES, format: project.format, ...attributes };
|
|
33
34
|
this._project = project;
|
|
34
35
|
if (notes) {
|
|
35
36
|
notes.forEach((note) => this.addNote(note));
|
|
@@ -38,7 +39,10 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
38
39
|
hasMatchingAttributes(message) {
|
|
39
40
|
const res = this.attributes;
|
|
40
41
|
const msg = message.attributes;
|
|
41
|
-
return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt;
|
|
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;
|
|
42
46
|
}
|
|
43
47
|
pseudoLocalizeMF2(source, options) {
|
|
44
48
|
const msg = parseMessage(source);
|
|
@@ -96,7 +100,8 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
96
100
|
}
|
|
97
101
|
const translated = _MsgResource.create({
|
|
98
102
|
title,
|
|
99
|
-
|
|
103
|
+
// preserve the source resource's format unless the translation overrides it
|
|
104
|
+
attributes: this.preserveFormat(attributes, this.attributes.format),
|
|
100
105
|
notes: this.notes
|
|
101
106
|
// transfer the notes
|
|
102
107
|
}, this._project);
|
|
@@ -108,7 +113,8 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
108
113
|
const msg = MsgMessage.create({
|
|
109
114
|
key,
|
|
110
115
|
value,
|
|
111
|
-
|
|
116
|
+
// preserve the source message's format unless the translation overrides it
|
|
117
|
+
attributes: this.preserveFormat(attributes2, this.get(key)?.attributes.format)
|
|
112
118
|
});
|
|
113
119
|
const notes = this.get(key)?.notes || [];
|
|
114
120
|
notes.forEach((note) => {
|
|
@@ -118,6 +124,12 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
118
124
|
});
|
|
119
125
|
return translated;
|
|
120
126
|
}
|
|
127
|
+
preserveFormat(attributes, format) {
|
|
128
|
+
if (format === void 0 || attributes?.format !== void 0) {
|
|
129
|
+
return attributes;
|
|
130
|
+
}
|
|
131
|
+
return { ...attributes, format };
|
|
132
|
+
}
|
|
121
133
|
async getTranslation(lang) {
|
|
122
134
|
const project = this._project;
|
|
123
135
|
const pseudoLocale = project.locales.pseudoLocale;
|
|
@@ -154,6 +166,8 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
154
166
|
return translated;
|
|
155
167
|
}
|
|
156
168
|
getData(stripNotes = false) {
|
|
169
|
+
const resourceFormat = this.resolveFormat(this.attributes);
|
|
170
|
+
const projectFormat = this._project.format;
|
|
157
171
|
const messages = [];
|
|
158
172
|
this.forEach((msg) => {
|
|
159
173
|
if (this.hasMatchingAttributes(msg)) {
|
|
@@ -164,12 +178,21 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
164
178
|
};
|
|
165
179
|
messages.push(data);
|
|
166
180
|
} else {
|
|
167
|
-
|
|
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);
|
|
168
187
|
}
|
|
169
188
|
});
|
|
189
|
+
const attributes = { ...this.attributes };
|
|
190
|
+
if (this.resolveFormat(attributes) === projectFormat) {
|
|
191
|
+
delete attributes.format;
|
|
192
|
+
}
|
|
170
193
|
return {
|
|
171
194
|
title: this.title,
|
|
172
|
-
attributes
|
|
195
|
+
attributes,
|
|
173
196
|
notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0,
|
|
174
197
|
messages
|
|
175
198
|
};
|
|
@@ -20,9 +20,11 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/classes/MsgInterface/MsgInterface.ts
|
|
21
21
|
var MsgInterface_exports = {};
|
|
22
22
|
__export(MsgInterface_exports, {
|
|
23
|
-
DEFAULT_ATTRIBUTES: () => DEFAULT_ATTRIBUTES
|
|
23
|
+
DEFAULT_ATTRIBUTES: () => DEFAULT_ATTRIBUTES,
|
|
24
|
+
MSG_DEFAULT_FORMAT: () => MSG_DEFAULT_FORMAT
|
|
24
25
|
});
|
|
25
26
|
module.exports = __toCommonJS(MsgInterface_exports);
|
|
27
|
+
var MSG_DEFAULT_FORMAT = "MF2";
|
|
26
28
|
var DEFAULT_ATTRIBUTES = {
|
|
27
29
|
lang: "und",
|
|
28
30
|
dir: "auto",
|
|
@@ -30,5 +32,6 @@ var DEFAULT_ATTRIBUTES = {
|
|
|
30
32
|
};
|
|
31
33
|
// Annotate the CommonJS export names for ESM import in node:
|
|
32
34
|
0 && (module.exports = {
|
|
33
|
-
DEFAULT_ATTRIBUTES
|
|
35
|
+
DEFAULT_ATTRIBUTES,
|
|
36
|
+
MSG_DEFAULT_FORMAT
|
|
34
37
|
});
|
|
@@ -3,10 +3,24 @@ type MsgNote = {
|
|
|
3
3
|
type: NoteTypes;
|
|
4
4
|
content: string;
|
|
5
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;
|
|
6
19
|
type MsgAttributes = {
|
|
7
20
|
lang?: string;
|
|
8
21
|
dir?: string;
|
|
9
22
|
dnt?: boolean;
|
|
23
|
+
format?: MsgFormat;
|
|
10
24
|
};
|
|
11
25
|
declare const DEFAULT_ATTRIBUTES: MsgAttributes;
|
|
12
26
|
interface MsgInterface {
|
|
@@ -14,4 +28,4 @@ interface MsgInterface {
|
|
|
14
28
|
notes: MsgNote[];
|
|
15
29
|
}
|
|
16
30
|
|
|
17
|
-
export { DEFAULT_ATTRIBUTES, type MsgAttributes, type MsgInterface, type MsgNote };
|
|
31
|
+
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, type MsgAttributes, type MsgFormat, type MsgInterface, type MsgNote };
|
|
@@ -3,10 +3,24 @@ type MsgNote = {
|
|
|
3
3
|
type: NoteTypes;
|
|
4
4
|
content: string;
|
|
5
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;
|
|
6
19
|
type MsgAttributes = {
|
|
7
20
|
lang?: string;
|
|
8
21
|
dir?: string;
|
|
9
22
|
dnt?: boolean;
|
|
23
|
+
format?: MsgFormat;
|
|
10
24
|
};
|
|
11
25
|
declare const DEFAULT_ATTRIBUTES: MsgAttributes;
|
|
12
26
|
interface MsgInterface {
|
|
@@ -14,4 +28,4 @@ interface MsgInterface {
|
|
|
14
28
|
notes: MsgNote[];
|
|
15
29
|
}
|
|
16
30
|
|
|
17
|
-
export { DEFAULT_ATTRIBUTES, type MsgAttributes, type MsgInterface, type MsgNote };
|
|
31
|
+
export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, type MsgAttributes, type MsgFormat, type MsgInterface, type MsgNote };
|
|
@@ -24,8 +24,10 @@ __export(MsgMessage_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(MsgMessage_exports);
|
|
26
26
|
var import_messageformat = require("messageformat");
|
|
27
|
+
var import_icu_messageformat_1 = require("@messageformat/icu-messageformat-1");
|
|
27
28
|
|
|
28
29
|
// src/classes/MsgInterface/MsgInterface.ts
|
|
30
|
+
var MSG_DEFAULT_FORMAT = "MF2";
|
|
29
31
|
var DEFAULT_ATTRIBUTES = {
|
|
30
32
|
lang: "und",
|
|
31
33
|
dir: "auto",
|
|
@@ -67,17 +69,29 @@ var MsgMessage = class _MsgMessage {
|
|
|
67
69
|
addNote(note) {
|
|
68
70
|
this.notes.push(note);
|
|
69
71
|
}
|
|
70
|
-
|
|
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) {
|
|
71
79
|
if (!this._mf) {
|
|
72
|
-
this._mf = new import_messageformat.MessageFormat(this.attributes.lang, this.value, options);
|
|
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;
|
|
73
87
|
}
|
|
74
|
-
return this.
|
|
88
|
+
return this.getFormatter(options).format(data);
|
|
75
89
|
}
|
|
76
90
|
formatToParts(data, options) {
|
|
77
|
-
if (
|
|
78
|
-
|
|
91
|
+
if (this.resolveFormat() === "NONE") {
|
|
92
|
+
return [{ type: "text", value: this.value }];
|
|
79
93
|
}
|
|
80
|
-
return this.
|
|
94
|
+
return this.getFormatter(options).formatToParts(data);
|
|
81
95
|
}
|
|
82
96
|
getData(stripNotes = false) {
|
|
83
97
|
return {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as messageformat from 'messageformat';
|
|
2
2
|
import { MessageFormatOptions } from 'messageformat';
|
|
3
|
-
import { MsgAttributes, MsgNote
|
|
3
|
+
import { MsgInterface, MsgAttributes, MsgNote } from '../MsgInterface/MsgInterface.cjs';
|
|
4
4
|
|
|
5
5
|
type MsgMessageData = {
|
|
6
6
|
key: string;
|
|
@@ -21,6 +21,11 @@ declare class MsgMessage implements MsgInterface {
|
|
|
21
21
|
get attributes(): MsgAttributes;
|
|
22
22
|
get notes(): MsgNote[];
|
|
23
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;
|
|
24
29
|
format(data: Record<string, any>, options?: MessageFormatOptions): string;
|
|
25
30
|
formatToParts(data: Record<string, any>, options?: MessageFormatOptions): messageformat.MessagePart<never>[];
|
|
26
31
|
getData(stripNotes?: boolean): {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as messageformat from 'messageformat';
|
|
2
2
|
import { MessageFormatOptions } from 'messageformat';
|
|
3
|
-
import { MsgAttributes, MsgNote
|
|
3
|
+
import { MsgInterface, MsgAttributes, MsgNote } from '../MsgInterface/MsgInterface.js';
|
|
4
4
|
|
|
5
5
|
type MsgMessageData = {
|
|
6
6
|
key: string;
|
|
@@ -21,6 +21,11 @@ declare class MsgMessage implements MsgInterface {
|
|
|
21
21
|
get attributes(): MsgAttributes;
|
|
22
22
|
get notes(): MsgNote[];
|
|
23
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;
|
|
24
29
|
format(data: Record<string, any>, options?: MessageFormatOptions): string;
|
|
25
30
|
formatToParts(data: Record<string, any>, options?: MessageFormatOptions): messageformat.MessagePart<never>[];
|
|
26
31
|
getData(stripNotes?: boolean): {
|
|
@@ -23,9 +23,15 @@ __export(MsgProject_exports, {
|
|
|
23
23
|
MsgProject: () => MsgProject
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(MsgProject_exports);
|
|
26
|
+
|
|
27
|
+
// src/classes/MsgInterface/MsgInterface.ts
|
|
28
|
+
var MSG_DEFAULT_FORMAT = "MF2";
|
|
29
|
+
|
|
30
|
+
// src/classes/MsgProject/MsgProject.ts
|
|
26
31
|
var defaultProjectSettings = {
|
|
27
32
|
name: "messages",
|
|
28
|
-
version: 1
|
|
33
|
+
version: 1,
|
|
34
|
+
format: MSG_DEFAULT_FORMAT
|
|
29
35
|
};
|
|
30
36
|
var defaultLocalesSettings = {
|
|
31
37
|
sourceLocale: "en",
|
|
@@ -56,6 +62,13 @@ var MsgProject = class _MsgProject {
|
|
|
56
62
|
get loader() {
|
|
57
63
|
return this._loader;
|
|
58
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* The project-wide default message format. Resources (and, through them,
|
|
67
|
+
* messages) inherit this value unless they specify their own.
|
|
68
|
+
*/
|
|
69
|
+
get format() {
|
|
70
|
+
return this._project.format ?? MSG_DEFAULT_FORMAT;
|
|
71
|
+
}
|
|
59
72
|
getTargetLocale(locale) {
|
|
60
73
|
return this._locales.targetLocales[locale];
|
|
61
74
|
}
|
|
@@ -1,65 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
1
|
+
export { M as MsgProject, a as MsgProjectData, b as MsgTranslationLoader } from '../MsgResource/MsgResource.cjs';
|
|
2
|
+
import '../MsgInterface/MsgInterface.cjs';
|
|
3
|
+
import '../MsgMessage/MsgMessage.cjs';
|
|
3
4
|
import 'messageformat';
|
|
4
|
-
|
|
5
|
-
type MsgProjectSettings = {
|
|
6
|
-
name: string;
|
|
7
|
-
version?: number;
|
|
8
|
-
};
|
|
9
|
-
type MsgTargetLocalesSettings = {
|
|
10
|
-
[key: string]: string[];
|
|
11
|
-
};
|
|
12
|
-
type MsgLocalesSettings = {
|
|
13
|
-
sourceLocale: string;
|
|
14
|
-
pseudoLocale: string;
|
|
15
|
-
targetLocales: MsgTargetLocalesSettings;
|
|
16
|
-
};
|
|
17
|
-
type MsgTranslationLoader = (project: string, title: string, lang: string) => Promise<MsgResourceData>;
|
|
18
|
-
type MsgProjectData = {
|
|
19
|
-
project: MsgProjectSettings;
|
|
20
|
-
locales: MsgLocalesSettings;
|
|
21
|
-
loader: MsgTranslationLoader;
|
|
22
|
-
};
|
|
23
|
-
declare class MsgProject {
|
|
24
|
-
_project: MsgProjectSettings;
|
|
25
|
-
_locales: MsgLocalesSettings;
|
|
26
|
-
_loader: MsgTranslationLoader;
|
|
27
|
-
static create(data: MsgProjectData): MsgProject;
|
|
28
|
-
private constructor();
|
|
29
|
-
get project(): MsgProjectSettings;
|
|
30
|
-
get locales(): MsgLocalesSettings;
|
|
31
|
-
get loader(): MsgTranslationLoader;
|
|
32
|
-
getTargetLocale(locale: string): string[] | undefined;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
type MsgResourceData = {
|
|
36
|
-
title: string;
|
|
37
|
-
attributes: MsgAttributes;
|
|
38
|
-
notes?: MsgNote[];
|
|
39
|
-
messages?: MsgMessageData[];
|
|
40
|
-
};
|
|
41
|
-
declare class MsgResource extends Map<string, MsgMessage> implements MsgInterface {
|
|
42
|
-
private _attributes;
|
|
43
|
-
private _notes;
|
|
44
|
-
private _title;
|
|
45
|
-
private _project;
|
|
46
|
-
static create(data: MsgResourceData, project: MsgProject): MsgResource;
|
|
47
|
-
private constructor();
|
|
48
|
-
private hasMatchingAttributes;
|
|
49
|
-
private pseudoLocalizeMF2;
|
|
50
|
-
get attributes(): MsgAttributes;
|
|
51
|
-
set attributes(attributes: MsgAttributes);
|
|
52
|
-
get notes(): MsgNote[];
|
|
53
|
-
set notes(notes: MsgNote[]);
|
|
54
|
-
addNote(note: MsgNote): void;
|
|
55
|
-
get title(): string;
|
|
56
|
-
set title(title: string);
|
|
57
|
-
getProject(): MsgProject;
|
|
58
|
-
add(key: string, value: string, attributes?: MsgAttributes, notes?: MsgNote[]): this;
|
|
59
|
-
translate(data: MsgResourceData): MsgResource;
|
|
60
|
-
getTranslation(lang: string): Promise<MsgResource>;
|
|
61
|
-
getData(stripNotes?: boolean): MsgResourceData;
|
|
62
|
-
toJSON(stripNotes?: boolean): string;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export { type MsgResourceData as M, MsgProject, type MsgProjectData, type MsgTranslationLoader, MsgResource as a };
|