@lingui/message-utils 4.0.0-next.4
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 +22 -0
- package/dist/compileMessage.cjs +53 -0
- package/dist/compileMessage.d.ts +9 -0
- package/dist/compileMessage.mjs +51 -0
- package/dist/generateMessageId.cjs +10 -0
- package/dist/generateMessageId.d.ts +3 -0
- package/dist/generateMessageId.mjs +8 -0
- package/package.json +44 -0
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[![License][badge-license]][license]
|
|
2
|
+
[![Version][badge-version]][package]
|
|
3
|
+
[![Downloads][badge-downloads]][package]
|
|
4
|
+
|
|
5
|
+
# @lingui/message-utils
|
|
6
|
+
|
|
7
|
+
> Internal package. You probably don't need to use it directly.
|
|
8
|
+
|
|
9
|
+
`@lingui/message-utils` is part of [LinguiJS][linguijs]. See the [documentation][documentation]
|
|
10
|
+
for all information, tutorials and examples.
|
|
11
|
+
|
|
12
|
+
## License
|
|
13
|
+
|
|
14
|
+
[MIT][license]
|
|
15
|
+
|
|
16
|
+
[license]: https://github.com/lingui/js-lingui/blob/main/LICENSE
|
|
17
|
+
[linguijs]: https://github.com/lingui/js-lingui
|
|
18
|
+
[documentation]: https://lingui.dev
|
|
19
|
+
[package]: https://www.npmjs.com/package/@lingui/message-utils
|
|
20
|
+
[badge-downloads]: https://img.shields.io/npm/dw/@lingui/message-utils.svg
|
|
21
|
+
[badge-version]: https://img.shields.io/npm/v/@lingui/message-utils.svg
|
|
22
|
+
[badge-license]: https://img.shields.io/npm/l/@lingui/message-utils.svg
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const parser = require('@messageformat/parser');
|
|
4
|
+
|
|
5
|
+
function processTokens(tokens, mapText) {
|
|
6
|
+
if (!tokens.filter((token) => token.type !== "content").length) {
|
|
7
|
+
return tokens.map((token) => mapText(token.value)).join("");
|
|
8
|
+
}
|
|
9
|
+
return tokens.map((token) => {
|
|
10
|
+
if (token.type === "content") {
|
|
11
|
+
return mapText(token.value);
|
|
12
|
+
} else if (token.type === "octothorpe") {
|
|
13
|
+
return "#";
|
|
14
|
+
} else if (token.type === "argument") {
|
|
15
|
+
return [token.arg];
|
|
16
|
+
} else if (token.type === "function") {
|
|
17
|
+
const _param = token?.param?.[0];
|
|
18
|
+
if (_param) {
|
|
19
|
+
return [token.arg, token.key, _param.value.trim()];
|
|
20
|
+
} else {
|
|
21
|
+
return [token.arg, token.key];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const offset = token.pluralOffset;
|
|
25
|
+
const formatProps = {};
|
|
26
|
+
token.cases.forEach((item) => {
|
|
27
|
+
formatProps[item.key.replace(/^=(.)+/, "$1")] = processTokens(
|
|
28
|
+
item.tokens,
|
|
29
|
+
mapText
|
|
30
|
+
);
|
|
31
|
+
});
|
|
32
|
+
return [
|
|
33
|
+
token.arg,
|
|
34
|
+
token.type,
|
|
35
|
+
{
|
|
36
|
+
offset,
|
|
37
|
+
...formatProps
|
|
38
|
+
}
|
|
39
|
+
];
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function compileMessage(message, mapText = (v) => v) {
|
|
43
|
+
try {
|
|
44
|
+
return processTokens(parser.parse(message), mapText);
|
|
45
|
+
} catch (e) {
|
|
46
|
+
console.error(`${e.message}
|
|
47
|
+
|
|
48
|
+
Message: ${message}`);
|
|
49
|
+
return message;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
exports.compileMessage = compileMessage;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type CompiledIcuChoices = Record<string, CompiledMessage> & {
|
|
2
|
+
offset: number;
|
|
3
|
+
};
|
|
4
|
+
type CompiledMessageToken = string | [name: string, type?: string, format?: null | string | CompiledIcuChoices];
|
|
5
|
+
type CompiledMessage = string | CompiledMessageToken[];
|
|
6
|
+
type MapTextFn = (value: string) => string;
|
|
7
|
+
declare function compileMessage(message: string, mapText?: MapTextFn): CompiledMessage;
|
|
8
|
+
|
|
9
|
+
export { CompiledIcuChoices, CompiledMessage, CompiledMessageToken, compileMessage };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { parse } from '@messageformat/parser';
|
|
2
|
+
|
|
3
|
+
function processTokens(tokens, mapText) {
|
|
4
|
+
if (!tokens.filter((token) => token.type !== "content").length) {
|
|
5
|
+
return tokens.map((token) => mapText(token.value)).join("");
|
|
6
|
+
}
|
|
7
|
+
return tokens.map((token) => {
|
|
8
|
+
if (token.type === "content") {
|
|
9
|
+
return mapText(token.value);
|
|
10
|
+
} else if (token.type === "octothorpe") {
|
|
11
|
+
return "#";
|
|
12
|
+
} else if (token.type === "argument") {
|
|
13
|
+
return [token.arg];
|
|
14
|
+
} else if (token.type === "function") {
|
|
15
|
+
const _param = token?.param?.[0];
|
|
16
|
+
if (_param) {
|
|
17
|
+
return [token.arg, token.key, _param.value.trim()];
|
|
18
|
+
} else {
|
|
19
|
+
return [token.arg, token.key];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const offset = token.pluralOffset;
|
|
23
|
+
const formatProps = {};
|
|
24
|
+
token.cases.forEach((item) => {
|
|
25
|
+
formatProps[item.key.replace(/^=(.)+/, "$1")] = processTokens(
|
|
26
|
+
item.tokens,
|
|
27
|
+
mapText
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
return [
|
|
31
|
+
token.arg,
|
|
32
|
+
token.type,
|
|
33
|
+
{
|
|
34
|
+
offset,
|
|
35
|
+
...formatProps
|
|
36
|
+
}
|
|
37
|
+
];
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function compileMessage(message, mapText = (v) => v) {
|
|
41
|
+
try {
|
|
42
|
+
return processTokens(parse(message), mapText);
|
|
43
|
+
} catch (e) {
|
|
44
|
+
console.error(`${e.message}
|
|
45
|
+
|
|
46
|
+
Message: ${message}`);
|
|
47
|
+
return message;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { compileMessage };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
|
|
5
|
+
const UNIT_SEPARATOR = "";
|
|
6
|
+
function generateMessageId(msg, context = "") {
|
|
7
|
+
return crypto.createHash("sha256").update(msg + UNIT_SEPARATOR + (context || "")).digest("base64").slice(0, 6);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
exports.generateMessageId = generateMessageId;
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lingui/message-utils",
|
|
3
|
+
"version": "4.0.0-next.4",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"exports": {
|
|
8
|
+
"./generateMessageId": {
|
|
9
|
+
"import": "./dist/generateMessageId.mjs",
|
|
10
|
+
"require": "./dist/generateMessageId.cjs",
|
|
11
|
+
"types": "./dist/generateMessageId.d.ts"
|
|
12
|
+
},
|
|
13
|
+
"./compileMessage": {
|
|
14
|
+
"import": "./dist/compileMessage.mjs",
|
|
15
|
+
"require": "./dist/compileMessage.cjs",
|
|
16
|
+
"types": "./dist/compileMessage.d.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "rimraf ./dist && unbuild",
|
|
21
|
+
"stub": "unbuild --stub"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/lingui/js-lingui.git"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/lingui/js-lingui/issues"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=16.0.0"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"README.md",
|
|
35
|
+
"dist/"
|
|
36
|
+
],
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@messageformat/parser": "^5.0.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@lingui/jest-mocks": "workspace:^",
|
|
42
|
+
"unbuild": "^1.1.2"
|
|
43
|
+
}
|
|
44
|
+
}
|