@worldware/msg 0.7.0 → 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.
- package/README.md +93 -24
- package/package.json +7 -3
- package/dist/chunk-2QUOYESW.mjs +0 -184
- package/dist/chunk-QWBDIKQK.mjs +0 -10
- package/dist/chunk-WUDKNZV2.mjs +0 -71
- package/dist/chunk-XS43NAP2.mjs +0 -42
- package/dist/chunk-ZKG5QRBZ.mjs +0 -0
- package/dist/classes/MsgInterface/MsgInterface.cjs +0 -34
- package/dist/classes/MsgInterface/MsgInterface.d.cts +0 -17
- package/dist/classes/MsgInterface/MsgInterface.d.ts +0 -17
- package/dist/classes/MsgInterface/MsgInterface.mjs +0 -6
- package/dist/classes/MsgMessage/MsgMessage.cjs +0 -100
- package/dist/classes/MsgMessage/MsgMessage.d.cts +0 -36
- package/dist/classes/MsgMessage/MsgMessage.d.ts +0 -36
- package/dist/classes/MsgMessage/MsgMessage.mjs +0 -7
- package/dist/classes/MsgProject/MsgProject.cjs +0 -66
- package/dist/classes/MsgProject/MsgProject.d.cts +0 -65
- package/dist/classes/MsgProject/MsgProject.d.ts +0 -65
- package/dist/classes/MsgProject/MsgProject.mjs +0 -6
- package/dist/classes/MsgResource/MsgResource.cjs +0 -276
- package/dist/classes/MsgResource/MsgResource.d.cts +0 -4
- package/dist/classes/MsgResource/MsgResource.d.ts +0 -4
- package/dist/classes/MsgResource/MsgResource.mjs +0 -8
- package/dist/classes/index.cjs +0 -319
- package/dist/classes/index.d.cts +0 -4
- package/dist/classes/index.d.ts +0 -4
- package/dist/classes/index.mjs +0 -16
- package/dist/index.cjs +0 -319
- package/dist/index.d.cts +0 -4
- package/dist/index.d.ts +0 -4
- package/dist/index.mjs +0 -16
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,24 +43,39 @@ 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
|
|
|
51
53
|
### Basic Setup
|
|
52
54
|
|
|
55
|
+
The following example matches the ES module output of the **msg-cli** `create project` command—a typical project file that loads translations from JSON under a translations directory:
|
|
56
|
+
|
|
53
57
|
```typescript
|
|
54
|
-
import { MsgProject
|
|
58
|
+
import { MsgProject } from '@worldware/msg';
|
|
59
|
+
|
|
60
|
+
const TRANSLATION_IMPORT_PATH = '../l10n/translations';
|
|
61
|
+
const loader = async (project, title, language) => {
|
|
62
|
+
const path = `${TRANSLATION_IMPORT_PATH}/${project}/${language}/${title}.json`;
|
|
63
|
+
try {
|
|
64
|
+
const module = await import(path, { with: { type: 'json' } });
|
|
65
|
+
return module.default;
|
|
66
|
+
} catch (error) {
|
|
67
|
+
console.warn(`Translations for locale ${language} could not be loaded.`, error);
|
|
68
|
+
return {
|
|
69
|
+
title,
|
|
70
|
+
attributes: { lang: language, dir: '' },
|
|
71
|
+
notes: [],
|
|
72
|
+
messages: []
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
};
|
|
55
76
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
project: {
|
|
59
|
-
name: 'my-app',
|
|
60
|
-
version: 1
|
|
61
|
-
},
|
|
77
|
+
export default MsgProject.create({
|
|
78
|
+
project: { name: 'my-app', version: 1 },
|
|
62
79
|
locales: {
|
|
63
80
|
sourceLocale: 'en',
|
|
64
81
|
pseudoLocale: 'en-XA',
|
|
@@ -66,18 +83,15 @@ const project = MsgProject.create({
|
|
|
66
83
|
'en': ['en'],
|
|
67
84
|
'es': ['es'],
|
|
68
85
|
'fr': ['fr'],
|
|
69
|
-
'fr-CA': ['fr', 'fr-CA']
|
|
86
|
+
'fr-CA': ['fr', 'fr-CA']
|
|
70
87
|
}
|
|
71
88
|
},
|
|
72
|
-
loader
|
|
73
|
-
// Custom loader to fetch translation data
|
|
74
|
-
const path = `./translations/${project}/${lang}/${title}.json`;
|
|
75
|
-
const data = await import(path);
|
|
76
|
-
return data;
|
|
77
|
-
}
|
|
89
|
+
loader
|
|
78
90
|
});
|
|
79
91
|
```
|
|
80
92
|
|
|
93
|
+
When using this in your app, import the default export as your project and pass it to `MsgResource.create` (see below).
|
|
94
|
+
|
|
81
95
|
### Creating a Resource
|
|
82
96
|
|
|
83
97
|
```typescript
|
|
@@ -116,6 +130,42 @@ const formatted = greetingMsg?.format({ name: 'Alice' });
|
|
|
116
130
|
// Result: "Hello, Alice!"
|
|
117
131
|
```
|
|
118
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
|
+
|
|
119
169
|
### Loading Translations
|
|
120
170
|
|
|
121
171
|
```typescript
|
|
@@ -126,6 +176,19 @@ const spanishResource = await resource.getTranslation('es');
|
|
|
126
176
|
// falling back to the source messages for missing translations
|
|
127
177
|
```
|
|
128
178
|
|
|
179
|
+
### Language fallbacks and translation layering
|
|
180
|
+
|
|
181
|
+
The project's `targetLocales` maps each requested locale to a **fallback chain**: an array of locale codes ordered from least specific to most specific (e.g. base language first, then region-specific). For example, `'zh-HK': ['zh', 'zh-Hant', 'zh-HK']` means that when you request `zh-HK`, the chain is first `zh`, then `zh-Hant`, then `zh-HK`. You can get the chain for any locale with `project.getTargetLocale(locale)`.
|
|
182
|
+
|
|
183
|
+
When you call `resource.getTranslation(locale)`:
|
|
184
|
+
|
|
185
|
+
1. The **source resource** (the resource you called it on) is the base.
|
|
186
|
+
2. For each locale in that locale's chain, the project **loader** is called to load that locale's translation data.
|
|
187
|
+
3. Each loaded dataset is **layered** onto the current result: messages in the new data add or override by key; keys missing in the new layer keep the value from the previous layer.
|
|
188
|
+
4. The final resource is the result after all layers have been applied.
|
|
189
|
+
|
|
190
|
+
So for `getTranslation('zh-HK')` with chain `['zh', 'zh-Hant', 'zh-HK']`, you get: source → then zh overlay → then zh-Hant overlay → then zh-HK overlay. Later entries in the chain override earlier ones for the same key; missing keys fall back to the previous layer (and ultimately to the source).
|
|
191
|
+
|
|
129
192
|
### Pseudo Localization
|
|
130
193
|
|
|
131
194
|
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:
|
|
@@ -189,9 +252,10 @@ const data = resource.getData();
|
|
|
189
252
|
- `create(data: MsgProjectData): MsgProject` - Create a new project instance
|
|
190
253
|
|
|
191
254
|
**Properties:**
|
|
192
|
-
- `project: MsgProjectSettings` - Project name and
|
|
255
|
+
- `project: MsgProjectSettings` - Project name, version, and default `format`
|
|
193
256
|
- `locales: MsgLocalesSettings` - Locale configuration
|
|
194
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
|
|
195
259
|
|
|
196
260
|
**Methods:**
|
|
197
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`
|
|
@@ -206,7 +270,7 @@ const data = resource.getData();
|
|
|
206
270
|
- `translate(data: MsgResourceData): MsgResource` - Create a translated version
|
|
207
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.
|
|
208
272
|
- `getProject(): MsgProject` - Returns the project instance associated with the resource
|
|
209
|
-
- `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
|
|
210
274
|
- `toJSON(stripNotes?: boolean): string` - Serialize to JSON
|
|
211
275
|
|
|
212
276
|
**Properties:**
|
|
@@ -220,8 +284,8 @@ const data = resource.getData();
|
|
|
220
284
|
- `create(data: MsgMessageData): MsgMessage` - Create a new message
|
|
221
285
|
|
|
222
286
|
**Methods:**
|
|
223
|
-
- `format(data: Record<string, any>, options?: MessageFormatOptions): string` - Format the message
|
|
224
|
-
- `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)
|
|
225
289
|
- `addNote(note: MsgNote): void` - Add a note
|
|
226
290
|
- `getData(stripNotes?: boolean): MsgMessageData` - Get message data
|
|
227
291
|
- `toJSON(stripNotes?: boolean): string` - Serialize to JSON
|
|
@@ -229,9 +293,14 @@ const data = resource.getData();
|
|
|
229
293
|
**Properties:**
|
|
230
294
|
- `key: string` - Message key
|
|
231
295
|
- `value: string` - Message value
|
|
232
|
-
- `attributes: MsgAttributes` - Message attributes (lang, dir, dnt)
|
|
296
|
+
- `attributes: MsgAttributes` - Message attributes (lang, dir, dnt, format)
|
|
233
297
|
- `notes: MsgNote[]` - Message notes
|
|
234
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
|
+
|
|
235
304
|
## Development
|
|
236
305
|
|
|
237
306
|
```bash
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@worldware/msg",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Message localization tooling",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Joel Sahleen",
|
|
@@ -33,18 +33,22 @@
|
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@types/typescript": "^0.4.29",
|
|
35
35
|
"@vitest/coverage-v8": "^4.0.15",
|
|
36
|
+
"semantic-release": "^25.0.8",
|
|
36
37
|
"tsup": "^8.5.1",
|
|
38
|
+
"typedoc": "^0.28.20",
|
|
37
39
|
"typescript": "^5.9.3",
|
|
38
40
|
"vitest": "^4.0.15"
|
|
39
41
|
},
|
|
40
42
|
"dependencies": {
|
|
41
|
-
"messageformat": "
|
|
43
|
+
"@messageformat/icu-messageformat-1": "^0.12.0",
|
|
44
|
+
"messageformat": "^4.0.0",
|
|
42
45
|
"pseudo-localization": "^2.4.0"
|
|
43
46
|
},
|
|
44
47
|
"scripts": {
|
|
45
48
|
"build": "tsup",
|
|
46
49
|
"test": "vitest run",
|
|
47
50
|
"test:watch": "vitest",
|
|
48
|
-
"coverage": "vitest run --coverage"
|
|
51
|
+
"coverage": "vitest run --coverage",
|
|
52
|
+
"docs": "typedoc src/index.ts"
|
|
49
53
|
}
|
|
50
54
|
}
|
package/dist/chunk-2QUOYESW.mjs
DELETED
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
MsgMessage
|
|
3
|
-
} from "./chunk-WUDKNZV2.mjs";
|
|
4
|
-
import {
|
|
5
|
-
DEFAULT_ATTRIBUTES
|
|
6
|
-
} from "./chunk-QWBDIKQK.mjs";
|
|
7
|
-
|
|
8
|
-
// src/classes/MsgResource/MsgResource.ts
|
|
9
|
-
import { parseMessage, stringifyMessage, visit } from "messageformat";
|
|
10
|
-
import { localize } from "pseudo-localization";
|
|
11
|
-
var MsgResource = class _MsgResource extends Map {
|
|
12
|
-
_attributes = {};
|
|
13
|
-
_notes = [];
|
|
14
|
-
_title;
|
|
15
|
-
_project;
|
|
16
|
-
static create(data, project) {
|
|
17
|
-
const { title, attributes, notes, messages } = data;
|
|
18
|
-
const res = new _MsgResource(title, attributes, project, notes);
|
|
19
|
-
if (messages) {
|
|
20
|
-
messages.forEach((messageData) => {
|
|
21
|
-
const { key, value, attributes: attributes2, notes: notes2 } = messageData;
|
|
22
|
-
res.add(key, value, attributes2, notes2);
|
|
23
|
-
});
|
|
24
|
-
} else {
|
|
25
|
-
res.clear();
|
|
26
|
-
}
|
|
27
|
-
return res;
|
|
28
|
-
}
|
|
29
|
-
constructor(title, attributes, project, notes) {
|
|
30
|
-
super();
|
|
31
|
-
this._title = title;
|
|
32
|
-
this._attributes = { ...DEFAULT_ATTRIBUTES, ...attributes };
|
|
33
|
-
this._project = project;
|
|
34
|
-
if (notes) {
|
|
35
|
-
notes.forEach((note) => this.addNote(note));
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
hasMatchingAttributes(message) {
|
|
39
|
-
const res = this.attributes;
|
|
40
|
-
const msg = message.attributes;
|
|
41
|
-
return res.lang === msg.lang && res.dir === msg.dir && res.dnt === msg.dnt;
|
|
42
|
-
}
|
|
43
|
-
pseudoLocalizeMF2(source, options) {
|
|
44
|
-
const msg = parseMessage(source);
|
|
45
|
-
visit(msg, {
|
|
46
|
-
pattern: (pattern) => {
|
|
47
|
-
for (let i = 0; i < pattern.length; i++) {
|
|
48
|
-
const part = pattern[i];
|
|
49
|
-
if (typeof part === "string") {
|
|
50
|
-
pattern[i] = localize(part, options);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
return stringifyMessage(msg);
|
|
56
|
-
}
|
|
57
|
-
get attributes() {
|
|
58
|
-
return this._attributes;
|
|
59
|
-
}
|
|
60
|
-
set attributes(attributes) {
|
|
61
|
-
this._attributes = attributes;
|
|
62
|
-
}
|
|
63
|
-
get notes() {
|
|
64
|
-
return this._notes;
|
|
65
|
-
}
|
|
66
|
-
set notes(notes) {
|
|
67
|
-
this._notes = notes;
|
|
68
|
-
}
|
|
69
|
-
addNote(note) {
|
|
70
|
-
this.notes.push(note);
|
|
71
|
-
}
|
|
72
|
-
get title() {
|
|
73
|
-
return this._title;
|
|
74
|
-
}
|
|
75
|
-
set title(title) {
|
|
76
|
-
this._title = title;
|
|
77
|
-
}
|
|
78
|
-
getProject() {
|
|
79
|
-
return this._project;
|
|
80
|
-
}
|
|
81
|
-
add(key, value, attributes, notes) {
|
|
82
|
-
const merged = { ...this.attributes, ...attributes };
|
|
83
|
-
const msg = MsgMessage.create({
|
|
84
|
-
key,
|
|
85
|
-
value,
|
|
86
|
-
attributes: merged,
|
|
87
|
-
notes
|
|
88
|
-
});
|
|
89
|
-
this.set(key, msg);
|
|
90
|
-
return this;
|
|
91
|
-
}
|
|
92
|
-
translate(data) {
|
|
93
|
-
const { title, attributes, messages } = data;
|
|
94
|
-
if (title !== this.title) {
|
|
95
|
-
throw new TypeError("Title of resource and translations do not match.");
|
|
96
|
-
}
|
|
97
|
-
const translated = _MsgResource.create({
|
|
98
|
-
title,
|
|
99
|
-
attributes,
|
|
100
|
-
notes: this.notes
|
|
101
|
-
// transfer the notes
|
|
102
|
-
}, this._project);
|
|
103
|
-
this.forEach((msg) => {
|
|
104
|
-
translated.set(msg.key, msg);
|
|
105
|
-
});
|
|
106
|
-
messages?.forEach((messageData) => {
|
|
107
|
-
const { key, value, attributes: attributes2 } = messageData;
|
|
108
|
-
const msg = MsgMessage.create({
|
|
109
|
-
key,
|
|
110
|
-
value,
|
|
111
|
-
attributes: attributes2
|
|
112
|
-
});
|
|
113
|
-
const notes = this.get(key)?.notes || [];
|
|
114
|
-
notes.forEach((note) => {
|
|
115
|
-
msg.addNote(note);
|
|
116
|
-
});
|
|
117
|
-
translated.set(key, msg);
|
|
118
|
-
});
|
|
119
|
-
return translated;
|
|
120
|
-
}
|
|
121
|
-
async getTranslation(lang) {
|
|
122
|
-
const project = this._project;
|
|
123
|
-
const pseudoLocale = project.locales.pseudoLocale;
|
|
124
|
-
if (lang === pseudoLocale) {
|
|
125
|
-
const pseudolocalizedData = {
|
|
126
|
-
title: this.title,
|
|
127
|
-
attributes: { ...this.attributes, lang: pseudoLocale },
|
|
128
|
-
notes: this.notes.length > 0 ? this.notes : void 0,
|
|
129
|
-
messages: []
|
|
130
|
-
};
|
|
131
|
-
this.forEach((msg) => {
|
|
132
|
-
pseudolocalizedData.messages.push({
|
|
133
|
-
key: msg.key,
|
|
134
|
-
value: this.pseudoLocalizeMF2(msg.value),
|
|
135
|
-
attributes: { ...this.attributes, lang: pseudoLocale }
|
|
136
|
-
});
|
|
137
|
-
});
|
|
138
|
-
return this.translate(pseudolocalizedData);
|
|
139
|
-
}
|
|
140
|
-
const languageChain = project.getTargetLocale(lang);
|
|
141
|
-
if (!languageChain) {
|
|
142
|
-
throw new Error("Unsupported locale for resource.");
|
|
143
|
-
}
|
|
144
|
-
if (languageChain.length == 0) {
|
|
145
|
-
throw new Error(`Empty language chain for locale: ${lang}`);
|
|
146
|
-
}
|
|
147
|
-
let translated = this;
|
|
148
|
-
for (let i = 0; i < languageChain.length; i++) {
|
|
149
|
-
const lang2 = languageChain[i];
|
|
150
|
-
if (lang2 && project._locales.targetLocales[lang2]) {
|
|
151
|
-
translated = await translated._project._loader(project.project.name, translated.title, lang2).then((data) => translated.translate(data));
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
return translated;
|
|
155
|
-
}
|
|
156
|
-
getData(stripNotes = false) {
|
|
157
|
-
const messages = [];
|
|
158
|
-
this.forEach((msg) => {
|
|
159
|
-
if (this.hasMatchingAttributes(msg)) {
|
|
160
|
-
const data = {
|
|
161
|
-
key: msg.key,
|
|
162
|
-
value: msg.value,
|
|
163
|
-
notes: !stripNotes && msg.notes.length > 0 ? msg.notes : void 0
|
|
164
|
-
};
|
|
165
|
-
messages.push(data);
|
|
166
|
-
} else {
|
|
167
|
-
messages.push(msg.getData(stripNotes));
|
|
168
|
-
}
|
|
169
|
-
});
|
|
170
|
-
return {
|
|
171
|
-
title: this.title,
|
|
172
|
-
attributes: this.attributes,
|
|
173
|
-
notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0,
|
|
174
|
-
messages
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
toJSON(stripNotes = false) {
|
|
178
|
-
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
179
|
-
}
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
export {
|
|
183
|
-
MsgResource
|
|
184
|
-
};
|
package/dist/chunk-QWBDIKQK.mjs
DELETED
package/dist/chunk-WUDKNZV2.mjs
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DEFAULT_ATTRIBUTES
|
|
3
|
-
} from "./chunk-QWBDIKQK.mjs";
|
|
4
|
-
|
|
5
|
-
// src/classes/MsgMessage/MsgMessage.ts
|
|
6
|
-
import { MessageFormat } from "messageformat";
|
|
7
|
-
var MsgMessage = class _MsgMessage {
|
|
8
|
-
_key;
|
|
9
|
-
_value;
|
|
10
|
-
_mf;
|
|
11
|
-
_attributes;
|
|
12
|
-
_notes = [];
|
|
13
|
-
constructor(key, value, attributes, notes) {
|
|
14
|
-
this._key = key;
|
|
15
|
-
this._value = value;
|
|
16
|
-
this._attributes = attributes ? { ...DEFAULT_ATTRIBUTES, ...attributes } : {};
|
|
17
|
-
if (notes) {
|
|
18
|
-
notes.forEach((note) => this.addNote(note));
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
static create(data) {
|
|
22
|
-
const { key, value, attributes, notes } = data;
|
|
23
|
-
const message = new _MsgMessage(key, value, attributes, notes);
|
|
24
|
-
return message;
|
|
25
|
-
}
|
|
26
|
-
get key() {
|
|
27
|
-
return this._key;
|
|
28
|
-
}
|
|
29
|
-
get value() {
|
|
30
|
-
return this._value;
|
|
31
|
-
}
|
|
32
|
-
get attributes() {
|
|
33
|
-
return this._attributes;
|
|
34
|
-
}
|
|
35
|
-
get notes() {
|
|
36
|
-
return this._notes;
|
|
37
|
-
}
|
|
38
|
-
addNote(note) {
|
|
39
|
-
this.notes.push(note);
|
|
40
|
-
}
|
|
41
|
-
format(data, options) {
|
|
42
|
-
if (!this._mf) {
|
|
43
|
-
this._mf = new MessageFormat(this.attributes.lang, this.value, options);
|
|
44
|
-
}
|
|
45
|
-
return this._mf.format(data);
|
|
46
|
-
}
|
|
47
|
-
formatToParts(data, options) {
|
|
48
|
-
if (!this._mf) {
|
|
49
|
-
this._mf = new MessageFormat(this.attributes.lang, this.value, options);
|
|
50
|
-
}
|
|
51
|
-
return this._mf?.formatToParts(data);
|
|
52
|
-
}
|
|
53
|
-
getData(stripNotes = false) {
|
|
54
|
-
return {
|
|
55
|
-
key: this.key,
|
|
56
|
-
value: this.value,
|
|
57
|
-
attributes: this.attributes,
|
|
58
|
-
notes: !stripNotes && this.notes.length > 0 ? this.notes : void 0
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
toString() {
|
|
62
|
-
return this.value;
|
|
63
|
-
}
|
|
64
|
-
toJSON(stripNotes = false) {
|
|
65
|
-
return JSON.stringify(this.getData(stripNotes), null, 2);
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
export {
|
|
70
|
-
MsgMessage
|
|
71
|
-
};
|
package/dist/chunk-XS43NAP2.mjs
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
// src/classes/MsgProject/MsgProject.ts
|
|
2
|
-
var defaultProjectSettings = {
|
|
3
|
-
name: "messages",
|
|
4
|
-
version: 1
|
|
5
|
-
};
|
|
6
|
-
var defaultLocalesSettings = {
|
|
7
|
-
sourceLocale: "en",
|
|
8
|
-
pseudoLocale: "en-XA",
|
|
9
|
-
targetLocales: {
|
|
10
|
-
en: ["en"]
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
var MsgProject = class _MsgProject {
|
|
14
|
-
_project;
|
|
15
|
-
_locales;
|
|
16
|
-
_loader;
|
|
17
|
-
static create(data) {
|
|
18
|
-
const { project, locales, loader } = data;
|
|
19
|
-
return new _MsgProject(project, locales, loader);
|
|
20
|
-
}
|
|
21
|
-
constructor(projectSettings, localesSettings, loader) {
|
|
22
|
-
this._project = { ...defaultProjectSettings, ...projectSettings };
|
|
23
|
-
this._locales = { ...defaultLocalesSettings, ...localesSettings };
|
|
24
|
-
this._loader = loader;
|
|
25
|
-
}
|
|
26
|
-
get project() {
|
|
27
|
-
return this._project;
|
|
28
|
-
}
|
|
29
|
-
get locales() {
|
|
30
|
-
return this._locales;
|
|
31
|
-
}
|
|
32
|
-
get loader() {
|
|
33
|
-
return this._loader;
|
|
34
|
-
}
|
|
35
|
-
getTargetLocale(locale) {
|
|
36
|
-
return this._locales.targetLocales[locale];
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export {
|
|
41
|
-
MsgProject
|
|
42
|
-
};
|
package/dist/chunk-ZKG5QRBZ.mjs
DELETED
|
File without changes
|
|
@@ -1,34 +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/MsgInterface/MsgInterface.ts
|
|
21
|
-
var MsgInterface_exports = {};
|
|
22
|
-
__export(MsgInterface_exports, {
|
|
23
|
-
DEFAULT_ATTRIBUTES: () => DEFAULT_ATTRIBUTES
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(MsgInterface_exports);
|
|
26
|
-
var DEFAULT_ATTRIBUTES = {
|
|
27
|
-
lang: "und",
|
|
28
|
-
dir: "auto",
|
|
29
|
-
dnt: false
|
|
30
|
-
};
|
|
31
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
32
|
-
0 && (module.exports = {
|
|
33
|
-
DEFAULT_ATTRIBUTES
|
|
34
|
-
});
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
type NoteTypes = 'DESCRIPTION' | 'AUTHORSHIP' | 'PARAMETERS' | 'CONTEXT' | 'COMMENT';
|
|
2
|
-
type MsgNote = {
|
|
3
|
-
type: NoteTypes;
|
|
4
|
-
content: string;
|
|
5
|
-
};
|
|
6
|
-
type MsgAttributes = {
|
|
7
|
-
lang?: string;
|
|
8
|
-
dir?: string;
|
|
9
|
-
dnt?: boolean;
|
|
10
|
-
};
|
|
11
|
-
declare const DEFAULT_ATTRIBUTES: MsgAttributes;
|
|
12
|
-
interface MsgInterface {
|
|
13
|
-
attributes: MsgAttributes;
|
|
14
|
-
notes: MsgNote[];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export { DEFAULT_ATTRIBUTES, type MsgAttributes, type MsgInterface, type MsgNote };
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
type NoteTypes = 'DESCRIPTION' | 'AUTHORSHIP' | 'PARAMETERS' | 'CONTEXT' | 'COMMENT';
|
|
2
|
-
type MsgNote = {
|
|
3
|
-
type: NoteTypes;
|
|
4
|
-
content: string;
|
|
5
|
-
};
|
|
6
|
-
type MsgAttributes = {
|
|
7
|
-
lang?: string;
|
|
8
|
-
dir?: string;
|
|
9
|
-
dnt?: boolean;
|
|
10
|
-
};
|
|
11
|
-
declare const DEFAULT_ATTRIBUTES: MsgAttributes;
|
|
12
|
-
interface MsgInterface {
|
|
13
|
-
attributes: MsgAttributes;
|
|
14
|
-
notes: MsgNote[];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export { DEFAULT_ATTRIBUTES, type MsgAttributes, type MsgInterface, type MsgNote };
|