@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/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.1",
|
|
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
|