@messagevisor/module-icu 0.0.1 → 0.2.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/CHANGELOG.md +24 -0
- package/LICENSE +21 -0
- package/README.md +7 -0
- package/jest.config.js +11 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +51 -0
- package/lib/index.js.map +1 -0
- package/package.json +43 -13
- package/src/index.spec.ts +258 -0
- package/src/index.ts +72 -0
- package/tsconfig.cjs.json +14 -0
- package/tsconfig.typecheck.json +4 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# [0.2.0](https://github.com/messagevisor/messagevisor/compare/v0.1.0...v0.2.0) (2026-06-02)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @messagevisor/module-icu
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# [0.1.0](https://github.com/messagevisor/messagevisor/compare/v0.0.2...v0.1.0) (2026-05-31)
|
|
15
|
+
|
|
16
|
+
**Note:** Version bump only for package @messagevisor/module-icu
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## 0.0.2 (2026-05-31)
|
|
23
|
+
|
|
24
|
+
**Note:** Version bump only for package @messagevisor/module-icu
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Fahad Heylaal (https://fahad19.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
package/jest.config.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const base = require("../../jest.config");
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
...base,
|
|
5
|
+
rootDir: "../..",
|
|
6
|
+
testRegex: undefined,
|
|
7
|
+
testMatch: ["<rootDir>/packages/module-icu/src/**/*.spec.ts"],
|
|
8
|
+
moduleNameMapper: {
|
|
9
|
+
"^@messagevisor/sdk$": "<rootDir>/packages/sdk/src/index.ts",
|
|
10
|
+
},
|
|
11
|
+
};
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createICUModule = createICUModule;
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
8
|
+
const intl_messageformat_1 = __importDefault(require("intl-messageformat"));
|
|
9
|
+
function createEmptyRecord() {
|
|
10
|
+
return {};
|
|
11
|
+
}
|
|
12
|
+
function createICUModuleCache() {
|
|
13
|
+
return {
|
|
14
|
+
messageFormat: createEmptyRecord(),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function getCacheKey(payload, ignoreTags) {
|
|
18
|
+
return JSON.stringify({
|
|
19
|
+
locale: payload.locale,
|
|
20
|
+
translation: payload.translation,
|
|
21
|
+
formats: payload.formats,
|
|
22
|
+
ignoreTags,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function getCachedIntlMessageFormat(cache, payload, ignoreTags) {
|
|
26
|
+
const cacheKey = getCacheKey(payload, ignoreTags);
|
|
27
|
+
if (!cache.messageFormat[cacheKey]) {
|
|
28
|
+
cache.messageFormat[cacheKey] = new intl_messageformat_1.default(String(payload.translation), payload.locale, {
|
|
29
|
+
number: payload.formats.number,
|
|
30
|
+
date: payload.formats.date,
|
|
31
|
+
time: payload.formats.time,
|
|
32
|
+
}, {
|
|
33
|
+
ignoreTag: ignoreTags,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return cache.messageFormat[cacheKey];
|
|
37
|
+
}
|
|
38
|
+
function createICUModule(options = {}) {
|
|
39
|
+
const cache = createICUModuleCache();
|
|
40
|
+
const name = options.name || "icu";
|
|
41
|
+
return {
|
|
42
|
+
name,
|
|
43
|
+
format(payload) {
|
|
44
|
+
var _a, _b, _c;
|
|
45
|
+
const moduleOptions = (_a = payload.moduleOptions) === null || _a === void 0 ? void 0 : _a[name];
|
|
46
|
+
const ignoreTags = (_c = (_b = moduleOptions === null || moduleOptions === void 0 ? void 0 : moduleOptions.ignoreTags) !== null && _b !== void 0 ? _b : options.ignoreTags) !== null && _c !== void 0 ? _c : true;
|
|
47
|
+
return getCachedIntlMessageFormat(cache, payload, ignoreTags).format(payload.values);
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;AA0DA,0CAaC;AAvED,sDAAsD;AACtD,4EAAmD;AAanD,SAAS,iBAAiB;IACxB,OAAO,EAAuB,CAAC;AACjC,CAAC;AAED,SAAS,oBAAoB;IAC3B,OAAO;QACL,aAAa,EAAE,iBAAiB,EAAqB;KACtD,CAAC;AACJ,CAAC;AAED,SAAS,WAAW,CAAC,OAAkC,EAAE,UAAmB;IAC1E,OAAO,IAAI,CAAC,SAAS,CAAC;QACpB,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,WAAW,EAAE,OAAO,CAAC,WAAW;QAChC,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,UAAU;KACX,CAAC,CAAC;AACL,CAAC;AAED,SAAS,0BAA0B,CACjC,KAAqB,EACrB,OAAkC,EAClC,UAAmB;IAEnB,MAAM,QAAQ,GAAG,WAAW,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;IAElD,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,CAAC;QACnC,KAAK,CAAC,aAAa,CAAC,QAAQ,CAAC,GAAG,IAAI,4BAAiB,CACnD,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,EAC3B,OAAO,CAAC,MAAM,EACd;YACE,MAAM,EAAE,OAAO,CAAC,OAAO,CAAC,MAAM;YAC9B,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,IAAI;YAC1B,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,IAAI;SACpB,EACR;YACE,SAAS,EAAE,UAAU;SACtB,CACF,CAAC;IACJ,CAAC;IAED,OAAO,KAAK,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;AACvC,CAAC;AAED,SAAgB,eAAe,CAAC,UAA4B,EAAE;IAC5D,MAAM,KAAK,GAAG,oBAAoB,EAAE,CAAC;IACrC,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,KAAK,CAAC;IAEnC,OAAO;QACL,IAAI;QACJ,MAAM,CAAC,OAAkC;;YACvC,MAAM,aAAa,GAAG,MAAA,OAAO,CAAC,aAAa,0CAAG,IAAI,CAAyC,CAAC;YAC5F,MAAM,UAAU,GAAG,MAAA,MAAA,aAAa,aAAb,aAAa,uBAAb,aAAa,CAAE,UAAU,mCAAI,OAAO,CAAC,UAAU,mCAAI,IAAI,CAAC;YAE3E,OAAO,0BAA0B,CAAC,KAAK,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,MAAa,CAAC,CAAC;QAC9F,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,15 +1,45 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
2
|
+
"name": "@messagevisor/module-icu",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "ICU formatting module for Messagevisor",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"module": "lib/index.js",
|
|
7
|
+
"types": "lib/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/index.d.ts",
|
|
11
|
+
"require": "./lib/index.js",
|
|
12
|
+
"import": "./lib/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./package.json": "./package.json"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"transpile": "rimraf lib && tsc --project tsconfig.cjs.json",
|
|
18
|
+
"dist": "echo 'Nothing to dist here'",
|
|
19
|
+
"build": "npm run transpile",
|
|
20
|
+
"test": "jest --config jest.config.js --verbose",
|
|
21
|
+
"typecheck": "tsc --noEmit --skipLibCheck -p tsconfig.typecheck.json"
|
|
22
|
+
},
|
|
23
|
+
"author": {
|
|
24
|
+
"name": "Fahad Heylaal",
|
|
25
|
+
"url": "https://fahad19.com"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://messagevisor.com",
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "https://github.com/messagevisor/messagevisor.git"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public",
|
|
34
|
+
"registry": "https://registry.npmjs.org/"
|
|
35
|
+
},
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/messagevisor/messagevisor/issues"
|
|
38
|
+
},
|
|
39
|
+
"license": "MIT",
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@messagevisor/sdk": "0.2.0",
|
|
42
|
+
"intl-messageformat": "^10.7.16"
|
|
43
|
+
},
|
|
44
|
+
"gitHead": "53cfef1a5ae28724b24226dd3a32ff813b063ce3"
|
|
15
45
|
}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import type { DatafileContent } from "@messagevisor/types";
|
|
2
|
+
import { createMessagevisor } from "@messagevisor/sdk";
|
|
3
|
+
|
|
4
|
+
import { createICUModule } from "./index";
|
|
5
|
+
|
|
6
|
+
const datafile: DatafileContent = {
|
|
7
|
+
schemaVersion: "1",
|
|
8
|
+
messagevisorVersion: "0.0.1",
|
|
9
|
+
revision: "1",
|
|
10
|
+
target: "web",
|
|
11
|
+
locale: "en-US",
|
|
12
|
+
formats: {
|
|
13
|
+
number: {
|
|
14
|
+
money: { style: "currency", currency: "USD", currencyDisplay: "code" },
|
|
15
|
+
moneySymbol: { style: "currency", currency: "USD", currencyDisplay: "symbol" },
|
|
16
|
+
moneyCode: { style: "currency", currency: "USD", currencyDisplay: "code" },
|
|
17
|
+
moneyAccounting: { style: "currency", currency: "USD", currencySign: "accounting" },
|
|
18
|
+
runtimeMoney: { style: "currency", currencyDisplay: "code" },
|
|
19
|
+
decimalFixed: { style: "decimal", minimumFractionDigits: 2, maximumFractionDigits: 2 },
|
|
20
|
+
compactShort: { notation: "compact", compactDisplay: "short" },
|
|
21
|
+
unitDistance: { style: "unit", unit: "kilometer", unitDisplay: "short" },
|
|
22
|
+
scientific: { notation: "scientific" },
|
|
23
|
+
noCurrency: { style: "currency", currencyDisplay: "code" } as any,
|
|
24
|
+
},
|
|
25
|
+
date: {
|
|
26
|
+
numeric: { year: "numeric", month: "2-digit", day: "2-digit" },
|
|
27
|
+
weekday: { weekday: "long", year: "numeric", month: "long", day: "numeric" },
|
|
28
|
+
fullStyle: { dateStyle: "full" },
|
|
29
|
+
},
|
|
30
|
+
time: {
|
|
31
|
+
short: { hour: "numeric", minute: "2-digit" },
|
|
32
|
+
seconds: { hour: "numeric", minute: "2-digit", second: "2-digit" },
|
|
33
|
+
event: { hour: "numeric", minute: "2-digit", timeZone: "UTC" },
|
|
34
|
+
fullStyle: { timeStyle: "full", timeZone: "UTC" },
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
segments: {
|
|
38
|
+
"platform-web": {
|
|
39
|
+
conditions: [{ attribute: "platform", operator: "equals", value: "web" }],
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
messages: {
|
|
43
|
+
greeting: {
|
|
44
|
+
overrides: [
|
|
45
|
+
{
|
|
46
|
+
key: "platform-web",
|
|
47
|
+
segments: "platform-web",
|
|
48
|
+
translation: "Hello web {name}",
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
namedTotal: {},
|
|
53
|
+
skeletonTotal: {},
|
|
54
|
+
fallbackTotal: {},
|
|
55
|
+
symbolTotal: {},
|
|
56
|
+
codeTotal: {},
|
|
57
|
+
accountingTotal: {},
|
|
58
|
+
runtimeTotal: {},
|
|
59
|
+
compactTotal: {},
|
|
60
|
+
compactAudience: {},
|
|
61
|
+
scientificTotal: {},
|
|
62
|
+
distance: {},
|
|
63
|
+
eventTime: {},
|
|
64
|
+
fullDate: {},
|
|
65
|
+
fullTime: {},
|
|
66
|
+
dateFormats: {},
|
|
67
|
+
timeFormats: {},
|
|
68
|
+
richTerms: {},
|
|
69
|
+
richPromo: {
|
|
70
|
+
overrides: [
|
|
71
|
+
{
|
|
72
|
+
key: "promo-web",
|
|
73
|
+
segments: "platform-web",
|
|
74
|
+
translation:
|
|
75
|
+
"Web <strong>{product}</strong> costs <price>{amount, number, money}</price> today.",
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
translations: {
|
|
81
|
+
greeting: "Hello {name}",
|
|
82
|
+
namedTotal: "Total: {amount, number, money}",
|
|
83
|
+
skeletonTotal: "Total: {amount, number, ::currency/GBP}",
|
|
84
|
+
fallbackTotal: "Total: {amount, number, noCurrency}",
|
|
85
|
+
symbolTotal: "Total: {amount, number, moneySymbol}",
|
|
86
|
+
codeTotal: "Total: {amount, number, moneyCode}",
|
|
87
|
+
accountingTotal: "Total: {amount, number, moneyAccounting}",
|
|
88
|
+
runtimeTotal: "Total: {amount, number, runtimeMoney}",
|
|
89
|
+
compactTotal: "Total: {currency}{amount, number, decimalFixed}",
|
|
90
|
+
compactAudience: "Audience: {count, number, compactShort}",
|
|
91
|
+
scientificTotal: "Scientific: {amount, number, scientific}",
|
|
92
|
+
distance: "Distance: {distance, number, unitDistance}",
|
|
93
|
+
eventTime: "Starts at {startsAt, time, event}",
|
|
94
|
+
fullDate: "Full date: {startsAt, date, fullStyle}",
|
|
95
|
+
fullTime: "Full time: {startsAt, time, fullStyle}",
|
|
96
|
+
dateFormats: "Numeric: {startsAt, date, numeric}; weekday: {startsAt, date, weekday}",
|
|
97
|
+
timeFormats: "Short: {startsAt, time, short}; seconds: {startsAt, time, seconds}",
|
|
98
|
+
richTerms: "Read our <link>terms</link> for <strong>{product}</strong>.",
|
|
99
|
+
richPromo: "Default <strong>{product}</strong> costs <price>{amount, number, money}</price>.",
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
describe("@messagevisor/module-icu", function () {
|
|
104
|
+
it("formats ICU interpolations when installed", function () {
|
|
105
|
+
const m = createMessagevisor({
|
|
106
|
+
datafile,
|
|
107
|
+
modules: [createICUModule()],
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
expect(m.translate("greeting", { name: "Ada" })).toEqual("Hello Ada");
|
|
111
|
+
expect(m.formatMessage("Hi {name}", { name: "Lin" })).toEqual("Hi Lin");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("formats ICU placeholders in per-call default translations for missing keys", function () {
|
|
115
|
+
const m = createMessagevisor({
|
|
116
|
+
datafile,
|
|
117
|
+
modules: [createICUModule()],
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
expect(
|
|
121
|
+
m.translate("missing.icu.fallback", { name: "Ada" }, { defaultTranslation: "Hi, {name}" }),
|
|
122
|
+
).toEqual("Hi, Ada");
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("supports rich tags and rich output arrays", function () {
|
|
126
|
+
const m = createMessagevisor({
|
|
127
|
+
datafile,
|
|
128
|
+
modules: [createICUModule({ ignoreTags: false })],
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
expect(
|
|
132
|
+
m.translate("richTerms", {
|
|
133
|
+
product: "Messagevisor",
|
|
134
|
+
link: (chunks: string[]) => `[${chunks.join("")}]`,
|
|
135
|
+
strong: (chunks: string[]) => chunks.join("").toUpperCase(),
|
|
136
|
+
}),
|
|
137
|
+
).toEqual("Read our [terms] for MESSAGEVISOR.");
|
|
138
|
+
|
|
139
|
+
expect(
|
|
140
|
+
m.formatMessage("Read <link>terms</link>.", {
|
|
141
|
+
link: (chunks) => ({ type: "link", children: chunks }),
|
|
142
|
+
}),
|
|
143
|
+
).toEqual(["Read ", { type: "link", children: ["terms"] }, "."]);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("supports per-call ignoreTags overrides", function () {
|
|
147
|
+
const richEnabled = createMessagevisor({
|
|
148
|
+
datafile,
|
|
149
|
+
modules: [createICUModule({ ignoreTags: false })],
|
|
150
|
+
});
|
|
151
|
+
const richDisabled = createMessagevisor({
|
|
152
|
+
datafile,
|
|
153
|
+
modules: [createICUModule()],
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
expect(
|
|
157
|
+
richEnabled.formatMessage(
|
|
158
|
+
"Read <link>terms</link>.",
|
|
159
|
+
{
|
|
160
|
+
link: (chunks) => `[${chunks.join("")}]`,
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
moduleOptions: {
|
|
164
|
+
icu: {
|
|
165
|
+
ignoreTags: true,
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
),
|
|
170
|
+
).toEqual("Read <link>terms</link>.");
|
|
171
|
+
|
|
172
|
+
expect(
|
|
173
|
+
richDisabled.formatMessage(
|
|
174
|
+
"Read <link>terms</link>.",
|
|
175
|
+
{
|
|
176
|
+
link: (chunks) => `[${chunks.join("")}]`,
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
moduleOptions: {
|
|
180
|
+
icu: {
|
|
181
|
+
ignoreTags: false,
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
),
|
|
186
|
+
).toEqual("Read [terms].");
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("honors currency and time zone precedence through resolved formats", function () {
|
|
190
|
+
const startsAt = new Date("2025-01-01T12:00:00Z");
|
|
191
|
+
const m = createMessagevisor({
|
|
192
|
+
datafile,
|
|
193
|
+
currency: "CHF",
|
|
194
|
+
timeZone: "Asia/Tokyo",
|
|
195
|
+
modules: [createICUModule()],
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
expect(m.translate("namedTotal", { amount: 12 }, { currency: "EUR" })).toContain("EUR");
|
|
199
|
+
expect(m.translate("namedTotal", { amount: 12 })).toContain("USD");
|
|
200
|
+
expect(m.translate("fallbackTotal", { amount: 12 })).toContain("CHF");
|
|
201
|
+
expect(m.translate("runtimeTotal", { amount: 12 })).toContain("CHF");
|
|
202
|
+
expect(m.translate("eventTime", { startsAt }, { timeZone: "America/New_York" })).toContain(
|
|
203
|
+
"7:00",
|
|
204
|
+
);
|
|
205
|
+
expect(m.translate("eventTime", { startsAt })).toContain("12:00");
|
|
206
|
+
expect(m.translate("skeletonTotal", { amount: 12 })).toContain("£");
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("supports override-selected translations and named format variants", function () {
|
|
210
|
+
const m = createMessagevisor({
|
|
211
|
+
datafile,
|
|
212
|
+
context: { platform: "web" },
|
|
213
|
+
currency: "EUR",
|
|
214
|
+
modules: [createICUModule({ ignoreTags: false })],
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
expect(m.translate("greeting", { name: "Ada" })).toEqual("Hello web Ada");
|
|
218
|
+
expect(m.translate("symbolTotal", { amount: 12 })).toEqual("Total: $12.00");
|
|
219
|
+
expect(m.translate("codeTotal", { amount: 12 })).toEqual("Total: USD 12.00");
|
|
220
|
+
expect(m.translate("accountingTotal", { amount: -12 })).toEqual("Total: ($12.00)");
|
|
221
|
+
expect(m.translate("compactAudience", { count: 1200 })).toContain("1.2K");
|
|
222
|
+
expect(m.translate("scientificTotal", { amount: 1200 })).toContain("1.2E3");
|
|
223
|
+
expect(m.translate("distance", { distance: 5 })).toMatch(/5\s?km/);
|
|
224
|
+
expect(
|
|
225
|
+
m.translate("richPromo", {
|
|
226
|
+
product: "Messagevisor",
|
|
227
|
+
amount: 12,
|
|
228
|
+
strong: (chunks: string[]) => `**${chunks.join("")}**`,
|
|
229
|
+
price: (chunks: string[]) => `<${chunks.join("")}>`,
|
|
230
|
+
}),
|
|
231
|
+
).toEqual("Web **Messagevisor** costs <USD 12.00> today.");
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("supports Intl date/time style presets in ICU messages", function () {
|
|
235
|
+
const m = createMessagevisor({
|
|
236
|
+
datafile,
|
|
237
|
+
timeZone: "UTC",
|
|
238
|
+
modules: [createICUModule()],
|
|
239
|
+
});
|
|
240
|
+
const startsAt = new Date("2025-01-01T12:00:00Z");
|
|
241
|
+
|
|
242
|
+
expect(m.translate("fullDate", { startsAt })).toContain("Wednesday, January 1, 2025");
|
|
243
|
+
expect(m.translate("fullTime", { startsAt })).toContain("12:00:00 PM");
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("can be removed by its default name", function () {
|
|
247
|
+
const m = createMessagevisor({
|
|
248
|
+
datafile,
|
|
249
|
+
modules: [createICUModule()],
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
expect(m.translate("greeting", { name: "Ada" })).toEqual("Hello Ada");
|
|
253
|
+
|
|
254
|
+
m.removeModule("icu");
|
|
255
|
+
|
|
256
|
+
expect(m.translate("greeting", { name: "Ada" })).toEqual("Hello {name}");
|
|
257
|
+
});
|
|
258
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import IntlMessageFormat from "intl-messageformat";
|
|
3
|
+
|
|
4
|
+
import type { MessagevisorFormatPayload, MessagevisorModule } from "@messagevisor/sdk";
|
|
5
|
+
|
|
6
|
+
export interface ICUModuleOptions {
|
|
7
|
+
name?: string;
|
|
8
|
+
ignoreTags?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface ICUModuleCache {
|
|
12
|
+
messageFormat: Record<string, IntlMessageFormat>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function createEmptyRecord<T>() {
|
|
16
|
+
return {} as Record<string, T>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function createICUModuleCache(): ICUModuleCache {
|
|
20
|
+
return {
|
|
21
|
+
messageFormat: createEmptyRecord<IntlMessageFormat>(),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function getCacheKey(payload: MessagevisorFormatPayload, ignoreTags: boolean) {
|
|
26
|
+
return JSON.stringify({
|
|
27
|
+
locale: payload.locale,
|
|
28
|
+
translation: payload.translation,
|
|
29
|
+
formats: payload.formats,
|
|
30
|
+
ignoreTags,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function getCachedIntlMessageFormat<T>(
|
|
35
|
+
cache: ICUModuleCache,
|
|
36
|
+
payload: MessagevisorFormatPayload,
|
|
37
|
+
ignoreTags: boolean,
|
|
38
|
+
) {
|
|
39
|
+
const cacheKey = getCacheKey(payload, ignoreTags);
|
|
40
|
+
|
|
41
|
+
if (!cache.messageFormat[cacheKey]) {
|
|
42
|
+
cache.messageFormat[cacheKey] = new IntlMessageFormat(
|
|
43
|
+
String(payload.translation),
|
|
44
|
+
payload.locale,
|
|
45
|
+
{
|
|
46
|
+
number: payload.formats.number,
|
|
47
|
+
date: payload.formats.date,
|
|
48
|
+
time: payload.formats.time,
|
|
49
|
+
} as any,
|
|
50
|
+
{
|
|
51
|
+
ignoreTag: ignoreTags,
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return cache.messageFormat[cacheKey];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function createICUModule(options: ICUModuleOptions = {}): MessagevisorModule {
|
|
60
|
+
const cache = createICUModuleCache();
|
|
61
|
+
const name = options.name || "icu";
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
name,
|
|
65
|
+
format(payload: MessagevisorFormatPayload) {
|
|
66
|
+
const moduleOptions = payload.moduleOptions?.[name] as { ignoreTags?: boolean } | undefined;
|
|
67
|
+
const ignoreTags = moduleOptions?.ignoreTags ?? options.ignoreTags ?? true;
|
|
68
|
+
|
|
69
|
+
return getCachedIntlMessageFormat(cache, payload, ignoreTags).format(payload.values as any);
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.cjs.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "./lib",
|
|
5
|
+
"rootDir": "./src",
|
|
6
|
+
"moduleResolution": "node",
|
|
7
|
+
"esModuleInterop": true,
|
|
8
|
+
"target": "es2018",
|
|
9
|
+
"lib": ["es2021", "es2021.intl", "dom"],
|
|
10
|
+
"skipLibCheck": true
|
|
11
|
+
},
|
|
12
|
+
"include": ["./src/**/*.ts"],
|
|
13
|
+
"exclude": ["./src/**/*.spec.ts"]
|
|
14
|
+
}
|