@worldware/msg 0.9.0 → 0.10.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 +80 -55
- package/dist/{chunk-IMOOTZIK.mjs → chunk-HUWFAJAN.mjs} +15 -4
- package/dist/chunk-KQTWUIAM.mjs +0 -0
- package/dist/{chunk-F3VAAZJZ.mjs → chunk-OKEV7FY7.mjs} +13 -2
- package/dist/classes/MsgMessage/MsgMessage.cjs +13 -2
- package/dist/classes/MsgMessage/MsgMessage.d.cts +10 -2
- package/dist/classes/MsgMessage/MsgMessage.d.ts +10 -2
- package/dist/classes/MsgMessage/MsgMessage.mjs +1 -1
- package/dist/classes/MsgProject/MsgProject.d.cts +1 -1
- package/dist/classes/MsgProject/MsgProject.d.ts +1 -1
- package/dist/classes/MsgResource/MsgResource.cjs +27 -5
- package/dist/classes/MsgResource/MsgResource.d.cts +3 -200
- package/dist/classes/MsgResource/MsgResource.d.ts +3 -200
- package/dist/classes/MsgResource/MsgResource.mjs +2 -2
- package/dist/classes/index.cjs +27 -5
- package/dist/classes/index.d.cts +209 -3
- package/dist/classes/index.d.ts +209 -3
- package/dist/classes/index.mjs +2 -2
- package/dist/index.cjs +50 -15
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +10 -3
- package/dist/lib/index.mjs +1 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -52,12 +52,12 @@ An individual message with:
|
|
|
52
52
|
|
|
53
53
|
### Basic Setup
|
|
54
54
|
|
|
55
|
-
The following example matches the ES module output of the **msg-cli** `create project
|
|
55
|
+
The following example matches the ES module output of the **msg-cli** command `msg create project Main en zh fr --format MF1`—a typical project file that loads translations from JSON under a translations directory (`TRANSLATION_IMPORT_PATH` may differ based on your `directories.i18n` / `directories.l10n` layout):
|
|
56
56
|
|
|
57
|
-
```
|
|
57
|
+
```javascript
|
|
58
58
|
import { MsgProject } from '@worldware/msg';
|
|
59
59
|
|
|
60
|
-
const TRANSLATION_IMPORT_PATH =
|
|
60
|
+
const TRANSLATION_IMPORT_PATH = "../l10n/translations";
|
|
61
61
|
const loader = async (project, title, language) => {
|
|
62
62
|
const path = `${TRANSLATION_IMPORT_PATH}/${project}/${language}/${title}.json`;
|
|
63
63
|
try {
|
|
@@ -75,59 +75,78 @@ const loader = async (project, title, language) => {
|
|
|
75
75
|
};
|
|
76
76
|
|
|
77
77
|
export default MsgProject.create({
|
|
78
|
-
project: { name:
|
|
78
|
+
project: { name: "Main", version: 1, format: "MF1" },
|
|
79
79
|
locales: {
|
|
80
|
-
sourceLocale:
|
|
81
|
-
pseudoLocale:
|
|
82
|
-
targetLocales: {
|
|
83
|
-
'en': ['en'],
|
|
84
|
-
'es': ['es'],
|
|
85
|
-
'fr': ['fr'],
|
|
86
|
-
'fr-CA': ['fr', 'fr-CA']
|
|
87
|
-
}
|
|
80
|
+
sourceLocale: "en",
|
|
81
|
+
pseudoLocale: "en-XA",
|
|
82
|
+
targetLocales: {"en":["en"],"zh":["zh"],"fr":["fr"]}
|
|
88
83
|
},
|
|
89
84
|
loader
|
|
90
85
|
});
|
|
91
86
|
```
|
|
92
87
|
|
|
93
|
-
When using this in your app, import the default export as your project
|
|
88
|
+
When using this in your app, import the default export as your project (msg-cli scaffolds resources that import it via `#i18n/projects/Main.js`).
|
|
94
89
|
|
|
95
90
|
### Creating a Resource
|
|
96
91
|
|
|
97
|
-
|
|
98
|
-
// Create a resource with messages
|
|
99
|
-
const resource = MsgResource.create({
|
|
100
|
-
title: 'CommonMessages',
|
|
101
|
-
attributes: {
|
|
102
|
-
lang: 'en',
|
|
103
|
-
dir: 'ltr'
|
|
104
|
-
},
|
|
105
|
-
messages: [
|
|
106
|
-
{
|
|
107
|
-
key: 'greeting',
|
|
108
|
-
value: 'Hello, {$name}!'
|
|
109
|
-
},
|
|
110
|
-
{
|
|
111
|
-
key: 'welcome',
|
|
112
|
-
value: 'Welcome to our application'
|
|
113
|
-
}
|
|
114
|
-
]
|
|
115
|
-
}, project);
|
|
92
|
+
The following example matches the ES module output of **msg-cli** `msg create resource Main Messages`:
|
|
116
93
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
94
|
+
```javascript
|
|
95
|
+
/** ESM module **/
|
|
96
|
+
|
|
97
|
+
import { MsgResource, getLang } from '@worldware/msg';
|
|
98
|
+
import project from '#i18n/projects/Main.js';
|
|
99
|
+
|
|
100
|
+
/** Create a MsgResource object */
|
|
101
|
+
|
|
102
|
+
export const resource = MsgResource.create({
|
|
103
|
+
title: 'Messages',
|
|
104
|
+
attributes: {
|
|
105
|
+
lang: 'en',
|
|
106
|
+
dir: 'ltr'
|
|
107
|
+
},
|
|
108
|
+
notes: [
|
|
109
|
+
{type: 'DESCRIPTION', content: 'This is the Messages resource.'}
|
|
110
|
+
]
|
|
111
|
+
}, project);
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Add messages to the resource using add(key, value, attributes, notes)
|
|
115
|
+
* The add method is chainable.
|
|
116
|
+
*/
|
|
117
|
+
|
|
118
|
+
resource
|
|
119
|
+
.add('sampleKey', 'Sample value.', {}, [
|
|
120
|
+
{ type: 'DESCRIPTION', content: 'This is first message.' }
|
|
121
|
+
])
|
|
122
|
+
.add('sampleKey2', 'Hi, {name}', { dnt: true }, [
|
|
123
|
+
{ type: 'DESCRIPTION', content: 'This is the second message.' },
|
|
124
|
+
{ type: 'PARAMETERS', content: 'The {name} parameter holds the user name.' }
|
|
125
|
+
]);
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* An async function to get a translated version of the resource
|
|
129
|
+
* If the runtime language has not been set using `setLang()`,
|
|
130
|
+
* it will return the original resource
|
|
131
|
+
*/
|
|
132
|
+
export async function getMessages() {
|
|
133
|
+
return await resource.getTranslation(getLang());
|
|
134
|
+
}
|
|
122
135
|
```
|
|
123
136
|
|
|
124
137
|
### Formatting Messages
|
|
125
138
|
|
|
126
|
-
```
|
|
127
|
-
//
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
139
|
+
```javascript
|
|
140
|
+
// Messages inherit MF1 from the Main project scaffold above
|
|
141
|
+
resource.get('sampleKey')?.format();
|
|
142
|
+
// Result: "Sample value."
|
|
143
|
+
|
|
144
|
+
resource.get('sampleKey2')?.format({ name: 'Alice' });
|
|
145
|
+
// Result: "Hi, Alice"
|
|
146
|
+
|
|
147
|
+
// Prefer getMessages() when you want the runtime locale (via setLang/getLang)
|
|
148
|
+
const messages = await getMessages();
|
|
149
|
+
messages.get('sampleKey2')?.format({ name: 'Alice' });
|
|
131
150
|
```
|
|
132
151
|
|
|
133
152
|
### Message Formats (MF1, MF2, NONE)
|
|
@@ -140,6 +159,8 @@ Every message is formatted according to its resolved `format` attribute:
|
|
|
140
159
|
|
|
141
160
|
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
161
|
|
|
162
|
+
The following is a **separate illustration** of format inheritance and per-message overrides (not the CLI `Main` / `Messages` scaffold above):
|
|
163
|
+
|
|
143
164
|
```typescript
|
|
144
165
|
import { MsgProject, MsgResource } from '@worldware/msg';
|
|
145
166
|
|
|
@@ -168,12 +189,15 @@ When serializing, an inherited `format` is omitted to keep output compact: a res
|
|
|
168
189
|
|
|
169
190
|
### Loading Translations
|
|
170
191
|
|
|
171
|
-
```
|
|
172
|
-
// Load a translation for a
|
|
173
|
-
const
|
|
192
|
+
```javascript
|
|
193
|
+
// Load a translation for a configured target locale (zh or fr in the Main project)
|
|
194
|
+
const zhResource = await resource.getTranslation('zh');
|
|
174
195
|
|
|
175
|
-
// The translated resource will have
|
|
196
|
+
// The translated resource will have Chinese messages where available,
|
|
176
197
|
// falling back to the source messages for missing translations
|
|
198
|
+
|
|
199
|
+
// Or use the scaffolded helper (honors setLang() / getLang())
|
|
200
|
+
const messages = await getMessages();
|
|
177
201
|
```
|
|
178
202
|
|
|
179
203
|
### Language fallbacks and translation layering
|
|
@@ -193,21 +217,21 @@ So for `getTranslation('zh-HK')` with chain `['zh', 'zh-Hant', 'zh-HK']`, you ge
|
|
|
193
217
|
|
|
194
218
|
When `getTranslation` is called with the project's `pseudoLocale` (e.g. `en-XA`), it returns a new resource with pseudolocalized message values—useful for testing UI layout and finding hardcoded strings without loading translation files:
|
|
195
219
|
|
|
196
|
-
```
|
|
220
|
+
```javascript
|
|
197
221
|
// Request pseudolocalized messages (project locales.pseudoLocale is 'en-XA')
|
|
198
222
|
const pseudoResource = await resource.getTranslation('en-XA');
|
|
199
223
|
|
|
200
|
-
// Message values are transformed: "
|
|
201
|
-
// Variables and
|
|
202
|
-
const greeting = pseudoResource.get('
|
|
203
|
-
// Result: "
|
|
224
|
+
// Message values are transformed: "Hi, {name}" → "Ħī, {name}"
|
|
225
|
+
// Variables and message-format syntax are preserved; only literal text is pseudolocalized
|
|
226
|
+
const greeting = pseudoResource.get('sampleKey2')?.format({ name: 'Alice' });
|
|
227
|
+
// Result: "Ħī, Alice"
|
|
204
228
|
```
|
|
205
229
|
|
|
206
230
|
### Working with Attributes and Notes
|
|
207
231
|
|
|
208
|
-
```
|
|
209
|
-
// Add notes to messages
|
|
210
|
-
resource.add('complex-message', 'You have {
|
|
232
|
+
```javascript
|
|
233
|
+
// Add notes to messages (MF1 syntax, matching the Main project's format)
|
|
234
|
+
resource.add('complex-message', 'You have {count} items', {
|
|
211
235
|
lang: 'en',
|
|
212
236
|
dir: 'ltr',
|
|
213
237
|
dnt: false // do-not-translate flag
|
|
@@ -267,6 +291,7 @@ const data = resource.getData();
|
|
|
267
291
|
|
|
268
292
|
**Methods:**
|
|
269
293
|
- `add(key: string, value: string, attributes?: MsgAttributes, notes?: MsgNote[]): MsgResource` - Add a message
|
|
294
|
+
- `addNote(type: NoteTypes, content: string): MsgResource` - Add a note (returns `this` for chaining)
|
|
270
295
|
- `translate(data: MsgResourceData): MsgResource` - Create a translated version
|
|
271
296
|
- `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.
|
|
272
297
|
- `getProject(): MsgProject` - Returns the project instance associated with the resource
|
|
@@ -286,7 +311,7 @@ const data = resource.getData();
|
|
|
286
311
|
**Methods:**
|
|
287
312
|
- `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
313
|
- `formatToParts(data: Record<string, any>, options?: MessageFormatOptions): MessagePart[]` - Format to parts (for `NONE`, a single `{ type: 'text', value }` part)
|
|
289
|
-
- `addNote(
|
|
314
|
+
- `addNote(type: NoteTypes, content: string): MsgMessage` - Add a note (returns `this` for chaining)
|
|
290
315
|
- `getData(stripNotes?: boolean): MsgMessageData` - Get message data
|
|
291
316
|
- `toJSON(stripNotes?: boolean): string` - Serialize to JSON
|
|
292
317
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MsgMessage
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-OKEV7FY7.mjs";
|
|
4
4
|
import {
|
|
5
5
|
DEFAULT_ATTRIBUTES,
|
|
6
6
|
MSG_DEFAULT_FORMAT
|
|
@@ -42,7 +42,7 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
42
42
|
this._attributes = { ...DEFAULT_ATTRIBUTES, format: project.format, ...attributes };
|
|
43
43
|
this._project = project;
|
|
44
44
|
if (notes) {
|
|
45
|
-
notes.forEach((note) => this.
|
|
45
|
+
notes.forEach((note) => this.setNote(note));
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
@@ -94,9 +94,20 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
94
94
|
}
|
|
95
95
|
/**
|
|
96
96
|
* Appends a note to this resource.
|
|
97
|
+
*
|
|
98
|
+
* @returns This resource, for chaining.
|
|
99
|
+
*/
|
|
100
|
+
addNote(type, content) {
|
|
101
|
+
return this.setNote({ type, content });
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Appends a full note object to this resource.
|
|
105
|
+
*
|
|
106
|
+
* @returns This resource, for chaining.
|
|
97
107
|
*/
|
|
98
|
-
|
|
108
|
+
setNote(note) {
|
|
99
109
|
this.notes.push(note);
|
|
110
|
+
return this;
|
|
100
111
|
}
|
|
101
112
|
/** Title that identifies this resource within a project. */
|
|
102
113
|
get title() {
|
|
@@ -161,7 +172,7 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
161
172
|
});
|
|
162
173
|
const notes = this.get(key)?.notes || [];
|
|
163
174
|
notes.forEach((note) => {
|
|
164
|
-
msg.addNote(note);
|
|
175
|
+
msg.addNote(note.type, note.content);
|
|
165
176
|
});
|
|
166
177
|
translated.set(key, msg);
|
|
167
178
|
});
|
|
File without changes
|
|
@@ -20,7 +20,7 @@ var MsgMessage = class _MsgMessage {
|
|
|
20
20
|
this._value = value;
|
|
21
21
|
this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
|
|
22
22
|
if (notes) {
|
|
23
|
-
notes.forEach((note) => this.
|
|
23
|
+
notes.forEach((note) => this.setNote(note));
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
@@ -49,9 +49,20 @@ var MsgMessage = class _MsgMessage {
|
|
|
49
49
|
}
|
|
50
50
|
/**
|
|
51
51
|
* Appends a note to this message.
|
|
52
|
+
*
|
|
53
|
+
* @returns This message, for chaining.
|
|
54
|
+
*/
|
|
55
|
+
addNote(type, content) {
|
|
56
|
+
return this.setNote({ type, content });
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Appends a full note object to this message.
|
|
60
|
+
*
|
|
61
|
+
* @returns This message, for chaining.
|
|
52
62
|
*/
|
|
53
|
-
|
|
63
|
+
setNote(note) {
|
|
54
64
|
this.notes.push(note);
|
|
65
|
+
return this;
|
|
55
66
|
}
|
|
56
67
|
/**
|
|
57
68
|
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
@@ -49,7 +49,7 @@ var MsgMessage = class _MsgMessage {
|
|
|
49
49
|
this._value = value;
|
|
50
50
|
this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
|
|
51
51
|
if (notes) {
|
|
52
|
-
notes.forEach((note) => this.
|
|
52
|
+
notes.forEach((note) => this.setNote(note));
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
@@ -78,9 +78,20 @@ var MsgMessage = class _MsgMessage {
|
|
|
78
78
|
}
|
|
79
79
|
/**
|
|
80
80
|
* Appends a note to this message.
|
|
81
|
+
*
|
|
82
|
+
* @returns This message, for chaining.
|
|
83
|
+
*/
|
|
84
|
+
addNote(type, content) {
|
|
85
|
+
return this.setNote({ type, content });
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Appends a full note object to this message.
|
|
89
|
+
*
|
|
90
|
+
* @returns This message, for chaining.
|
|
81
91
|
*/
|
|
82
|
-
|
|
92
|
+
setNote(note) {
|
|
83
93
|
this.notes.push(note);
|
|
94
|
+
return this;
|
|
84
95
|
}
|
|
85
96
|
/**
|
|
86
97
|
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as messageformat from 'messageformat';
|
|
2
2
|
import { MessageFormatOptions } from 'messageformat';
|
|
3
|
-
import { MsgInterface, MsgAttributes, MsgNote } from '../MsgInterface/MsgInterface.cjs';
|
|
3
|
+
import { MsgInterface, MsgAttributes, MsgNote, NoteTypes } from '../MsgInterface/MsgInterface.cjs';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Plain data used to create a {@link MsgMessage}.
|
|
@@ -44,8 +44,16 @@ declare class MsgMessage implements MsgInterface {
|
|
|
44
44
|
get notes(): MsgNote[];
|
|
45
45
|
/**
|
|
46
46
|
* Appends a note to this message.
|
|
47
|
+
*
|
|
48
|
+
* @returns This message, for chaining.
|
|
49
|
+
*/
|
|
50
|
+
addNote(type: NoteTypes, content: string): this;
|
|
51
|
+
/**
|
|
52
|
+
* Appends a full note object to this message.
|
|
53
|
+
*
|
|
54
|
+
* @returns This message, for chaining.
|
|
47
55
|
*/
|
|
48
|
-
|
|
56
|
+
private setNote;
|
|
49
57
|
/**
|
|
50
58
|
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
51
59
|
*/
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as messageformat from 'messageformat';
|
|
2
2
|
import { MessageFormatOptions } from 'messageformat';
|
|
3
|
-
import { MsgInterface, MsgAttributes, MsgNote } from '../MsgInterface/MsgInterface.js';
|
|
3
|
+
import { MsgInterface, MsgAttributes, MsgNote, NoteTypes } from '../MsgInterface/MsgInterface.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Plain data used to create a {@link MsgMessage}.
|
|
@@ -44,8 +44,16 @@ declare class MsgMessage implements MsgInterface {
|
|
|
44
44
|
get notes(): MsgNote[];
|
|
45
45
|
/**
|
|
46
46
|
* Appends a note to this message.
|
|
47
|
+
*
|
|
48
|
+
* @returns This message, for chaining.
|
|
49
|
+
*/
|
|
50
|
+
addNote(type: NoteTypes, content: string): this;
|
|
51
|
+
/**
|
|
52
|
+
* Appends a full note object to this message.
|
|
53
|
+
*
|
|
54
|
+
* @returns This message, for chaining.
|
|
47
55
|
*/
|
|
48
|
-
|
|
56
|
+
private setNote;
|
|
49
57
|
/**
|
|
50
58
|
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
51
59
|
*/
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { MsgLocalesSettings, MsgProject, MsgProjectData, MsgProjectSettings, MsgTargetLocalesSettings, MsgTranslationLoader } from '../index.cjs';
|
|
2
2
|
import '../MsgInterface/MsgInterface.cjs';
|
|
3
3
|
import '../MsgMessage/MsgMessage.cjs';
|
|
4
4
|
import 'messageformat';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { MsgLocalesSettings, MsgProject, MsgProjectData, MsgProjectSettings, MsgTargetLocalesSettings, MsgTranslationLoader } from '../index.js';
|
|
2
2
|
import '../MsgInterface/MsgInterface.js';
|
|
3
3
|
import '../MsgMessage/MsgMessage.js';
|
|
4
4
|
import 'messageformat';
|
|
@@ -53,7 +53,7 @@ var MsgMessage = class _MsgMessage {
|
|
|
53
53
|
this._value = value;
|
|
54
54
|
this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
|
|
55
55
|
if (notes) {
|
|
56
|
-
notes.forEach((note) => this.
|
|
56
|
+
notes.forEach((note) => this.setNote(note));
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
/**
|
|
@@ -82,9 +82,20 @@ var MsgMessage = class _MsgMessage {
|
|
|
82
82
|
}
|
|
83
83
|
/**
|
|
84
84
|
* Appends a note to this message.
|
|
85
|
+
*
|
|
86
|
+
* @returns This message, for chaining.
|
|
85
87
|
*/
|
|
86
|
-
addNote(
|
|
88
|
+
addNote(type, content) {
|
|
89
|
+
return this.setNote({ type, content });
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Appends a full note object to this message.
|
|
93
|
+
*
|
|
94
|
+
* @returns This message, for chaining.
|
|
95
|
+
*/
|
|
96
|
+
setNote(note) {
|
|
87
97
|
this.notes.push(note);
|
|
98
|
+
return this;
|
|
88
99
|
}
|
|
89
100
|
/**
|
|
90
101
|
* The message's resolved format, defaulting to MF2 when not set or inherited.
|
|
@@ -186,7 +197,7 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
186
197
|
this._attributes = { ...DEFAULT_ATTRIBUTES, format: project.format, ...attributes };
|
|
187
198
|
this._project = project;
|
|
188
199
|
if (notes) {
|
|
189
|
-
notes.forEach((note) => this.
|
|
200
|
+
notes.forEach((note) => this.setNote(note));
|
|
190
201
|
}
|
|
191
202
|
}
|
|
192
203
|
/**
|
|
@@ -238,9 +249,20 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
238
249
|
}
|
|
239
250
|
/**
|
|
240
251
|
* Appends a note to this resource.
|
|
252
|
+
*
|
|
253
|
+
* @returns This resource, for chaining.
|
|
241
254
|
*/
|
|
242
|
-
addNote(
|
|
255
|
+
addNote(type, content) {
|
|
256
|
+
return this.setNote({ type, content });
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Appends a full note object to this resource.
|
|
260
|
+
*
|
|
261
|
+
* @returns This resource, for chaining.
|
|
262
|
+
*/
|
|
263
|
+
setNote(note) {
|
|
243
264
|
this.notes.push(note);
|
|
265
|
+
return this;
|
|
244
266
|
}
|
|
245
267
|
/** Title that identifies this resource within a project. */
|
|
246
268
|
get title() {
|
|
@@ -305,7 +327,7 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
305
327
|
});
|
|
306
328
|
const notes = this.get(key)?.notes || [];
|
|
307
329
|
notes.forEach((note) => {
|
|
308
|
-
msg.addNote(note);
|
|
330
|
+
msg.addNote(note.type, note.content);
|
|
309
331
|
});
|
|
310
332
|
translated.set(key, msg);
|
|
311
333
|
});
|
|
@@ -1,201 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import '../MsgMessage/MsgMessage.cjs';
|
|
2
|
+
import '../MsgInterface/MsgInterface.cjs';
|
|
3
|
+
export { MsgResource, MsgResourceData } from '../index.cjs';
|
|
3
4
|
import 'messageformat';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Plain data used to create or describe a {@link MsgResource}.
|
|
7
|
-
*/
|
|
8
|
-
type MsgResourceData = {
|
|
9
|
-
/** Title that identifies this resource within a project. */
|
|
10
|
-
title: string;
|
|
11
|
-
/** Locale and formatting metadata for the resource. */
|
|
12
|
-
attributes: MsgAttributes;
|
|
13
|
-
/** Optional notes for translators and tooling. */
|
|
14
|
-
notes?: MsgNote[];
|
|
15
|
-
/** Optional list of messages in this resource. */
|
|
16
|
-
messages?: MsgMessageData[];
|
|
17
|
-
};
|
|
18
|
-
/**
|
|
19
|
-
* A named collection of messages belonging to a {@link MsgProject}.
|
|
20
|
-
*
|
|
21
|
-
* Resources inherit format defaults from their project, support translation
|
|
22
|
-
* loading (including pseudo-localization), and can serialize to plain data.
|
|
23
|
-
*/
|
|
24
|
-
declare class MsgResource extends Map<string, MsgMessage> implements MsgInterface {
|
|
25
|
-
private _attributes;
|
|
26
|
-
private _notes;
|
|
27
|
-
private _title;
|
|
28
|
-
private _project;
|
|
29
|
-
/**
|
|
30
|
-
* Builds a resource from {@link MsgResourceData} and an owning project.
|
|
31
|
-
*
|
|
32
|
-
* When `messages` is provided, each entry is added to the resource.
|
|
33
|
-
* When omitted, the resource starts empty.
|
|
34
|
-
*/
|
|
35
|
-
static create(data: MsgResourceData, project: MsgProject): MsgResource;
|
|
36
|
-
/**
|
|
37
|
-
* Creates a resource bound to a project. Prefer {@link MsgResource.create}.
|
|
38
|
-
*/
|
|
39
|
-
private constructor();
|
|
40
|
-
/**
|
|
41
|
-
* Returns true when the message's attributes match this resource's attributes.
|
|
42
|
-
*/
|
|
43
|
-
private hasMatchingAttributes;
|
|
44
|
-
/**
|
|
45
|
-
* Resolves a format from attributes, falling back to the library default.
|
|
46
|
-
*/
|
|
47
|
-
private resolveFormat;
|
|
48
|
-
/**
|
|
49
|
-
* Pseudo-localizes an MF2 message by localizing only literal text parts.
|
|
50
|
-
*/
|
|
51
|
-
private pseudoLocalizeMF2;
|
|
52
|
-
/** Locale and formatting metadata for this resource. */
|
|
53
|
-
get attributes(): MsgAttributes;
|
|
54
|
-
/** Replaces the resource's locale and formatting metadata. */
|
|
55
|
-
set attributes(attributes: MsgAttributes);
|
|
56
|
-
/** Notes attached to this resource for translators and tooling. */
|
|
57
|
-
get notes(): MsgNote[];
|
|
58
|
-
/** Replaces the notes attached to this resource. */
|
|
59
|
-
set notes(notes: MsgNote[]);
|
|
60
|
-
/**
|
|
61
|
-
* Appends a note to this resource.
|
|
62
|
-
*/
|
|
63
|
-
addNote(note: MsgNote): void;
|
|
64
|
-
/** Title that identifies this resource within a project. */
|
|
65
|
-
get title(): string;
|
|
66
|
-
/** Sets the title that identifies this resource within a project. */
|
|
67
|
-
set title(title: string);
|
|
68
|
-
/**
|
|
69
|
-
* Returns the project this resource belongs to.
|
|
70
|
-
*/
|
|
71
|
-
getProject(): MsgProject;
|
|
72
|
-
/**
|
|
73
|
-
* Adds a message to this resource, merging resource attributes with any overrides.
|
|
74
|
-
*
|
|
75
|
-
* @returns This resource, for chaining.
|
|
76
|
-
*/
|
|
77
|
-
add(key: string, value: string, attributes?: MsgAttributes, notes?: MsgNote[]): this;
|
|
78
|
-
/**
|
|
79
|
-
* Builds a translated copy of this resource from translation data.
|
|
80
|
-
*
|
|
81
|
-
* Messages present in the translation replace the source; missing keys keep
|
|
82
|
-
* the source message. Notes are carried over from the source.
|
|
83
|
-
*
|
|
84
|
-
* @throws {TypeError} When the translation title does not match this resource.
|
|
85
|
-
*/
|
|
86
|
-
translate(data: MsgResourceData): MsgResource;
|
|
87
|
-
/**
|
|
88
|
-
* Merge a `format` into an attribute set unless one is already present,
|
|
89
|
-
* so translations inherit the source's format without overriding an
|
|
90
|
-
* explicitly translated one. Returns the input unchanged when there is
|
|
91
|
-
* no format to preserve.
|
|
92
|
-
*/
|
|
93
|
-
private preserveFormat;
|
|
94
|
-
/**
|
|
95
|
-
* Loads and applies translations for a locale via the project loader.
|
|
96
|
-
*
|
|
97
|
-
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
98
|
-
* copy without calling the loader.
|
|
99
|
-
*
|
|
100
|
-
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
101
|
-
*/
|
|
102
|
-
getTranslation(lang?: string): Promise<MsgResource>;
|
|
103
|
-
/**
|
|
104
|
-
* Returns a plain data object for this resource and its messages.
|
|
105
|
-
*
|
|
106
|
-
* Attributes that match the resource or project defaults are omitted where
|
|
107
|
-
* possible to keep the output compact.
|
|
108
|
-
*
|
|
109
|
-
* @param stripNotes - When true, notes are omitted from the result.
|
|
110
|
-
*/
|
|
111
|
-
getData(stripNotes?: boolean): MsgResourceData;
|
|
112
|
-
/**
|
|
113
|
-
* Serializes this resource to a formatted JSON string.
|
|
114
|
-
*
|
|
115
|
-
* @param stripNotes - When true, notes are omitted from the serialized data.
|
|
116
|
-
*/
|
|
117
|
-
toJSON(stripNotes?: boolean): string;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Project-level settings such as name, version, and default message format.
|
|
122
|
-
*/
|
|
123
|
-
type MsgProjectSettings = {
|
|
124
|
-
/** Display name of the localization project. */
|
|
125
|
-
name: string;
|
|
126
|
-
/** Optional project version number. */
|
|
127
|
-
version?: number;
|
|
128
|
-
/** Default message format inherited by resources and messages. */
|
|
129
|
-
format?: MsgFormat;
|
|
130
|
-
};
|
|
131
|
-
/**
|
|
132
|
-
* Map of a requested locale to the chain of locales used to load translations.
|
|
133
|
-
*/
|
|
134
|
-
type MsgTargetLocalesSettings = {
|
|
135
|
-
[key: string]: string[];
|
|
136
|
-
};
|
|
137
|
-
/**
|
|
138
|
-
* Locale configuration for a project: source, pseudo, and target locales.
|
|
139
|
-
*/
|
|
140
|
-
type MsgLocalesSettings = {
|
|
141
|
-
/** Locale of the source (authoring) strings. */
|
|
142
|
-
sourceLocale: string;
|
|
143
|
-
/** Locale used for pseudo-localization during development. */
|
|
144
|
-
pseudoLocale: string;
|
|
145
|
-
/** Target locales and their fallback load chains. */
|
|
146
|
-
targetLocales: MsgTargetLocalesSettings;
|
|
147
|
-
};
|
|
148
|
-
/**
|
|
149
|
-
* Async function that loads translated resource data for a project, title, and language.
|
|
150
|
-
*/
|
|
151
|
-
type MsgTranslationLoader = (project: string, title: string, lang: string) => Promise<MsgResourceData>;
|
|
152
|
-
/**
|
|
153
|
-
* Plain data used to create a {@link MsgProject}.
|
|
154
|
-
*/
|
|
155
|
-
type MsgProjectData = {
|
|
156
|
-
/** Project name, version, and default format. */
|
|
157
|
-
project: MsgProjectSettings;
|
|
158
|
-
/** Source, pseudo, and target locale settings. */
|
|
159
|
-
locales: MsgLocalesSettings;
|
|
160
|
-
/** Function used to load translations for a locale. */
|
|
161
|
-
loader: MsgTranslationLoader;
|
|
162
|
-
};
|
|
163
|
-
/**
|
|
164
|
-
* A localization project that holds settings, locales, and a translation loader.
|
|
165
|
-
*
|
|
166
|
-
* Resources are created against a project so they can inherit format defaults
|
|
167
|
-
* and load translations through the project's loader.
|
|
168
|
-
*/
|
|
169
|
-
declare class MsgProject {
|
|
170
|
-
/** Project name, version, and default format. */
|
|
171
|
-
_project: MsgProjectSettings;
|
|
172
|
-
/** Source, pseudo, and target locale settings. */
|
|
173
|
-
_locales: MsgLocalesSettings;
|
|
174
|
-
/** Function used to load translations for a locale. */
|
|
175
|
-
_loader: MsgTranslationLoader;
|
|
176
|
-
/**
|
|
177
|
-
* Builds a project from {@link MsgProjectData}.
|
|
178
|
-
*/
|
|
179
|
-
static create(data: MsgProjectData): MsgProject;
|
|
180
|
-
/**
|
|
181
|
-
* Creates a project from its settings and loader. Prefer {@link MsgProject.create}.
|
|
182
|
-
*/
|
|
183
|
-
private constructor();
|
|
184
|
-
/** Project name, version, and default format. */
|
|
185
|
-
get project(): MsgProjectSettings;
|
|
186
|
-
/** Source, pseudo, and target locale settings. */
|
|
187
|
-
get locales(): MsgLocalesSettings;
|
|
188
|
-
/** Function used to load translations for a locale. */
|
|
189
|
-
get loader(): MsgTranslationLoader;
|
|
190
|
-
/**
|
|
191
|
-
* The project-wide default message format. Resources (and, through them,
|
|
192
|
-
* messages) inherit this value unless they specify their own.
|
|
193
|
-
*/
|
|
194
|
-
get format(): MsgFormat;
|
|
195
|
-
/**
|
|
196
|
-
* Returns the translation load chain for a locale, or `undefined` if unsupported.
|
|
197
|
-
*/
|
|
198
|
-
getTargetLocale(locale: string): string[] | undefined;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
export { type MsgLocalesSettings as M, MsgResource, type MsgResourceData, MsgProject as a, type MsgProjectData as b, type MsgProjectSettings as c, type MsgTargetLocalesSettings as d, type MsgTranslationLoader as e };
|