@worldware/msg 0.10.1 → 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 +21 -5
- 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 +2 -1
- /package/dist/{chunk-KQTWUIAM.mjs → chunk-KI3KVHLZ.mjs} +0 -0
package/README.md
CHANGED
|
@@ -215,16 +215,24 @@ So for `getTranslation('zh-HK')` with chain `['zh', 'zh-Hant', 'zh-HK']`, you ge
|
|
|
215
215
|
|
|
216
216
|
### Pseudo Localization
|
|
217
217
|
|
|
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
|
|
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`:
|
|
219
|
+
|
|
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.
|
|
219
223
|
|
|
220
224
|
```javascript
|
|
221
225
|
// Request pseudolocalized messages (project locales.pseudoLocale is 'en-XA')
|
|
222
226
|
const pseudoResource = await resource.getTranslation('en-XA');
|
|
223
227
|
|
|
224
|
-
//
|
|
225
|
-
//
|
|
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}!"
|
|
226
234
|
const greeting = pseudoResource.get('sampleKey2')?.format({ name: 'Alice' });
|
|
227
|
-
// Result
|
|
235
|
+
// Result includes accented "Hello" with Alice substituted for {$name}
|
|
228
236
|
```
|
|
229
237
|
|
|
230
238
|
### Working with Attributes and Notes
|
|
@@ -293,7 +301,7 @@ const data = resource.getData();
|
|
|
293
301
|
- `add(key: string, value: string, attributes?: MsgAttributes, notes?: MsgNote[]): MsgResource` - Add a message
|
|
294
302
|
- `addNote(type: NoteTypes, content: string): MsgResource` - Add a note (returns `this` for chaining)
|
|
295
303
|
- `translate(data: MsgResourceData): MsgResource` - Create a translated version
|
|
296
|
-
- `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.
|
|
297
305
|
- `getProject(): MsgProject` - Returns the project instance associated with the resource
|
|
298
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
|
|
299
307
|
- `toJSON(stripNotes?: boolean): string` - Serialize to JSON
|
|
@@ -326,6 +334,14 @@ const data = resource.getData();
|
|
|
326
334
|
- `MsgFormat` - `'MF1' | 'MF2' | 'NONE'`; the formatting syntax for a message.
|
|
327
335
|
- `MsgAttributes` - `{ lang?: string; dir?: string; dnt?: boolean; format?: MsgFormat }`.
|
|
328
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
|
+
|
|
329
345
|
## Development
|
|
330
346
|
|
|
331
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);
|
package/dist/classes/index.cjs
CHANGED
|
@@ -163,9 +163,87 @@ var MsgMessage = class _MsgMessage {
|
|
|
163
163
|
}
|
|
164
164
|
};
|
|
165
165
|
|
|
166
|
-
// src/
|
|
166
|
+
// src/lib/pseudo-localize.ts
|
|
167
167
|
var import_messageformat2 = require("messageformat");
|
|
168
|
+
var import_parser = require("@messageformat/parser");
|
|
168
169
|
var import_pseudo_localization = require("pseudo-localization");
|
|
170
|
+
function pseudoLocalizeMF2(source, options) {
|
|
171
|
+
const msg = (0, import_messageformat2.parseMessage)(source);
|
|
172
|
+
(0, import_messageformat2.visit)(msg, {
|
|
173
|
+
pattern: (pattern) => {
|
|
174
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
175
|
+
const part = pattern[i];
|
|
176
|
+
if (typeof part === "string") {
|
|
177
|
+
pattern[i] = (0, import_pseudo_localization.localize)(part, options);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
return (0, import_messageformat2.stringifyMessage)(msg);
|
|
183
|
+
}
|
|
184
|
+
function collectMf1ContentTokens(tokens, into = [], inFunctionParam = false) {
|
|
185
|
+
for (const token of tokens) {
|
|
186
|
+
switch (token.type) {
|
|
187
|
+
case "content":
|
|
188
|
+
if (!inFunctionParam && token.value.length > 0) {
|
|
189
|
+
into.push({
|
|
190
|
+
offset: token.ctx.offset,
|
|
191
|
+
length: token.value.length,
|
|
192
|
+
value: token.value
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
break;
|
|
196
|
+
case "plural":
|
|
197
|
+
case "select":
|
|
198
|
+
case "selectordinal":
|
|
199
|
+
for (const selectCase of token.cases) {
|
|
200
|
+
collectMf1ContentTokens(selectCase.tokens, into, inFunctionParam);
|
|
201
|
+
}
|
|
202
|
+
break;
|
|
203
|
+
case "function":
|
|
204
|
+
if (token.param) {
|
|
205
|
+
collectMf1ContentTokens(token.param, into, true);
|
|
206
|
+
}
|
|
207
|
+
break;
|
|
208
|
+
case "argument":
|
|
209
|
+
case "octothorpe":
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return into;
|
|
214
|
+
}
|
|
215
|
+
function pseudoLocalizeMF1(source, options) {
|
|
216
|
+
const spans = collectMf1ContentTokens((0, import_parser.parse)(source));
|
|
217
|
+
if (spans.length === 0) {
|
|
218
|
+
return source;
|
|
219
|
+
}
|
|
220
|
+
spans.sort((a, b) => a.offset - b.offset);
|
|
221
|
+
let result = "";
|
|
222
|
+
let cursor = 0;
|
|
223
|
+
for (const span of spans) {
|
|
224
|
+
result += source.slice(cursor, span.offset);
|
|
225
|
+
result += (0, import_pseudo_localization.localize)(span.value, options);
|
|
226
|
+
cursor = span.offset + span.length;
|
|
227
|
+
}
|
|
228
|
+
result += source.slice(cursor);
|
|
229
|
+
return result;
|
|
230
|
+
}
|
|
231
|
+
function pseudoLocalizeNone(source, options) {
|
|
232
|
+
return (0, import_pseudo_localization.localize)(source, options);
|
|
233
|
+
}
|
|
234
|
+
function pseudoLocalize(source, format, options) {
|
|
235
|
+
switch (format) {
|
|
236
|
+
case "MF1":
|
|
237
|
+
return pseudoLocalizeMF1(source, options);
|
|
238
|
+
case "NONE":
|
|
239
|
+
return pseudoLocalizeNone(source, options);
|
|
240
|
+
case "MF2":
|
|
241
|
+
default:
|
|
242
|
+
return pseudoLocalizeMF2(source, options);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// src/classes/MsgResource/MsgResource.ts
|
|
169
247
|
var MsgResource = class _MsgResource extends Map {
|
|
170
248
|
_attributes = {};
|
|
171
249
|
_notes = [];
|
|
@@ -216,23 +294,6 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
216
294
|
resolveFormat(attributes) {
|
|
217
295
|
return attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
218
296
|
}
|
|
219
|
-
/**
|
|
220
|
-
* Pseudo-localizes an MF2 message by localizing only literal text parts.
|
|
221
|
-
*/
|
|
222
|
-
pseudoLocalizeMF2(source, options) {
|
|
223
|
-
const msg = (0, import_messageformat2.parseMessage)(source);
|
|
224
|
-
(0, import_messageformat2.visit)(msg, {
|
|
225
|
-
pattern: (pattern) => {
|
|
226
|
-
for (let i = 0; i < pattern.length; i++) {
|
|
227
|
-
const part = pattern[i];
|
|
228
|
-
if (typeof part === "string") {
|
|
229
|
-
pattern[i] = (0, import_pseudo_localization.localize)(part, options);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
return (0, import_messageformat2.stringifyMessage)(msg);
|
|
235
|
-
}
|
|
236
297
|
/** Locale and formatting metadata for this resource. */
|
|
237
298
|
get attributes() {
|
|
238
299
|
return this._attributes;
|
|
@@ -345,7 +406,11 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
345
406
|
* Loads and applies translations for a locale via the project loader.
|
|
346
407
|
*
|
|
347
408
|
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
348
|
-
* copy without calling the loader.
|
|
409
|
+
* copy without calling the loader. Each message is transformed according to
|
|
410
|
+
* its resolved `format` (`MF1`, `MF2`, or `NONE`): only literal text is
|
|
411
|
+
* accented; placeholders and message-format syntax are preserved.
|
|
412
|
+
*
|
|
413
|
+
* When `lang` is omitted, returns a clone of the current resource.
|
|
349
414
|
*
|
|
350
415
|
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
351
416
|
*/
|
|
@@ -356,19 +421,17 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
356
421
|
const project = this._project;
|
|
357
422
|
const pseudoLocale = project.locales.pseudoLocale;
|
|
358
423
|
if (lang === pseudoLocale) {
|
|
424
|
+
const messages = [...this.values()].map((msg) => ({
|
|
425
|
+
key: msg.key,
|
|
426
|
+
value: pseudoLocalize(msg.value, this.resolveFormat(msg.attributes)),
|
|
427
|
+
attributes: { ...msg.attributes, lang: pseudoLocale }
|
|
428
|
+
}));
|
|
359
429
|
const pseudolocalizedData = {
|
|
360
430
|
title: this.title,
|
|
361
431
|
attributes: { ...this.attributes, lang: pseudoLocale },
|
|
362
432
|
notes: this.notes.length > 0 ? this.notes : void 0,
|
|
363
|
-
messages
|
|
433
|
+
messages
|
|
364
434
|
};
|
|
365
|
-
this.forEach((msg) => {
|
|
366
|
-
pseudolocalizedData.messages.push({
|
|
367
|
-
key: msg.key,
|
|
368
|
-
value: this.pseudoLocalizeMF2(msg.value),
|
|
369
|
-
attributes: { ...this.attributes, lang: pseudoLocale }
|
|
370
|
-
});
|
|
371
|
-
});
|
|
372
435
|
return this.translate(pseudolocalizedData);
|
|
373
436
|
}
|
|
374
437
|
const languageChain = project.getTargetLocale(lang);
|
package/dist/classes/index.d.cts
CHANGED
|
@@ -46,10 +46,6 @@ declare class MsgResource extends Map<string, MsgMessage> implements MsgInterfac
|
|
|
46
46
|
* Resolves a format from attributes, falling back to the library default.
|
|
47
47
|
*/
|
|
48
48
|
private resolveFormat;
|
|
49
|
-
/**
|
|
50
|
-
* Pseudo-localizes an MF2 message by localizing only literal text parts.
|
|
51
|
-
*/
|
|
52
|
-
private pseudoLocalizeMF2;
|
|
53
49
|
/** Locale and formatting metadata for this resource. */
|
|
54
50
|
get attributes(): MsgAttributes;
|
|
55
51
|
/** Replaces the resource's locale and formatting metadata. */
|
|
@@ -104,7 +100,11 @@ declare class MsgResource extends Map<string, MsgMessage> implements MsgInterfac
|
|
|
104
100
|
* Loads and applies translations for a locale via the project loader.
|
|
105
101
|
*
|
|
106
102
|
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
107
|
-
* copy without calling the loader.
|
|
103
|
+
* copy without calling the loader. Each message is transformed according to
|
|
104
|
+
* its resolved `format` (`MF1`, `MF2`, or `NONE`): only literal text is
|
|
105
|
+
* accented; placeholders and message-format syntax are preserved.
|
|
106
|
+
*
|
|
107
|
+
* When `lang` is omitted, returns a clone of the current resource.
|
|
108
108
|
*
|
|
109
109
|
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
110
110
|
*/
|
package/dist/classes/index.d.ts
CHANGED
|
@@ -46,10 +46,6 @@ declare class MsgResource extends Map<string, MsgMessage> implements MsgInterfac
|
|
|
46
46
|
* Resolves a format from attributes, falling back to the library default.
|
|
47
47
|
*/
|
|
48
48
|
private resolveFormat;
|
|
49
|
-
/**
|
|
50
|
-
* Pseudo-localizes an MF2 message by localizing only literal text parts.
|
|
51
|
-
*/
|
|
52
|
-
private pseudoLocalizeMF2;
|
|
53
49
|
/** Locale and formatting metadata for this resource. */
|
|
54
50
|
get attributes(): MsgAttributes;
|
|
55
51
|
/** Replaces the resource's locale and formatting metadata. */
|
|
@@ -104,7 +100,11 @@ declare class MsgResource extends Map<string, MsgMessage> implements MsgInterfac
|
|
|
104
100
|
* Loads and applies translations for a locale via the project loader.
|
|
105
101
|
*
|
|
106
102
|
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
107
|
-
* copy without calling the loader.
|
|
103
|
+
* copy without calling the loader. Each message is transformed according to
|
|
104
|
+
* its resolved `format` (`MF1`, `MF2`, or `NONE`): only literal text is
|
|
105
|
+
* accented; placeholders and message-format syntax are preserved.
|
|
106
|
+
*
|
|
107
|
+
* When `lang` is omitted, returns a clone of the current resource.
|
|
108
108
|
*
|
|
109
109
|
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
110
110
|
*/
|
package/dist/classes/index.mjs
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import "../chunk-ORHP7HGX.mjs";
|
|
2
|
-
import {
|
|
3
|
-
MsgProject
|
|
4
|
-
} from "../chunk-DJFPVXQ5.mjs";
|
|
5
2
|
import {
|
|
6
3
|
MsgResource
|
|
7
|
-
} from "../chunk-
|
|
4
|
+
} from "../chunk-K35ZABAI.mjs";
|
|
5
|
+
import "../chunk-GHDKBE4P.mjs";
|
|
8
6
|
import {
|
|
9
7
|
MsgMessage
|
|
10
8
|
} from "../chunk-OKEV7FY7.mjs";
|
|
9
|
+
import {
|
|
10
|
+
MsgProject
|
|
11
|
+
} from "../chunk-DJFPVXQ5.mjs";
|
|
11
12
|
import {
|
|
12
13
|
DEFAULT_ATTRIBUTES,
|
|
13
14
|
MSG_DEFAULT_FORMAT
|
package/dist/index.cjs
CHANGED
|
@@ -26,6 +26,10 @@ __export(index_exports, {
|
|
|
26
26
|
MsgProject: () => MsgProject,
|
|
27
27
|
MsgResource: () => MsgResource,
|
|
28
28
|
getLang: () => getLang,
|
|
29
|
+
pseudoLocalize: () => pseudoLocalize,
|
|
30
|
+
pseudoLocalizeMF1: () => pseudoLocalizeMF1,
|
|
31
|
+
pseudoLocalizeMF2: () => pseudoLocalizeMF2,
|
|
32
|
+
pseudoLocalizeNone: () => pseudoLocalizeNone,
|
|
29
33
|
setLang: () => setLang
|
|
30
34
|
});
|
|
31
35
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -165,9 +169,87 @@ var MsgMessage = class _MsgMessage {
|
|
|
165
169
|
}
|
|
166
170
|
};
|
|
167
171
|
|
|
168
|
-
// src/
|
|
172
|
+
// src/lib/pseudo-localize.ts
|
|
169
173
|
var import_messageformat2 = require("messageformat");
|
|
174
|
+
var import_parser = require("@messageformat/parser");
|
|
170
175
|
var import_pseudo_localization = require("pseudo-localization");
|
|
176
|
+
function pseudoLocalizeMF2(source, options) {
|
|
177
|
+
const msg = (0, import_messageformat2.parseMessage)(source);
|
|
178
|
+
(0, import_messageformat2.visit)(msg, {
|
|
179
|
+
pattern: (pattern) => {
|
|
180
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
181
|
+
const part = pattern[i];
|
|
182
|
+
if (typeof part === "string") {
|
|
183
|
+
pattern[i] = (0, import_pseudo_localization.localize)(part, options);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
return (0, import_messageformat2.stringifyMessage)(msg);
|
|
189
|
+
}
|
|
190
|
+
function collectMf1ContentTokens(tokens, into = [], inFunctionParam = false) {
|
|
191
|
+
for (const token of tokens) {
|
|
192
|
+
switch (token.type) {
|
|
193
|
+
case "content":
|
|
194
|
+
if (!inFunctionParam && token.value.length > 0) {
|
|
195
|
+
into.push({
|
|
196
|
+
offset: token.ctx.offset,
|
|
197
|
+
length: token.value.length,
|
|
198
|
+
value: token.value
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
break;
|
|
202
|
+
case "plural":
|
|
203
|
+
case "select":
|
|
204
|
+
case "selectordinal":
|
|
205
|
+
for (const selectCase of token.cases) {
|
|
206
|
+
collectMf1ContentTokens(selectCase.tokens, into, inFunctionParam);
|
|
207
|
+
}
|
|
208
|
+
break;
|
|
209
|
+
case "function":
|
|
210
|
+
if (token.param) {
|
|
211
|
+
collectMf1ContentTokens(token.param, into, true);
|
|
212
|
+
}
|
|
213
|
+
break;
|
|
214
|
+
case "argument":
|
|
215
|
+
case "octothorpe":
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return into;
|
|
220
|
+
}
|
|
221
|
+
function pseudoLocalizeMF1(source, options) {
|
|
222
|
+
const spans = collectMf1ContentTokens((0, import_parser.parse)(source));
|
|
223
|
+
if (spans.length === 0) {
|
|
224
|
+
return source;
|
|
225
|
+
}
|
|
226
|
+
spans.sort((a, b) => a.offset - b.offset);
|
|
227
|
+
let result = "";
|
|
228
|
+
let cursor = 0;
|
|
229
|
+
for (const span of spans) {
|
|
230
|
+
result += source.slice(cursor, span.offset);
|
|
231
|
+
result += (0, import_pseudo_localization.localize)(span.value, options);
|
|
232
|
+
cursor = span.offset + span.length;
|
|
233
|
+
}
|
|
234
|
+
result += source.slice(cursor);
|
|
235
|
+
return result;
|
|
236
|
+
}
|
|
237
|
+
function pseudoLocalizeNone(source, options) {
|
|
238
|
+
return (0, import_pseudo_localization.localize)(source, options);
|
|
239
|
+
}
|
|
240
|
+
function pseudoLocalize(source, format, options) {
|
|
241
|
+
switch (format) {
|
|
242
|
+
case "MF1":
|
|
243
|
+
return pseudoLocalizeMF1(source, options);
|
|
244
|
+
case "NONE":
|
|
245
|
+
return pseudoLocalizeNone(source, options);
|
|
246
|
+
case "MF2":
|
|
247
|
+
default:
|
|
248
|
+
return pseudoLocalizeMF2(source, options);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// src/classes/MsgResource/MsgResource.ts
|
|
171
253
|
var MsgResource = class _MsgResource extends Map {
|
|
172
254
|
_attributes = {};
|
|
173
255
|
_notes = [];
|
|
@@ -218,23 +300,6 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
218
300
|
resolveFormat(attributes) {
|
|
219
301
|
return attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
220
302
|
}
|
|
221
|
-
/**
|
|
222
|
-
* Pseudo-localizes an MF2 message by localizing only literal text parts.
|
|
223
|
-
*/
|
|
224
|
-
pseudoLocalizeMF2(source, options) {
|
|
225
|
-
const msg = (0, import_messageformat2.parseMessage)(source);
|
|
226
|
-
(0, import_messageformat2.visit)(msg, {
|
|
227
|
-
pattern: (pattern) => {
|
|
228
|
-
for (let i = 0; i < pattern.length; i++) {
|
|
229
|
-
const part = pattern[i];
|
|
230
|
-
if (typeof part === "string") {
|
|
231
|
-
pattern[i] = (0, import_pseudo_localization.localize)(part, options);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
return (0, import_messageformat2.stringifyMessage)(msg);
|
|
237
|
-
}
|
|
238
303
|
/** Locale and formatting metadata for this resource. */
|
|
239
304
|
get attributes() {
|
|
240
305
|
return this._attributes;
|
|
@@ -347,7 +412,11 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
347
412
|
* Loads and applies translations for a locale via the project loader.
|
|
348
413
|
*
|
|
349
414
|
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
350
|
-
* copy without calling the loader.
|
|
415
|
+
* copy without calling the loader. Each message is transformed according to
|
|
416
|
+
* its resolved `format` (`MF1`, `MF2`, or `NONE`): only literal text is
|
|
417
|
+
* accented; placeholders and message-format syntax are preserved.
|
|
418
|
+
*
|
|
419
|
+
* When `lang` is omitted, returns a clone of the current resource.
|
|
351
420
|
*
|
|
352
421
|
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
353
422
|
*/
|
|
@@ -358,19 +427,17 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
358
427
|
const project = this._project;
|
|
359
428
|
const pseudoLocale = project.locales.pseudoLocale;
|
|
360
429
|
if (lang2 === pseudoLocale) {
|
|
430
|
+
const messages = [...this.values()].map((msg) => ({
|
|
431
|
+
key: msg.key,
|
|
432
|
+
value: pseudoLocalize(msg.value, this.resolveFormat(msg.attributes)),
|
|
433
|
+
attributes: { ...msg.attributes, lang: pseudoLocale }
|
|
434
|
+
}));
|
|
361
435
|
const pseudolocalizedData = {
|
|
362
436
|
title: this.title,
|
|
363
437
|
attributes: { ...this.attributes, lang: pseudoLocale },
|
|
364
438
|
notes: this.notes.length > 0 ? this.notes : void 0,
|
|
365
|
-
messages
|
|
439
|
+
messages
|
|
366
440
|
};
|
|
367
|
-
this.forEach((msg) => {
|
|
368
|
-
pseudolocalizedData.messages.push({
|
|
369
|
-
key: msg.key,
|
|
370
|
-
value: this.pseudoLocalizeMF2(msg.value),
|
|
371
|
-
attributes: { ...this.attributes, lang: pseudoLocale }
|
|
372
|
-
});
|
|
373
|
-
});
|
|
374
441
|
return this.translate(pseudolocalizedData);
|
|
375
442
|
}
|
|
376
443
|
const languageChain = project.getTargetLocale(lang2);
|
|
@@ -517,5 +584,9 @@ function getLang() {
|
|
|
517
584
|
MsgProject,
|
|
518
585
|
MsgResource,
|
|
519
586
|
getLang,
|
|
587
|
+
pseudoLocalize,
|
|
588
|
+
pseudoLocalizeMF1,
|
|
589
|
+
pseudoLocalizeMF2,
|
|
590
|
+
pseudoLocalizeNone,
|
|
520
591
|
setLang
|
|
521
592
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -2,4 +2,5 @@ export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, MsgAttributes, MsgFormat, MsgIn
|
|
|
2
2
|
export { MsgMessage, MsgMessageData } from './classes/MsgMessage/MsgMessage.cjs';
|
|
3
3
|
export { MsgLocalesSettings, MsgProject, MsgProjectData, MsgProjectSettings, MsgResource, MsgResourceData, MsgTargetLocalesSettings, MsgTranslationLoader } from './classes/index.cjs';
|
|
4
4
|
export { getLang, setLang } from './lib/lang-utils.cjs';
|
|
5
|
+
export { PseudoLocalizeOptions, pseudoLocalize, pseudoLocalizeMF1, pseudoLocalizeMF2, pseudoLocalizeNone } from './lib/pseudo-localize.cjs';
|
|
5
6
|
import 'messageformat';
|
package/dist/index.d.ts
CHANGED
|
@@ -2,4 +2,5 @@ export { DEFAULT_ATTRIBUTES, MSG_DEFAULT_FORMAT, MsgAttributes, MsgFormat, MsgIn
|
|
|
2
2
|
export { MsgMessage, MsgMessageData } from './classes/MsgMessage/MsgMessage.js';
|
|
3
3
|
export { MsgLocalesSettings, MsgProject, MsgProjectData, MsgProjectSettings, MsgResource, MsgResourceData, MsgTargetLocalesSettings, MsgTranslationLoader } from './classes/index.js';
|
|
4
4
|
export { getLang, setLang } from './lib/lang-utils.js';
|
|
5
|
+
export { PseudoLocalizeOptions, pseudoLocalize, pseudoLocalizeMF1, pseudoLocalizeMF2, pseudoLocalizeNone } from './lib/pseudo-localize.js';
|
|
5
6
|
import 'messageformat';
|
package/dist/index.mjs
CHANGED
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
import "./chunk-ORHP7HGX.mjs";
|
|
2
|
-
import
|
|
2
|
+
import {
|
|
3
|
+
MsgResource
|
|
4
|
+
} from "./chunk-K35ZABAI.mjs";
|
|
5
|
+
import "./chunk-KI3KVHLZ.mjs";
|
|
3
6
|
import {
|
|
4
7
|
getLang,
|
|
5
8
|
setLang
|
|
6
9
|
} from "./chunk-PU7ZZ5XH.mjs";
|
|
7
10
|
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
} from "./chunk-
|
|
11
|
+
pseudoLocalize,
|
|
12
|
+
pseudoLocalizeMF1,
|
|
13
|
+
pseudoLocalizeMF2,
|
|
14
|
+
pseudoLocalizeNone
|
|
15
|
+
} from "./chunk-GHDKBE4P.mjs";
|
|
13
16
|
import {
|
|
14
17
|
MsgMessage
|
|
15
18
|
} from "./chunk-OKEV7FY7.mjs";
|
|
19
|
+
import {
|
|
20
|
+
MsgProject
|
|
21
|
+
} from "./chunk-DJFPVXQ5.mjs";
|
|
16
22
|
import {
|
|
17
23
|
DEFAULT_ATTRIBUTES,
|
|
18
24
|
MSG_DEFAULT_FORMAT
|
|
@@ -24,5 +30,9 @@ export {
|
|
|
24
30
|
MsgProject,
|
|
25
31
|
MsgResource,
|
|
26
32
|
getLang,
|
|
33
|
+
pseudoLocalize,
|
|
34
|
+
pseudoLocalizeMF1,
|
|
35
|
+
pseudoLocalizeMF2,
|
|
36
|
+
pseudoLocalizeNone,
|
|
27
37
|
setLang
|
|
28
38
|
};
|
package/dist/lib/index.cjs
CHANGED
|
@@ -21,6 +21,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var lib_exports = {};
|
|
22
22
|
__export(lib_exports, {
|
|
23
23
|
getLang: () => getLang,
|
|
24
|
+
pseudoLocalize: () => pseudoLocalize,
|
|
25
|
+
pseudoLocalizeMF1: () => pseudoLocalizeMF1,
|
|
26
|
+
pseudoLocalizeMF2: () => pseudoLocalizeMF2,
|
|
27
|
+
pseudoLocalizeNone: () => pseudoLocalizeNone,
|
|
24
28
|
setLang: () => setLang
|
|
25
29
|
});
|
|
26
30
|
module.exports = __toCommonJS(lib_exports);
|
|
@@ -33,8 +37,92 @@ function setLang(tag) {
|
|
|
33
37
|
function getLang() {
|
|
34
38
|
return lang;
|
|
35
39
|
}
|
|
40
|
+
|
|
41
|
+
// src/lib/pseudo-localize.ts
|
|
42
|
+
var import_messageformat = require("messageformat");
|
|
43
|
+
var import_parser = require("@messageformat/parser");
|
|
44
|
+
var import_pseudo_localization = require("pseudo-localization");
|
|
45
|
+
function pseudoLocalizeMF2(source, options) {
|
|
46
|
+
const msg = (0, import_messageformat.parseMessage)(source);
|
|
47
|
+
(0, import_messageformat.visit)(msg, {
|
|
48
|
+
pattern: (pattern) => {
|
|
49
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
50
|
+
const part = pattern[i];
|
|
51
|
+
if (typeof part === "string") {
|
|
52
|
+
pattern[i] = (0, import_pseudo_localization.localize)(part, options);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
return (0, import_messageformat.stringifyMessage)(msg);
|
|
58
|
+
}
|
|
59
|
+
function collectMf1ContentTokens(tokens, into = [], inFunctionParam = false) {
|
|
60
|
+
for (const token of tokens) {
|
|
61
|
+
switch (token.type) {
|
|
62
|
+
case "content":
|
|
63
|
+
if (!inFunctionParam && token.value.length > 0) {
|
|
64
|
+
into.push({
|
|
65
|
+
offset: token.ctx.offset,
|
|
66
|
+
length: token.value.length,
|
|
67
|
+
value: token.value
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
break;
|
|
71
|
+
case "plural":
|
|
72
|
+
case "select":
|
|
73
|
+
case "selectordinal":
|
|
74
|
+
for (const selectCase of token.cases) {
|
|
75
|
+
collectMf1ContentTokens(selectCase.tokens, into, inFunctionParam);
|
|
76
|
+
}
|
|
77
|
+
break;
|
|
78
|
+
case "function":
|
|
79
|
+
if (token.param) {
|
|
80
|
+
collectMf1ContentTokens(token.param, into, true);
|
|
81
|
+
}
|
|
82
|
+
break;
|
|
83
|
+
case "argument":
|
|
84
|
+
case "octothorpe":
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return into;
|
|
89
|
+
}
|
|
90
|
+
function pseudoLocalizeMF1(source, options) {
|
|
91
|
+
const spans = collectMf1ContentTokens((0, import_parser.parse)(source));
|
|
92
|
+
if (spans.length === 0) {
|
|
93
|
+
return source;
|
|
94
|
+
}
|
|
95
|
+
spans.sort((a, b) => a.offset - b.offset);
|
|
96
|
+
let result = "";
|
|
97
|
+
let cursor = 0;
|
|
98
|
+
for (const span of spans) {
|
|
99
|
+
result += source.slice(cursor, span.offset);
|
|
100
|
+
result += (0, import_pseudo_localization.localize)(span.value, options);
|
|
101
|
+
cursor = span.offset + span.length;
|
|
102
|
+
}
|
|
103
|
+
result += source.slice(cursor);
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
function pseudoLocalizeNone(source, options) {
|
|
107
|
+
return (0, import_pseudo_localization.localize)(source, options);
|
|
108
|
+
}
|
|
109
|
+
function pseudoLocalize(source, format, options) {
|
|
110
|
+
switch (format) {
|
|
111
|
+
case "MF1":
|
|
112
|
+
return pseudoLocalizeMF1(source, options);
|
|
113
|
+
case "NONE":
|
|
114
|
+
return pseudoLocalizeNone(source, options);
|
|
115
|
+
case "MF2":
|
|
116
|
+
default:
|
|
117
|
+
return pseudoLocalizeMF2(source, options);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
36
120
|
// Annotate the CommonJS export names for ESM import in node:
|
|
37
121
|
0 && (module.exports = {
|
|
38
122
|
getLang,
|
|
123
|
+
pseudoLocalize,
|
|
124
|
+
pseudoLocalizeMF1,
|
|
125
|
+
pseudoLocalizeMF2,
|
|
126
|
+
pseudoLocalizeNone,
|
|
39
127
|
setLang
|
|
40
128
|
});
|
package/dist/lib/index.d.cts
CHANGED
package/dist/lib/index.d.ts
CHANGED
package/dist/lib/index.mjs
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
|
-
import "../chunk-
|
|
1
|
+
import "../chunk-KI3KVHLZ.mjs";
|
|
2
2
|
import {
|
|
3
3
|
getLang,
|
|
4
4
|
setLang
|
|
5
5
|
} from "../chunk-PU7ZZ5XH.mjs";
|
|
6
|
+
import {
|
|
7
|
+
pseudoLocalize,
|
|
8
|
+
pseudoLocalizeMF1,
|
|
9
|
+
pseudoLocalizeMF2,
|
|
10
|
+
pseudoLocalizeNone
|
|
11
|
+
} from "../chunk-GHDKBE4P.mjs";
|
|
6
12
|
export {
|
|
7
13
|
getLang,
|
|
14
|
+
pseudoLocalize,
|
|
15
|
+
pseudoLocalizeMF1,
|
|
16
|
+
pseudoLocalizeMF2,
|
|
17
|
+
pseudoLocalizeNone,
|
|
8
18
|
setLang
|
|
9
19
|
};
|
|
@@ -0,0 +1,113 @@
|
|
|
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/lib/pseudo-localize.ts
|
|
21
|
+
var pseudo_localize_exports = {};
|
|
22
|
+
__export(pseudo_localize_exports, {
|
|
23
|
+
pseudoLocalize: () => pseudoLocalize,
|
|
24
|
+
pseudoLocalizeMF1: () => pseudoLocalizeMF1,
|
|
25
|
+
pseudoLocalizeMF2: () => pseudoLocalizeMF2,
|
|
26
|
+
pseudoLocalizeNone: () => pseudoLocalizeNone
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(pseudo_localize_exports);
|
|
29
|
+
var import_messageformat = require("messageformat");
|
|
30
|
+
var import_parser = require("@messageformat/parser");
|
|
31
|
+
var import_pseudo_localization = require("pseudo-localization");
|
|
32
|
+
function pseudoLocalizeMF2(source, options) {
|
|
33
|
+
const msg = (0, import_messageformat.parseMessage)(source);
|
|
34
|
+
(0, import_messageformat.visit)(msg, {
|
|
35
|
+
pattern: (pattern) => {
|
|
36
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
37
|
+
const part = pattern[i];
|
|
38
|
+
if (typeof part === "string") {
|
|
39
|
+
pattern[i] = (0, import_pseudo_localization.localize)(part, options);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
return (0, import_messageformat.stringifyMessage)(msg);
|
|
45
|
+
}
|
|
46
|
+
function collectMf1ContentTokens(tokens, into = [], inFunctionParam = false) {
|
|
47
|
+
for (const token of tokens) {
|
|
48
|
+
switch (token.type) {
|
|
49
|
+
case "content":
|
|
50
|
+
if (!inFunctionParam && token.value.length > 0) {
|
|
51
|
+
into.push({
|
|
52
|
+
offset: token.ctx.offset,
|
|
53
|
+
length: token.value.length,
|
|
54
|
+
value: token.value
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
break;
|
|
58
|
+
case "plural":
|
|
59
|
+
case "select":
|
|
60
|
+
case "selectordinal":
|
|
61
|
+
for (const selectCase of token.cases) {
|
|
62
|
+
collectMf1ContentTokens(selectCase.tokens, into, inFunctionParam);
|
|
63
|
+
}
|
|
64
|
+
break;
|
|
65
|
+
case "function":
|
|
66
|
+
if (token.param) {
|
|
67
|
+
collectMf1ContentTokens(token.param, into, true);
|
|
68
|
+
}
|
|
69
|
+
break;
|
|
70
|
+
case "argument":
|
|
71
|
+
case "octothorpe":
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return into;
|
|
76
|
+
}
|
|
77
|
+
function pseudoLocalizeMF1(source, options) {
|
|
78
|
+
const spans = collectMf1ContentTokens((0, import_parser.parse)(source));
|
|
79
|
+
if (spans.length === 0) {
|
|
80
|
+
return source;
|
|
81
|
+
}
|
|
82
|
+
spans.sort((a, b) => a.offset - b.offset);
|
|
83
|
+
let result = "";
|
|
84
|
+
let cursor = 0;
|
|
85
|
+
for (const span of spans) {
|
|
86
|
+
result += source.slice(cursor, span.offset);
|
|
87
|
+
result += (0, import_pseudo_localization.localize)(span.value, options);
|
|
88
|
+
cursor = span.offset + span.length;
|
|
89
|
+
}
|
|
90
|
+
result += source.slice(cursor);
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
function pseudoLocalizeNone(source, options) {
|
|
94
|
+
return (0, import_pseudo_localization.localize)(source, options);
|
|
95
|
+
}
|
|
96
|
+
function pseudoLocalize(source, format, options) {
|
|
97
|
+
switch (format) {
|
|
98
|
+
case "MF1":
|
|
99
|
+
return pseudoLocalizeMF1(source, options);
|
|
100
|
+
case "NONE":
|
|
101
|
+
return pseudoLocalizeNone(source, options);
|
|
102
|
+
case "MF2":
|
|
103
|
+
default:
|
|
104
|
+
return pseudoLocalizeMF2(source, options);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
108
|
+
0 && (module.exports = {
|
|
109
|
+
pseudoLocalize,
|
|
110
|
+
pseudoLocalizeMF1,
|
|
111
|
+
pseudoLocalizeMF2,
|
|
112
|
+
pseudoLocalizeNone
|
|
113
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { MsgFormat } from '../classes/MsgInterface/MsgInterface.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Options forwarded to `pseudo-localization`'s `localize`.
|
|
5
|
+
*/
|
|
6
|
+
type PseudoLocalizeOptions = {
|
|
7
|
+
strategy?: "accented" | "bidi";
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Pseudo-localizes an MF2 message by localizing only literal text parts.
|
|
11
|
+
*/
|
|
12
|
+
declare function pseudoLocalizeMF2(source: string, options?: PseudoLocalizeOptions): string;
|
|
13
|
+
/**
|
|
14
|
+
* Pseudo-localizes an MF1 (ICU MessageFormat 1) message by localizing only
|
|
15
|
+
* human-readable content tokens, preserving placeholders and syntax.
|
|
16
|
+
*
|
|
17
|
+
* Parses with `@messageformat/parser`, then rebuilds the source with localized
|
|
18
|
+
* content spans so ICU structure round-trips unchanged.
|
|
19
|
+
*/
|
|
20
|
+
declare function pseudoLocalizeMF1(source: string, options?: PseudoLocalizeOptions): string;
|
|
21
|
+
/**
|
|
22
|
+
* Pseudo-localizes an unformatted (`NONE`) string in full.
|
|
23
|
+
*/
|
|
24
|
+
declare function pseudoLocalizeNone(source: string, options?: PseudoLocalizeOptions): string;
|
|
25
|
+
/**
|
|
26
|
+
* Pseudo-localizes a message value according to its resolved {@link MsgFormat}.
|
|
27
|
+
*/
|
|
28
|
+
declare function pseudoLocalize(source: string, format: MsgFormat, options?: PseudoLocalizeOptions): string;
|
|
29
|
+
|
|
30
|
+
export { type PseudoLocalizeOptions, pseudoLocalize, pseudoLocalizeMF1, pseudoLocalizeMF2, pseudoLocalizeNone };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { MsgFormat } from '../classes/MsgInterface/MsgInterface.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Options forwarded to `pseudo-localization`'s `localize`.
|
|
5
|
+
*/
|
|
6
|
+
type PseudoLocalizeOptions = {
|
|
7
|
+
strategy?: "accented" | "bidi";
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Pseudo-localizes an MF2 message by localizing only literal text parts.
|
|
11
|
+
*/
|
|
12
|
+
declare function pseudoLocalizeMF2(source: string, options?: PseudoLocalizeOptions): string;
|
|
13
|
+
/**
|
|
14
|
+
* Pseudo-localizes an MF1 (ICU MessageFormat 1) message by localizing only
|
|
15
|
+
* human-readable content tokens, preserving placeholders and syntax.
|
|
16
|
+
*
|
|
17
|
+
* Parses with `@messageformat/parser`, then rebuilds the source with localized
|
|
18
|
+
* content spans so ICU structure round-trips unchanged.
|
|
19
|
+
*/
|
|
20
|
+
declare function pseudoLocalizeMF1(source: string, options?: PseudoLocalizeOptions): string;
|
|
21
|
+
/**
|
|
22
|
+
* Pseudo-localizes an unformatted (`NONE`) string in full.
|
|
23
|
+
*/
|
|
24
|
+
declare function pseudoLocalizeNone(source: string, options?: PseudoLocalizeOptions): string;
|
|
25
|
+
/**
|
|
26
|
+
* Pseudo-localizes a message value according to its resolved {@link MsgFormat}.
|
|
27
|
+
*/
|
|
28
|
+
declare function pseudoLocalize(source: string, format: MsgFormat, options?: PseudoLocalizeOptions): string;
|
|
29
|
+
|
|
30
|
+
export { type PseudoLocalizeOptions, pseudoLocalize, pseudoLocalizeMF1, pseudoLocalizeMF2, pseudoLocalizeNone };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@worldware/msg",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "Message localization tooling",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Joel Sahleen",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@messageformat/icu-messageformat-1": "^0.12.0",
|
|
44
|
+
"@messageformat/parser": "^5.1.1",
|
|
44
45
|
"messageformat": "^4.0.0",
|
|
45
46
|
"pseudo-localization": "^2.4.0"
|
|
46
47
|
},
|
|
File without changes
|