@worldware/msg 0.10.1 → 0.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -7
- package/dist/chunk-GHDKBE4P.mjs +86 -0
- package/dist/chunk-QU3HGZUN.mjs +16 -0
- package/dist/{chunk-OKEV7FY7.mjs → chunk-RMZ42XIY.mjs} +4 -2
- package/dist/{chunk-HUWFAJAN.mjs → chunk-VDOVMTPF.mjs} +20 -32
- package/dist/classes/MsgInterface/MsgInterface.d.cts +1 -1
- package/dist/classes/MsgInterface/MsgInterface.d.ts +1 -1
- package/dist/classes/MsgMessage/MsgMessage.cjs +10 -1
- package/dist/classes/MsgMessage/MsgMessage.mjs +2 -1
- package/dist/classes/MsgResource/MsgResource.cjs +103 -31
- package/dist/classes/MsgResource/MsgResource.mjs +4 -2
- package/dist/classes/index.cjs +104 -30
- package/dist/classes/index.d.cts +5 -5
- package/dist/classes/index.d.ts +5 -5
- package/dist/classes/index.mjs +4 -2
- package/dist/index.cjs +112 -30
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +18 -7
- package/dist/lib/apply-attributes.cjs +45 -0
- package/dist/lib/apply-attributes.d.cts +11 -0
- package/dist/lib/apply-attributes.d.ts +11 -0
- package/dist/lib/apply-attributes.mjs +7 -0
- 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
|
@@ -67,7 +67,7 @@ const loader = async (project, title, language) => {
|
|
|
67
67
|
console.warn(`Translations for locale ${language} could not be loaded.`, error);
|
|
68
68
|
return {
|
|
69
69
|
title,
|
|
70
|
-
attributes: { lang: language, dir: '' },
|
|
70
|
+
attributes: { lang: language, dir: 'auto' },
|
|
71
71
|
notes: [],
|
|
72
72
|
messages: []
|
|
73
73
|
};
|
|
@@ -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
|
|
@@ -324,7 +332,15 @@ const data = resource.getData();
|
|
|
324
332
|
### Types
|
|
325
333
|
|
|
326
334
|
- `MsgFormat` - `'MF1' | 'MF2' | 'NONE'`; the formatting syntax for a message.
|
|
327
|
-
- `MsgAttributes` - `{ lang?: string; dir?: string; dnt?: boolean; format?: MsgFormat }`.
|
|
335
|
+
- `MsgAttributes` - `{ lang?: string; dir?: string; dnt?: boolean; format?: MsgFormat }`. Omitted or empty `dir` resolves to `'auto'`.
|
|
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' }`
|
|
328
344
|
|
|
329
345
|
## Development
|
|
330
346
|
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_ATTRIBUTES
|
|
3
|
+
} from "./chunk-XQVALCKX.mjs";
|
|
4
|
+
|
|
5
|
+
// src/lib/apply-attributes.ts
|
|
6
|
+
function applyAttributes(attributes, extras) {
|
|
7
|
+
const merged = { ...DEFAULT_ATTRIBUTES, ...extras, ...attributes };
|
|
8
|
+
if (!merged.dir) {
|
|
9
|
+
merged.dir = "auto";
|
|
10
|
+
}
|
|
11
|
+
return merged;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
applyAttributes
|
|
16
|
+
};
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
applyAttributes
|
|
3
|
+
} from "./chunk-QU3HGZUN.mjs";
|
|
4
|
+
import {
|
|
3
5
|
MSG_DEFAULT_FORMAT
|
|
4
6
|
} from "./chunk-XQVALCKX.mjs";
|
|
5
7
|
|
|
@@ -18,7 +20,7 @@ var MsgMessage = class _MsgMessage {
|
|
|
18
20
|
constructor(key, value, attributes, notes) {
|
|
19
21
|
this._key = key;
|
|
20
22
|
this._value = value;
|
|
21
|
-
this._attributes = attributes
|
|
23
|
+
this._attributes = applyAttributes(attributes);
|
|
22
24
|
if (notes) {
|
|
23
25
|
notes.forEach((note) => this.setNote(note));
|
|
24
26
|
}
|
|
@@ -1,14 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
pseudoLocalize
|
|
3
|
+
} from "./chunk-GHDKBE4P.mjs";
|
|
1
4
|
import {
|
|
2
5
|
MsgMessage
|
|
3
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-RMZ42XIY.mjs";
|
|
7
|
+
import {
|
|
8
|
+
applyAttributes
|
|
9
|
+
} from "./chunk-QU3HGZUN.mjs";
|
|
4
10
|
import {
|
|
5
|
-
DEFAULT_ATTRIBUTES,
|
|
6
11
|
MSG_DEFAULT_FORMAT
|
|
7
12
|
} from "./chunk-XQVALCKX.mjs";
|
|
8
13
|
|
|
9
14
|
// src/classes/MsgResource/MsgResource.ts
|
|
10
|
-
import { parseMessage, stringifyMessage, visit } from "messageformat";
|
|
11
|
-
import { localize } from "pseudo-localization";
|
|
12
15
|
var MsgResource = class _MsgResource extends Map {
|
|
13
16
|
_attributes = {};
|
|
14
17
|
_notes = [];
|
|
@@ -39,7 +42,7 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
39
42
|
constructor(title, attributes, project, notes) {
|
|
40
43
|
super();
|
|
41
44
|
this._title = title;
|
|
42
|
-
this._attributes = {
|
|
45
|
+
this._attributes = applyAttributes(attributes, { format: project.format });
|
|
43
46
|
this._project = project;
|
|
44
47
|
if (notes) {
|
|
45
48
|
notes.forEach((note) => this.setNote(note));
|
|
@@ -59,30 +62,13 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
59
62
|
resolveFormat(attributes) {
|
|
60
63
|
return attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
61
64
|
}
|
|
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
65
|
/** Locale and formatting metadata for this resource. */
|
|
80
66
|
get attributes() {
|
|
81
67
|
return this._attributes;
|
|
82
68
|
}
|
|
83
69
|
/** Replaces the resource's locale and formatting metadata. */
|
|
84
70
|
set attributes(attributes) {
|
|
85
|
-
this._attributes = attributes;
|
|
71
|
+
this._attributes = applyAttributes(attributes);
|
|
86
72
|
}
|
|
87
73
|
/** Notes attached to this resource for translators and tooling. */
|
|
88
74
|
get notes() {
|
|
@@ -188,7 +174,11 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
188
174
|
* Loads and applies translations for a locale via the project loader.
|
|
189
175
|
*
|
|
190
176
|
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
191
|
-
* copy without calling the loader.
|
|
177
|
+
* copy without calling the loader. Each message is transformed according to
|
|
178
|
+
* its resolved `format` (`MF1`, `MF2`, or `NONE`): only literal text is
|
|
179
|
+
* accented; placeholders and message-format syntax are preserved.
|
|
180
|
+
*
|
|
181
|
+
* When `lang` is omitted, returns a clone of the current resource.
|
|
192
182
|
*
|
|
193
183
|
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
194
184
|
*/
|
|
@@ -199,19 +189,17 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
199
189
|
const project = this._project;
|
|
200
190
|
const pseudoLocale = project.locales.pseudoLocale;
|
|
201
191
|
if (lang === pseudoLocale) {
|
|
192
|
+
const messages = [...this.values()].map((msg) => ({
|
|
193
|
+
key: msg.key,
|
|
194
|
+
value: pseudoLocalize(msg.value, this.resolveFormat(msg.attributes)),
|
|
195
|
+
attributes: { ...msg.attributes, lang: pseudoLocale }
|
|
196
|
+
}));
|
|
202
197
|
const pseudolocalizedData = {
|
|
203
198
|
title: this.title,
|
|
204
199
|
attributes: { ...this.attributes, lang: pseudoLocale },
|
|
205
200
|
notes: this.notes.length > 0 ? this.notes : void 0,
|
|
206
|
-
messages
|
|
201
|
+
messages
|
|
207
202
|
};
|
|
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
203
|
return this.translate(pseudolocalizedData);
|
|
216
204
|
}
|
|
217
205
|
const languageChain = project.getTargetLocale(lang);
|
|
@@ -30,7 +30,7 @@ declare const MSG_DEFAULT_FORMAT: MsgFormat;
|
|
|
30
30
|
type MsgAttributes = {
|
|
31
31
|
/** BCP 47 language tag for the content (for example, `en` or `zh-Hans`). */
|
|
32
32
|
lang?: string;
|
|
33
|
-
/** Text direction: typically `ltr`, `rtl`, or `auto`. */
|
|
33
|
+
/** Text direction: typically `ltr`, `rtl`, or `auto`. Omitted or empty values resolve to `'auto'`. */
|
|
34
34
|
dir?: string;
|
|
35
35
|
/** When true, the content should not be translated (Do Not Translate). */
|
|
36
36
|
dnt?: boolean;
|
|
@@ -30,7 +30,7 @@ declare const MSG_DEFAULT_FORMAT: MsgFormat;
|
|
|
30
30
|
type MsgAttributes = {
|
|
31
31
|
/** BCP 47 language tag for the content (for example, `en` or `zh-Hans`). */
|
|
32
32
|
lang?: string;
|
|
33
|
-
/** Text direction: typically `ltr`, `rtl`, or `auto`. */
|
|
33
|
+
/** Text direction: typically `ltr`, `rtl`, or `auto`. Omitted or empty values resolve to `'auto'`. */
|
|
34
34
|
dir?: string;
|
|
35
35
|
/** When true, the content should not be translated (Do Not Translate). */
|
|
36
36
|
dnt?: boolean;
|
|
@@ -34,6 +34,15 @@ var DEFAULT_ATTRIBUTES = {
|
|
|
34
34
|
dnt: false
|
|
35
35
|
};
|
|
36
36
|
|
|
37
|
+
// src/lib/apply-attributes.ts
|
|
38
|
+
function applyAttributes(attributes, extras) {
|
|
39
|
+
const merged = { ...DEFAULT_ATTRIBUTES, ...extras, ...attributes };
|
|
40
|
+
if (!merged.dir) {
|
|
41
|
+
merged.dir = "auto";
|
|
42
|
+
}
|
|
43
|
+
return merged;
|
|
44
|
+
}
|
|
45
|
+
|
|
37
46
|
// src/classes/MsgMessage/MsgMessage.ts
|
|
38
47
|
var MsgMessage = class _MsgMessage {
|
|
39
48
|
_key;
|
|
@@ -47,7 +56,7 @@ var MsgMessage = class _MsgMessage {
|
|
|
47
56
|
constructor(key, value, attributes, notes) {
|
|
48
57
|
this._key = key;
|
|
49
58
|
this._value = value;
|
|
50
|
-
this._attributes = attributes
|
|
59
|
+
this._attributes = applyAttributes(attributes);
|
|
51
60
|
if (notes) {
|
|
52
61
|
notes.forEach((note) => this.setNote(note));
|
|
53
62
|
}
|
|
@@ -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");
|
|
@@ -38,6 +36,15 @@ var DEFAULT_ATTRIBUTES = {
|
|
|
38
36
|
dnt: false
|
|
39
37
|
};
|
|
40
38
|
|
|
39
|
+
// src/lib/apply-attributes.ts
|
|
40
|
+
function applyAttributes(attributes, extras) {
|
|
41
|
+
const merged = { ...DEFAULT_ATTRIBUTES, ...extras, ...attributes };
|
|
42
|
+
if (!merged.dir) {
|
|
43
|
+
merged.dir = "auto";
|
|
44
|
+
}
|
|
45
|
+
return merged;
|
|
46
|
+
}
|
|
47
|
+
|
|
41
48
|
// src/classes/MsgMessage/MsgMessage.ts
|
|
42
49
|
var MsgMessage = class _MsgMessage {
|
|
43
50
|
_key;
|
|
@@ -51,7 +58,7 @@ var MsgMessage = class _MsgMessage {
|
|
|
51
58
|
constructor(key, value, attributes, notes) {
|
|
52
59
|
this._key = key;
|
|
53
60
|
this._value = value;
|
|
54
|
-
this._attributes = attributes
|
|
61
|
+
this._attributes = applyAttributes(attributes);
|
|
55
62
|
if (notes) {
|
|
56
63
|
notes.forEach((note) => this.setNote(note));
|
|
57
64
|
}
|
|
@@ -163,6 +170,86 @@ var MsgMessage = class _MsgMessage {
|
|
|
163
170
|
}
|
|
164
171
|
};
|
|
165
172
|
|
|
173
|
+
// src/lib/pseudo-localize.ts
|
|
174
|
+
var import_messageformat2 = require("messageformat");
|
|
175
|
+
var import_parser = require("@messageformat/parser");
|
|
176
|
+
var import_pseudo_localization = require("pseudo-localization");
|
|
177
|
+
function pseudoLocalizeMF2(source, options) {
|
|
178
|
+
const msg = (0, import_messageformat2.parseMessage)(source);
|
|
179
|
+
(0, import_messageformat2.visit)(msg, {
|
|
180
|
+
pattern: (pattern) => {
|
|
181
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
182
|
+
const part = pattern[i];
|
|
183
|
+
if (typeof part === "string") {
|
|
184
|
+
pattern[i] = (0, import_pseudo_localization.localize)(part, options);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
return (0, import_messageformat2.stringifyMessage)(msg);
|
|
190
|
+
}
|
|
191
|
+
function collectMf1ContentTokens(tokens, into = [], inFunctionParam = false) {
|
|
192
|
+
for (const token of tokens) {
|
|
193
|
+
switch (token.type) {
|
|
194
|
+
case "content":
|
|
195
|
+
if (!inFunctionParam && token.value.length > 0) {
|
|
196
|
+
into.push({
|
|
197
|
+
offset: token.ctx.offset,
|
|
198
|
+
length: token.value.length,
|
|
199
|
+
value: token.value
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
break;
|
|
203
|
+
case "plural":
|
|
204
|
+
case "select":
|
|
205
|
+
case "selectordinal":
|
|
206
|
+
for (const selectCase of token.cases) {
|
|
207
|
+
collectMf1ContentTokens(selectCase.tokens, into, inFunctionParam);
|
|
208
|
+
}
|
|
209
|
+
break;
|
|
210
|
+
case "function":
|
|
211
|
+
if (token.param) {
|
|
212
|
+
collectMf1ContentTokens(token.param, into, true);
|
|
213
|
+
}
|
|
214
|
+
break;
|
|
215
|
+
case "argument":
|
|
216
|
+
case "octothorpe":
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return into;
|
|
221
|
+
}
|
|
222
|
+
function pseudoLocalizeMF1(source, options) {
|
|
223
|
+
const spans = collectMf1ContentTokens((0, import_parser.parse)(source));
|
|
224
|
+
if (spans.length === 0) {
|
|
225
|
+
return source;
|
|
226
|
+
}
|
|
227
|
+
spans.sort((a, b) => a.offset - b.offset);
|
|
228
|
+
let result = "";
|
|
229
|
+
let cursor = 0;
|
|
230
|
+
for (const span of spans) {
|
|
231
|
+
result += source.slice(cursor, span.offset);
|
|
232
|
+
result += (0, import_pseudo_localization.localize)(span.value, options);
|
|
233
|
+
cursor = span.offset + span.length;
|
|
234
|
+
}
|
|
235
|
+
result += source.slice(cursor);
|
|
236
|
+
return result;
|
|
237
|
+
}
|
|
238
|
+
function pseudoLocalizeNone(source, options) {
|
|
239
|
+
return (0, import_pseudo_localization.localize)(source, options);
|
|
240
|
+
}
|
|
241
|
+
function pseudoLocalize(source, format, options) {
|
|
242
|
+
switch (format) {
|
|
243
|
+
case "MF1":
|
|
244
|
+
return pseudoLocalizeMF1(source, options);
|
|
245
|
+
case "NONE":
|
|
246
|
+
return pseudoLocalizeNone(source, options);
|
|
247
|
+
case "MF2":
|
|
248
|
+
default:
|
|
249
|
+
return pseudoLocalizeMF2(source, options);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
166
253
|
// src/classes/MsgResource/MsgResource.ts
|
|
167
254
|
var MsgResource = class _MsgResource extends Map {
|
|
168
255
|
_attributes = {};
|
|
@@ -194,7 +281,7 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
194
281
|
constructor(title, attributes, project, notes) {
|
|
195
282
|
super();
|
|
196
283
|
this._title = title;
|
|
197
|
-
this._attributes = {
|
|
284
|
+
this._attributes = applyAttributes(attributes, { format: project.format });
|
|
198
285
|
this._project = project;
|
|
199
286
|
if (notes) {
|
|
200
287
|
notes.forEach((note) => this.setNote(note));
|
|
@@ -214,30 +301,13 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
214
301
|
resolveFormat(attributes) {
|
|
215
302
|
return attributes.format ?? MSG_DEFAULT_FORMAT;
|
|
216
303
|
}
|
|
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
304
|
/** Locale and formatting metadata for this resource. */
|
|
235
305
|
get attributes() {
|
|
236
306
|
return this._attributes;
|
|
237
307
|
}
|
|
238
308
|
/** Replaces the resource's locale and formatting metadata. */
|
|
239
309
|
set attributes(attributes) {
|
|
240
|
-
this._attributes = attributes;
|
|
310
|
+
this._attributes = applyAttributes(attributes);
|
|
241
311
|
}
|
|
242
312
|
/** Notes attached to this resource for translators and tooling. */
|
|
243
313
|
get notes() {
|
|
@@ -343,7 +413,11 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
343
413
|
* Loads and applies translations for a locale via the project loader.
|
|
344
414
|
*
|
|
345
415
|
* When `lang` is the project's pseudo-locale, returns a pseudo-localized
|
|
346
|
-
* copy without calling the loader.
|
|
416
|
+
* copy without calling the loader. Each message is transformed according to
|
|
417
|
+
* its resolved `format` (`MF1`, `MF2`, or `NONE`): only literal text is
|
|
418
|
+
* accented; placeholders and message-format syntax are preserved.
|
|
419
|
+
*
|
|
420
|
+
* When `lang` is omitted, returns a clone of the current resource.
|
|
347
421
|
*
|
|
348
422
|
* @throws {Error} When the locale is unsupported or its language chain is empty.
|
|
349
423
|
*/
|
|
@@ -354,19 +428,17 @@ var MsgResource = class _MsgResource extends Map {
|
|
|
354
428
|
const project = this._project;
|
|
355
429
|
const pseudoLocale = project.locales.pseudoLocale;
|
|
356
430
|
if (lang === pseudoLocale) {
|
|
431
|
+
const messages = [...this.values()].map((msg) => ({
|
|
432
|
+
key: msg.key,
|
|
433
|
+
value: pseudoLocalize(msg.value, this.resolveFormat(msg.attributes)),
|
|
434
|
+
attributes: { ...msg.attributes, lang: pseudoLocale }
|
|
435
|
+
}));
|
|
357
436
|
const pseudolocalizedData = {
|
|
358
437
|
title: this.title,
|
|
359
438
|
attributes: { ...this.attributes, lang: pseudoLocale },
|
|
360
439
|
notes: this.notes.length > 0 ? this.notes : void 0,
|
|
361
|
-
messages
|
|
440
|
+
messages
|
|
362
441
|
};
|
|
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
442
|
return this.translate(pseudolocalizedData);
|
|
371
443
|
}
|
|
372
444
|
const languageChain = project.getTargetLocale(lang);
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MsgResource
|
|
3
|
-
} from "../../chunk-
|
|
4
|
-
import "../../chunk-
|
|
3
|
+
} from "../../chunk-VDOVMTPF.mjs";
|
|
4
|
+
import "../../chunk-GHDKBE4P.mjs";
|
|
5
|
+
import "../../chunk-RMZ42XIY.mjs";
|
|
6
|
+
import "../../chunk-QU3HGZUN.mjs";
|
|
5
7
|
import "../../chunk-XQVALCKX.mjs";
|
|
6
8
|
export {
|
|
7
9
|
MsgResource
|