@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/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
|
+
});
|