@worldware/msg 0.10.0 → 0.11.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 +96 -56
- package/dist/chunk-GHDKBE4P.mjs +86 -0
- package/dist/{chunk-HUWFAJAN.mjs → chunk-K35ZABAI.mjs} +14 -28
- package/dist/classes/MsgResource/MsgResource.cjs +91 -28
- package/dist/classes/MsgResource/MsgResource.mjs +2 -1
- package/dist/classes/index.cjs +90 -27
- package/dist/classes/index.d.cts +5 -5
- package/dist/classes/index.d.ts +5 -5
- package/dist/classes/index.mjs +5 -4
- package/dist/index.cjs +98 -27
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +16 -6
- package/dist/lib/index.cjs +88 -0
- package/dist/lib/index.d.cts +2 -0
- package/dist/lib/index.d.ts +2 -0
- package/dist/lib/index.mjs +11 -1
- package/dist/lib/pseudo-localize.cjs +113 -0
- package/dist/lib/pseudo-localize.d.cts +30 -0
- package/dist/lib/pseudo-localize.d.ts +30 -0
- package/dist/lib/pseudo-localize.mjs +12 -0
- package/package.json +3 -2
- /package/dist/{chunk-KQTWUIAM.mjs → chunk-KI3KVHLZ.mjs} +0 -0
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
|
|
@@ -191,23 +215,31 @@ So for `getTranslation('zh-HK')` with chain `['zh', 'zh-Hant', 'zh-HK']`, you ge
|
|
|
191
215
|
|
|
192
216
|
### Pseudo Localization
|
|
193
217
|
|
|
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
|
|
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. Each message is handled according to its resolved `format`:
|
|
195
219
|
|
|
196
|
-
|
|
220
|
+
- **MF2** — only literal text parts are transformed; expressions like `{$name}` stay intact.
|
|
221
|
+
- **MF1** — only human-readable content is transformed; ICU placeholders, plural/select structure, `#`, and formatter styles (e.g. `::currency/EUR`) stay intact.
|
|
222
|
+
- **NONE** — the whole string is transformed.
|
|
223
|
+
|
|
224
|
+
```javascript
|
|
197
225
|
// Request pseudolocalized messages (project locales.pseudoLocale is 'en-XA')
|
|
198
226
|
const pseudoResource = await resource.getTranslation('en-XA');
|
|
199
227
|
|
|
200
|
-
//
|
|
201
|
-
//
|
|
202
|
-
|
|
203
|
-
|
|
228
|
+
// MF1 example: literals are accented; syntax is preserved
|
|
229
|
+
// "{count, plural, one {# file} other {# files}}"
|
|
230
|
+
// → "{count, plural, one {# ƒīŀḗ} other {# ƒīŀḗş}}"
|
|
231
|
+
const files = pseudoResource.get('files')?.format({ count: 2 });
|
|
232
|
+
|
|
233
|
+
// MF2 example: "Hello, {$name}!" → "Ħḗŀŀǿ, {$name}!"
|
|
234
|
+
const greeting = pseudoResource.get('sampleKey2')?.format({ name: 'Alice' });
|
|
235
|
+
// Result includes accented "Hello" with Alice substituted for {$name}
|
|
204
236
|
```
|
|
205
237
|
|
|
206
238
|
### Working with Attributes and Notes
|
|
207
239
|
|
|
208
|
-
```
|
|
209
|
-
// Add notes to messages
|
|
210
|
-
resource.add('complex-message', 'You have {
|
|
240
|
+
```javascript
|
|
241
|
+
// Add notes to messages (MF1 syntax, matching the Main project's format)
|
|
242
|
+
resource.add('complex-message', 'You have {count} items', {
|
|
211
243
|
lang: 'en',
|
|
212
244
|
dir: 'ltr',
|
|
213
245
|
dnt: false // do-not-translate flag
|
|
@@ -269,7 +301,7 @@ const data = resource.getData();
|
|
|
269
301
|
- `add(key: string, value: string, attributes?: MsgAttributes, notes?: MsgNote[]): MsgResource` - Add a message
|
|
270
302
|
- `addNote(type: NoteTypes, content: string): MsgResource` - Add a note (returns `this` for chaining)
|
|
271
303
|
- `translate(data: MsgResourceData): MsgResource` - Create a translated version
|
|
272
|
-
- `getTranslation(lang
|
|
304
|
+
- `getTranslation(lang?: string): Promise<MsgResource>` - Load and apply translations. When `lang` matches the project's `pseudoLocale`, returns a resource with message values pseudo-localized according to each message's resolved `format` (`MF1` / `MF2` / `NONE`) instead of loading from the loader. When `lang` is omitted, returns a clone of the current resource.
|
|
273
305
|
- `getProject(): MsgProject` - Returns the project instance associated with the resource
|
|
274
306
|
- `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
|
|
275
307
|
- `toJSON(stripNotes?: boolean): string` - Serialize to JSON
|
|
@@ -302,6 +334,14 @@ const data = resource.getData();
|
|
|
302
334
|
- `MsgFormat` - `'MF1' | 'MF2' | 'NONE'`; the formatting syntax for a message.
|
|
303
335
|
- `MsgAttributes` - `{ lang?: string; dir?: string; dnt?: boolean; format?: MsgFormat }`.
|
|
304
336
|
|
|
337
|
+
### Pseudo-localization helpers
|
|
338
|
+
|
|
339
|
+
Exported from `@worldware/msg` for advanced use (also used internally by `MsgResource.getTranslation`):
|
|
340
|
+
|
|
341
|
+
- `pseudoLocalize(source, format, options?)` - Dispatch by `MsgFormat`
|
|
342
|
+
- `pseudoLocalizeMF1(source, options?)` / `pseudoLocalizeMF2(source, options?)` / `pseudoLocalizeNone(source, options?)` - Format-specific helpers
|
|
343
|
+
- `PseudoLocalizeOptions` - `{ strategy?: 'accented' | 'bidi' }`
|
|
344
|
+
|
|
305
345
|
## Development
|
|
306
346
|
|
|
307
347
|
```bash
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// src/lib/pseudo-localize.ts
|
|
2
|
+
import { parseMessage, stringifyMessage, visit } from "messageformat";
|
|
3
|
+
import { parse } from "@messageformat/parser";
|
|
4
|
+
import { localize } from "pseudo-localization";
|
|
5
|
+
function pseudoLocalizeMF2(source, options) {
|
|
6
|
+
const msg = parseMessage(source);
|
|
7
|
+
visit(msg, {
|
|
8
|
+
pattern: (pattern) => {
|
|
9
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
10
|
+
const part = pattern[i];
|
|
11
|
+
if (typeof part === "string") {
|
|
12
|
+
pattern[i] = localize(part, options);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
return stringifyMessage(msg);
|
|
18
|
+
}
|
|
19
|
+
function collectMf1ContentTokens(tokens, into = [], inFunctionParam = false) {
|
|
20
|
+
for (const token of tokens) {
|
|
21
|
+
switch (token.type) {
|
|
22
|
+
case "content":
|
|
23
|
+
if (!inFunctionParam && token.value.length > 0) {
|
|
24
|
+
into.push({
|
|
25
|
+
offset: token.ctx.offset,
|
|
26
|
+
length: token.value.length,
|
|
27
|
+
value: token.value
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
break;
|
|
31
|
+
case "plural":
|
|
32
|
+
case "select":
|
|
33
|
+
case "selectordinal":
|
|
34
|
+
for (const selectCase of token.cases) {
|
|
35
|
+
collectMf1ContentTokens(selectCase.tokens, into, inFunctionParam);
|
|
36
|
+
}
|
|
37
|
+
break;
|
|
38
|
+
case "function":
|
|
39
|
+
if (token.param) {
|
|
40
|
+
collectMf1ContentTokens(token.param, into, true);
|
|
41
|
+
}
|
|
42
|
+
break;
|
|
43
|
+
case "argument":
|
|
44
|
+
case "octothorpe":
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return into;
|
|
49
|
+
}
|
|
50
|
+
function pseudoLocalizeMF1(source, options) {
|
|
51
|
+
const spans = collectMf1ContentTokens(parse(source));
|
|
52
|
+
if (spans.length === 0) {
|
|
53
|
+
return source;
|
|
54
|
+
}
|
|
55
|
+
spans.sort((a, b) => a.offset - b.offset);
|
|
56
|
+
let result = "";
|
|
57
|
+
let cursor = 0;
|
|
58
|
+
for (const span of spans) {
|
|
59
|
+
result += source.slice(cursor, span.offset);
|
|
60
|
+
result += localize(span.value, options);
|
|
61
|
+
cursor = span.offset + span.length;
|
|
62
|
+
}
|
|
63
|
+
result += source.slice(cursor);
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
function pseudoLocalizeNone(source, options) {
|
|
67
|
+
return localize(source, options);
|
|
68
|
+
}
|
|
69
|
+
function pseudoLocalize(source, format, options) {
|
|
70
|
+
switch (format) {
|
|
71
|
+
case "MF1":
|
|
72
|
+
return pseudoLocalizeMF1(source, options);
|
|
73
|
+
case "NONE":
|
|
74
|
+
return pseudoLocalizeNone(source, options);
|
|
75
|
+
case "MF2":
|
|
76
|
+
default:
|
|
77
|
+
return pseudoLocalizeMF2(source, options);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export {
|
|
82
|
+
pseudoLocalizeMF2,
|
|
83
|
+
pseudoLocalizeMF1,
|
|
84
|
+
pseudoLocalizeNone,
|
|
85
|
+
pseudoLocalize
|
|
86
|
+
};
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import {
|
|
2
|
+
pseudoLocalize
|
|
3
|
+
} from "./chunk-GHDKBE4P.mjs";
|
|
1
4
|
import {
|
|
2
5
|
MsgMessage
|
|
3
6
|
} from "./chunk-OKEV7FY7.mjs";
|
|
@@ -7,8 +10,6 @@ import {
|
|
|
7
10
|
} from "./chunk-XQVALCKX.mjs";
|
|
8
11
|
|
|
9
12
|
// src/classes/MsgResource/MsgResource.ts
|
|
10
|
-
import { parseMessage, stringifyMessage, visit } from "messageformat";
|
|
11
|
-
import { localize } from "pseudo-localization";
|
|
12
13
|
var MsgResource = class _MsgResource extends Map {
|
|
13
14
|
_attributes = {};
|
|
14
15
|
_notes = [];
|
|
@@ -59,23 +60,6 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
59
60
|
resolveFormat(attributes) {
|
|
60
61
|
return attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
61
62
|
}
|
|
62
|
-
/**
|
|
63
|
-
* Pseudo-localizes an MF2 message by localizing only literal text parts.
|
|
64
|
-
*/
|
|
65
|
-
pseudoLocalizeMF2(source, options) {
|
|
66
|
-
const msg = parseMessage(source);
|
|
67
|
-
visit(msg, {
|
|
68
|
-
pattern: (pattern) => {
|
|
69
|
-
for (let i = 0; i < pattern.length; i++) {
|
|
70
|
-
const part = pattern[i];
|
|
71
|
-
if (typeof part === "string") {
|
|
72
|
-
pattern[i] = localize(part, options);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
return stringifyMessage(msg);
|
|
78
|
-
}
|
|
79
63
|
/** Locale and formatting metadata for this resource. */
|
|
80
64
|
get attributes() {
|
|
81
65
|
return this._attributes;
|
|
@@ -188,7 +172,11 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
188
172
|
* Loads and applies translations for a locale via the project loader.
|
|
189
173
|
*
|
|
190
174
|
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
191
|
-
* copy without calling the loader.
|
|
175
|
+
* copy without calling the loader. Each message is transformed according to
|
|
176
|
+
* its resolved `format` (`MF1`, `MF2`, or `NONE`): only literal text is
|
|
177
|
+
* accented; placeholders and message-format syntax are preserved.
|
|
178
|
+
*
|
|
179
|
+
* When `lang` is omitted, returns a clone of the current resource.
|
|
192
180
|
*
|
|
193
181
|
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
194
182
|
*/
|
|
@@ -199,19 +187,17 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
199
187
|
const project = this._project;
|
|
200
188
|
const pseudoLocale = project.locales.pseudoLocale;
|
|
201
189
|
if (lang === pseudoLocale) {
|
|
190
|
+
const messages = [...this.values()].map((msg) => ({
|
|
191
|
+
key: msg.key,
|
|
192
|
+
value: pseudoLocalize(msg.value, this.resolveFormat(msg.attributes)),
|
|
193
|
+
attributes: { ...msg.attributes, lang: pseudoLocale }
|
|
194
|
+
}));
|
|
202
195
|
const pseudolocalizedData = {
|
|
203
196
|
title: this.title,
|
|
204
197
|
attributes: { ...this.attributes, lang: pseudoLocale },
|
|
205
198
|
notes: this.notes.length > 0 ? this.notes : void 0,
|
|
206
|
-
messages
|
|
199
|
+
messages
|
|
207
200
|
};
|
|
208
|
-
this.forEach((msg) => {
|
|
209
|
-
pseudolocalizedData.messages.push({
|
|
210
|
-
key: msg.key,
|
|
211
|
-
value: this.pseudoLocalizeMF2(msg.value),
|
|
212
|
-
attributes: { ...this.attributes, lang: pseudoLocale }
|
|
213
|
-
});
|
|
214
|
-
});
|
|
215
201
|
return this.translate(pseudolocalizedData);
|
|
216
202
|
}
|
|
217
203
|
const languageChain = project.getTargetLocale(lang);
|
|
@@ -23,8 +23,6 @@ __export(MsgResource_exports, {
|
|
|
23
23
|
MsgResource: () => MsgResource
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(MsgResource_exports);
|
|
26
|
-
var import_messageformat2 = require("messageformat");
|
|
27
|
-
var import_pseudo_localization = require("pseudo-localization");
|
|
28
26
|
|
|
29
27
|
// src/classes/MsgMessage/MsgMessage.ts
|
|
30
28
|
var import_messageformat = require("messageformat");
|
|
@@ -163,6 +161,86 @@ var MsgMessage = class _MsgMessage {
|
|
|
163
161
|
}
|
|
164
162
|
};
|
|
165
163
|
|
|
164
|
+
// src/lib/pseudo-localize.ts
|
|
165
|
+
var import_messageformat2 = require("messageformat");
|
|
166
|
+
var import_parser = require("@messageformat/parser");
|
|
167
|
+
var import_pseudo_localization = require("pseudo-localization");
|
|
168
|
+
function pseudoLocalizeMF2(source, options) {
|
|
169
|
+
const msg = (0, import_messageformat2.parseMessage)(source);
|
|
170
|
+
(0, import_messageformat2.visit)(msg, {
|
|
171
|
+
pattern: (pattern) => {
|
|
172
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
173
|
+
const part = pattern[i];
|
|
174
|
+
if (typeof part === "string") {
|
|
175
|
+
pattern[i] = (0, import_pseudo_localization.localize)(part, options);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
return (0, import_messageformat2.stringifyMessage)(msg);
|
|
181
|
+
}
|
|
182
|
+
function collectMf1ContentTokens(tokens, into = [], inFunctionParam = false) {
|
|
183
|
+
for (const token of tokens) {
|
|
184
|
+
switch (token.type) {
|
|
185
|
+
case "content":
|
|
186
|
+
if (!inFunctionParam && token.value.length > 0) {
|
|
187
|
+
into.push({
|
|
188
|
+
offset: token.ctx.offset,
|
|
189
|
+
length: token.value.length,
|
|
190
|
+
value: token.value
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
break;
|
|
194
|
+
case "plural":
|
|
195
|
+
case "select":
|
|
196
|
+
case "selectordinal":
|
|
197
|
+
for (const selectCase of token.cases) {
|
|
198
|
+
collectMf1ContentTokens(selectCase.tokens, into, inFunctionParam);
|
|
199
|
+
}
|
|
200
|
+
break;
|
|
201
|
+
case "function":
|
|
202
|
+
if (token.param) {
|
|
203
|
+
collectMf1ContentTokens(token.param, into, true);
|
|
204
|
+
}
|
|
205
|
+
break;
|
|
206
|
+
case "argument":
|
|
207
|
+
case "octothorpe":
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return into;
|
|
212
|
+
}
|
|
213
|
+
function pseudoLocalizeMF1(source, options) {
|
|
214
|
+
const spans = collectMf1ContentTokens((0, import_parser.parse)(source));
|
|
215
|
+
if (spans.length === 0) {
|
|
216
|
+
return source;
|
|
217
|
+
}
|
|
218
|
+
spans.sort((a, b) => a.offset - b.offset);
|
|
219
|
+
let result = "";
|
|
220
|
+
let cursor = 0;
|
|
221
|
+
for (const span of spans) {
|
|
222
|
+
result += source.slice(cursor, span.offset);
|
|
223
|
+
result += (0, import_pseudo_localization.localize)(span.value, options);
|
|
224
|
+
cursor = span.offset + span.length;
|
|
225
|
+
}
|
|
226
|
+
result += source.slice(cursor);
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
229
|
+
function pseudoLocalizeNone(source, options) {
|
|
230
|
+
return (0, import_pseudo_localization.localize)(source, options);
|
|
231
|
+
}
|
|
232
|
+
function pseudoLocalize(source, format, options) {
|
|
233
|
+
switch (format) {
|
|
234
|
+
case "MF1":
|
|
235
|
+
return pseudoLocalizeMF1(source, options);
|
|
236
|
+
case "NONE":
|
|
237
|
+
return pseudoLocalizeNone(source, options);
|
|
238
|
+
case "MF2":
|
|
239
|
+
default:
|
|
240
|
+
return pseudoLocalizeMF2(source, options);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
166
244
|
// src/classes/MsgResource/MsgResource.ts
|
|
167
245
|
var MsgResource = class _MsgResource extends Map {
|
|
168
246
|
_attributes = {};
|
|
@@ -214,23 +292,6 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
214
292
|
resolveFormat(attributes) {
|
|
215
293
|
return attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
216
294
|
}
|
|
217
|
-
/**
|
|
218
|
-
* Pseudo-localizes an MF2 message by localizing only literal text parts.
|
|
219
|
-
*/
|
|
220
|
-
pseudoLocalizeMF2(source, options) {
|
|
221
|
-
const msg = (0, import_messageformat2.parseMessage)(source);
|
|
222
|
-
(0, import_messageformat2.visit)(msg, {
|
|
223
|
-
pattern: (pattern) => {
|
|
224
|
-
for (let i = 0; i < pattern.length; i++) {
|
|
225
|
-
const part = pattern[i];
|
|
226
|
-
if (typeof part === "string") {
|
|
227
|
-
pattern[i] = (0, import_pseudo_localization.localize)(part, options);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
});
|
|
232
|
-
return (0, import_messageformat2.stringifyMessage)(msg);
|
|
233
|
-
}
|
|
234
295
|
/** Locale and formatting metadata for this resource. */
|
|
235
296
|
get attributes() {
|
|
236
297
|
return this._attributes;
|
|
@@ -343,7 +404,11 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
343
404
|
* Loads and applies translations for a locale via the project loader.
|
|
344
405
|
*
|
|
345
406
|
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
346
|
-
* copy without calling the loader.
|
|
407
|
+
* copy without calling the loader. Each message is transformed according to
|
|
408
|
+
* its resolved `format` (`MF1`, `MF2`, or `NONE`): only literal text is
|
|
409
|
+
* accented; placeholders and message-format syntax are preserved.
|
|
410
|
+
*
|
|
411
|
+
* When `lang` is omitted, returns a clone of the current resource.
|
|
347
412
|
*
|
|
348
413
|
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
349
414
|
*/
|
|
@@ -354,19 +419,17 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
354
419
|
const project = this._project;
|
|
355
420
|
const pseudoLocale = project.locales.pseudoLocale;
|
|
356
421
|
if (lang === pseudoLocale) {
|
|
422
|
+
const messages = [...this.values()].map((msg) => ({
|
|
423
|
+
key: msg.key,
|
|
424
|
+
value: pseudoLocalize(msg.value, this.resolveFormat(msg.attributes)),
|
|
425
|
+
attributes: { ...msg.attributes, lang: pseudoLocale }
|
|
426
|
+
}));
|
|
357
427
|
const pseudolocalizedData = {
|
|
358
428
|
title: this.title,
|
|
359
429
|
attributes: { ...this.attributes, lang: pseudoLocale },
|
|
360
430
|
notes: this.notes.length > 0 ? this.notes : void 0,
|
|
361
|
-
messages
|
|
431
|
+
messages
|
|
362
432
|
};
|
|
363
|
-
this.forEach((msg) => {
|
|
364
|
-
pseudolocalizedData.messages.push({
|
|
365
|
-
key: msg.key,
|
|
366
|
-
value: this.pseudoLocalizeMF2(msg.value),
|
|
367
|
-
attributes: { ...this.attributes, lang: pseudoLocale }
|
|
368
|
-
});
|
|
369
|
-
});
|
|
370
433
|
return this.translate(pseudolocalizedData);
|
|
371
434
|
}
|
|
372
435
|
const languageChain = project.getTargetLocale(lang);
|