intl-template 1.0.0-alpha.0 → 1.0.0-alpha.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/dist/intl.d.ts +3 -5
- package/dist/intl.js +25 -3
- package/dist/intl.js.map +1 -1
- package/package.json +1 -1
- package/src/intl.js +14 -2
- package/src/intl.test.js +22 -0
package/dist/intl.d.ts
CHANGED
|
@@ -16,11 +16,8 @@ export class Runes extends Array<any> {
|
|
|
16
16
|
* Represents a Translation object that handles string translation based on locale and templates.
|
|
17
17
|
*/
|
|
18
18
|
export class Translation {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
* @type {Proxy}
|
|
22
|
-
*/
|
|
23
|
-
templates: ProxyConstructor;
|
|
19
|
+
set templates(value: ProxyConstructor);
|
|
20
|
+
get templates(): ProxyConstructor;
|
|
24
21
|
/**
|
|
25
22
|
* Translates a string based on the provided locale and strings.
|
|
26
23
|
*
|
|
@@ -31,6 +28,7 @@ export class Translation {
|
|
|
31
28
|
* @throws {Error} - If the length of the template parts does not match the length of the template.
|
|
32
29
|
*/
|
|
33
30
|
translate: (locale: string, strings: string | string[], ...parts: any[]) => Runes;
|
|
31
|
+
#private;
|
|
34
32
|
}
|
|
35
33
|
export default translation;
|
|
36
34
|
export const l10n: (strings: string | string[], ...parts: any[]) => Runes;
|
package/dist/intl.js
CHANGED
|
@@ -13,7 +13,13 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
13
13
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
14
|
};
|
|
15
15
|
})();
|
|
16
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
17
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
18
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
19
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
20
|
+
};
|
|
16
21
|
var _a;
|
|
22
|
+
var _Translation_templates;
|
|
17
23
|
var Runes = /** @class */ (function (_super) {
|
|
18
24
|
__extends(Runes, _super);
|
|
19
25
|
function Runes() {
|
|
@@ -60,7 +66,7 @@ var Translation = /** @class */ (function () {
|
|
|
60
66
|
* Templates object that stores the translation templates for each locale.
|
|
61
67
|
* @type {Proxy}
|
|
62
68
|
*/
|
|
63
|
-
this
|
|
69
|
+
_Translation_templates.set(this, new Proxy({}, {
|
|
64
70
|
get: function (templates, locale) {
|
|
65
71
|
return new Proxy(templates[locale] || {}, {
|
|
66
72
|
set: function (region, key, value) {
|
|
@@ -80,7 +86,7 @@ var Translation = /** @class */ (function () {
|
|
|
80
86
|
}, {});
|
|
81
87
|
return true;
|
|
82
88
|
}
|
|
83
|
-
});
|
|
89
|
+
}));
|
|
84
90
|
/**
|
|
85
91
|
* Translates a string based on the provided locale and strings.
|
|
86
92
|
*
|
|
@@ -100,7 +106,7 @@ var Translation = /** @class */ (function () {
|
|
|
100
106
|
strings = strings.split("{}");
|
|
101
107
|
}
|
|
102
108
|
var key = strings.join("{}");
|
|
103
|
-
var translation = (_a = _this
|
|
109
|
+
var translation = (_a = __classPrivateFieldGet(_this, _Translation_templates, "f")) === null || _a === void 0 ? void 0 : _a[locale];
|
|
104
110
|
var _d = (translation === null || translation === void 0 ? void 0 : translation[key]) || {}, template = _d.template, order = _d.order;
|
|
105
111
|
if (!template) {
|
|
106
112
|
if (((_c = (_b = import.meta) === null || _b === void 0 ? void 0 : _b.env) === null || _c === void 0 ? void 0 : _c.MODE) === "development") {
|
|
@@ -129,9 +135,25 @@ var Translation = /** @class */ (function () {
|
|
|
129
135
|
return runes;
|
|
130
136
|
};
|
|
131
137
|
}
|
|
138
|
+
Object.defineProperty(Translation.prototype, "templates", {
|
|
139
|
+
get: function () {
|
|
140
|
+
return __classPrivateFieldGet(this, _Translation_templates, "f");
|
|
141
|
+
},
|
|
142
|
+
set: function (value) {
|
|
143
|
+
var _this = this;
|
|
144
|
+
Object.entries(value).forEach(function (_a) {
|
|
145
|
+
var locale = _a[0], regionTemplates = _a[1];
|
|
146
|
+
__classPrivateFieldGet(_this, _Translation_templates, "f")[locale] = regionTemplates;
|
|
147
|
+
});
|
|
148
|
+
return true;
|
|
149
|
+
},
|
|
150
|
+
enumerable: false,
|
|
151
|
+
configurable: true
|
|
152
|
+
});
|
|
132
153
|
return Translation;
|
|
133
154
|
}());
|
|
134
155
|
export { Translation };
|
|
156
|
+
_Translation_templates = new WeakMap();
|
|
135
157
|
var translation = new Translation();
|
|
136
158
|
export default translation;
|
|
137
159
|
export var l10n = translation.translate.bind(null, (_a = globalThis === null || globalThis === void 0 ? void 0 : globalThis.navigator) === null || _a === void 0 ? void 0 : _a.language);
|
package/dist/intl.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"intl.js","sourceRoot":"","sources":["../src/intl.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"intl.js","sourceRoot":"","sources":["../src/intl.js"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;IAA2B,yBAAK;IAAhC;;IAIA,CAAC;IAHA,wBAAQ,GAAR;QACC,OAAO,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IACrB,CAAC;IACF,YAAC;AAAD,CAAC,AAJD,CAA2B,KAAK,GAI/B;;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,cAAc;IAC3C,IAAM,KAAK,GAAG,EAAE,CAAC;IACjB,IAAM,QAAQ,GAAG,EAAE,CAAC;IACpB,IAAM,KAAK,GAAG,cAAc,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;IAC7C,KAAK,CAAC,OAAO,CAAC,UAAC,IAAI;QAClB,IAAI,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC;YAC3B,IAAI,IAAI,KAAK,IAAI,EAAE,CA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,UAAU,uBAAV,UAAU,CAAE,SAAS,0CAAE,QAAQ,CAAC,CAAC"}
|
package/package.json
CHANGED
package/src/intl.js
CHANGED
|
@@ -37,7 +37,7 @@ export class Translation {
|
|
|
37
37
|
* Templates object that stores the translation templates for each locale.
|
|
38
38
|
* @type {Proxy}
|
|
39
39
|
*/
|
|
40
|
-
templates = new Proxy({}, {
|
|
40
|
+
#templates = new Proxy({}, {
|
|
41
41
|
get(templates, locale) {
|
|
42
42
|
return new Proxy(templates[locale] || {}, {
|
|
43
43
|
set(region, key, value) {
|
|
@@ -62,6 +62,18 @@ export class Translation {
|
|
|
62
62
|
}
|
|
63
63
|
})
|
|
64
64
|
|
|
65
|
+
get templates() {
|
|
66
|
+
return this.#templates
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
set templates(value) {
|
|
70
|
+
Object.entries(value).forEach(([locale, regionTemplates]) => {
|
|
71
|
+
this.#templates[locale] = regionTemplates
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
return true
|
|
75
|
+
}
|
|
76
|
+
|
|
65
77
|
/**
|
|
66
78
|
* Translates a string based on the provided locale and strings.
|
|
67
79
|
*
|
|
@@ -77,7 +89,7 @@ export class Translation {
|
|
|
77
89
|
}
|
|
78
90
|
|
|
79
91
|
const key = strings.join("{}")
|
|
80
|
-
const translation = this
|
|
92
|
+
const translation = this.#templates?.[locale]
|
|
81
93
|
let { template, order } = translation?.[key] || {}
|
|
82
94
|
if (!template) {
|
|
83
95
|
if (import.meta?.env?.MODE === "development") {
|
package/src/intl.test.js
CHANGED
|
@@ -3,6 +3,28 @@ import test from "node:test"
|
|
|
3
3
|
import { Translation, Runes } from "./intl.js"
|
|
4
4
|
|
|
5
5
|
test("template", async (t) => {
|
|
6
|
+
await t.test("multiple locale template parse", () => {
|
|
7
|
+
const translation = new Translation()
|
|
8
|
+
translation.templates = {
|
|
9
|
+
"es-ES": {
|
|
10
|
+
"{}matched{}": "{}{}uno",
|
|
11
|
+
},
|
|
12
|
+
"zh-CN": {
|
|
13
|
+
"{}matched{}": "{}{}丁戊卯",
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
assert.deepEqual(
|
|
18
|
+
translation.templates["zh-CN"],
|
|
19
|
+
{
|
|
20
|
+
"{}matched{}": {
|
|
21
|
+
template: ["", "", "丁戊卯"],
|
|
22
|
+
order: [0, 1]
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
)
|
|
26
|
+
})
|
|
27
|
+
|
|
6
28
|
await t.test("template slot parse", () => {
|
|
7
29
|
const translation = new Translation()
|
|
8
30
|
const locale = "zh-CN"
|