intl-template 1.0.0-alpha.3 → 1.0.0-alpha.5
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 +7 -3
- package/dist/intl.js +58 -93
- package/dist/intl.js.map +1 -1
- package/package.json +1 -1
- package/src/intl.js +19 -4
- package/tsconfig.json +1 -1
package/dist/intl.d.ts
CHANGED
|
@@ -25,6 +25,8 @@ export class Runes extends Array<any> {
|
|
|
25
25
|
* Represents a Translation object that handles string translation based on locale and templates.
|
|
26
26
|
*/
|
|
27
27
|
export class Translation {
|
|
28
|
+
constructor(locale: any);
|
|
29
|
+
defaultLocale: any;
|
|
28
30
|
set templates(value: ProxyConstructor);
|
|
29
31
|
get templates(): ProxyConstructor;
|
|
30
32
|
/**
|
|
@@ -38,16 +40,18 @@ export class Translation {
|
|
|
38
40
|
*/
|
|
39
41
|
translate: (locale: string, strings: TemplateStringsArray | string, ...parts: any[]) => Runes;
|
|
40
42
|
/**
|
|
41
|
-
*
|
|
43
|
+
* Use regex extract template and vars
|
|
42
44
|
* @param {*} locale
|
|
43
45
|
* @param {*} regexString
|
|
44
46
|
* @param {*} matchString
|
|
45
47
|
* @returns {Runes}
|
|
46
48
|
*/
|
|
47
49
|
translateByRegEx: (locale: any, regexString: any, matchString: any) => Runes;
|
|
50
|
+
l10n: (strings: any, ...parts: any[]) => Runes;
|
|
51
|
+
l10nRegExp: (regexString: any, matchString: any) => Runes;
|
|
48
52
|
#private;
|
|
49
53
|
}
|
|
50
54
|
export default translation;
|
|
51
|
-
export
|
|
52
|
-
export
|
|
55
|
+
export function l10n(strings: any, ...parts: any[]): Runes;
|
|
56
|
+
export function l10nRegExp(regexString: any, matchString: any): Runes;
|
|
53
57
|
declare const translation: Translation;
|
package/dist/intl.js
CHANGED
|
@@ -1,55 +1,24 @@
|
|
|
1
|
-
var __extends = (this && this.__extends) || (function () {
|
|
2
|
-
var extendStatics = function (d, b) {
|
|
3
|
-
extendStatics = Object.setPrototypeOf ||
|
|
4
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
5
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
6
|
-
return extendStatics(d, b);
|
|
7
|
-
};
|
|
8
|
-
return function (d, b) {
|
|
9
|
-
if (typeof b !== "function" && b !== null)
|
|
10
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
11
|
-
extendStatics(d, b);
|
|
12
|
-
function __() { this.constructor = d; }
|
|
13
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
|
-
};
|
|
15
|
-
})();
|
|
16
1
|
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
17
2
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
18
3
|
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
4
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
20
5
|
};
|
|
21
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
22
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
23
|
-
if (ar || !(i in from)) {
|
|
24
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
25
|
-
ar[i] = from[i];
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
29
|
-
};
|
|
30
|
-
var _a, _b;
|
|
31
6
|
var _Translation_templates;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
function Runes() {
|
|
35
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
36
|
-
}
|
|
37
|
-
Runes.prototype.toString = function () {
|
|
7
|
+
export class Runes extends Array {
|
|
8
|
+
toString() {
|
|
38
9
|
return this.join("");
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
}(Array));
|
|
42
|
-
export { Runes };
|
|
10
|
+
}
|
|
11
|
+
}
|
|
43
12
|
/**
|
|
44
13
|
* Parses a template string and extracts the template parts and order of slots.
|
|
45
14
|
* @param {string} templateString - The template string to parse.
|
|
46
15
|
* @returns {{ template: string[], order: number[] }}
|
|
47
16
|
*/
|
|
48
17
|
export function parseTemplate(templateString) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
parts.forEach(
|
|
18
|
+
const order = [];
|
|
19
|
+
const template = [];
|
|
20
|
+
const parts = templateString.split(/({\d*})/);
|
|
21
|
+
parts.forEach((part) => {
|
|
53
22
|
if (part.match(/^{\d*}$/)) {
|
|
54
23
|
if (part === '{}') {
|
|
55
24
|
order.push(order.length);
|
|
@@ -63,22 +32,21 @@ export function parseTemplate(templateString) {
|
|
|
63
32
|
template.push(part);
|
|
64
33
|
}
|
|
65
34
|
});
|
|
66
|
-
return { template
|
|
35
|
+
return { template, order };
|
|
67
36
|
}
|
|
68
37
|
/**
|
|
69
38
|
* Represents a Translation object that handles string translation based on locale and templates.
|
|
70
39
|
*/
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
var _this = this;
|
|
40
|
+
export class Translation {
|
|
41
|
+
constructor(locale) {
|
|
74
42
|
/**
|
|
75
43
|
* Templates object that stores the translation templates for each locale.
|
|
76
44
|
* @type {Proxy}
|
|
77
45
|
*/
|
|
78
46
|
_Translation_templates.set(this, new Proxy({}, {
|
|
79
|
-
get
|
|
47
|
+
get(templates, locale) {
|
|
80
48
|
return new Proxy(templates[locale] || {}, {
|
|
81
|
-
set
|
|
49
|
+
set(region, key, value) {
|
|
82
50
|
if (typeof value !== "string") {
|
|
83
51
|
throw new Error("Template must be a string.");
|
|
84
52
|
}
|
|
@@ -87,9 +55,8 @@ var Translation = /** @class */ (function () {
|
|
|
87
55
|
}
|
|
88
56
|
});
|
|
89
57
|
},
|
|
90
|
-
set
|
|
91
|
-
templates[locale] = Object.entries(regionTemplates).reduce(
|
|
92
|
-
var key = _a[0], value = _a[1];
|
|
58
|
+
set(templates, locale, regionTemplates) {
|
|
59
|
+
templates[locale] = Object.entries(regionTemplates).reduce((region, [key, value]) => {
|
|
93
60
|
region[key] = parseTemplate(value);
|
|
94
61
|
return region;
|
|
95
62
|
}, {});
|
|
@@ -105,33 +72,32 @@ var Translation = /** @class */ (function () {
|
|
|
105
72
|
* @returns {Runes} - The translated string with dynamic parts inserted.
|
|
106
73
|
* @throws {Error} - If the length of the template parts does not match the length of the template.
|
|
107
74
|
*/
|
|
108
|
-
this.translate =
|
|
75
|
+
this.translate = (locale, strings, ...parts) => {
|
|
109
76
|
var _a, _b, _c;
|
|
110
|
-
var parts = [];
|
|
111
|
-
for (var _i = 2; _i < arguments.length; _i++) {
|
|
112
|
-
parts[_i - 2] = arguments[_i];
|
|
113
|
-
}
|
|
114
77
|
if (typeof strings === "string") {
|
|
115
78
|
strings = strings.split("{}");
|
|
116
79
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
80
|
+
const key = strings.join("{}");
|
|
81
|
+
let translation = (_a = __classPrivateFieldGet(this, _Translation_templates, "f")) === null || _a === void 0 ? void 0 : _a[locale];
|
|
82
|
+
if (!translation) {
|
|
83
|
+
translation = __classPrivateFieldGet(this, _Translation_templates, "f")[this.defaultLocale];
|
|
84
|
+
}
|
|
85
|
+
let { template, order } = (translation === null || translation === void 0 ? void 0 : translation[key]) || {};
|
|
120
86
|
if (!template) {
|
|
121
87
|
if (((_c = (_b = import.meta) === null || _b === void 0 ? void 0 : _b.env) === null || _c === void 0 ? void 0 : _c.MODE) === "development") {
|
|
122
|
-
console.warn(
|
|
88
|
+
console.warn(`not match translate key, ${key}`, { translation, locale, strings, parts });
|
|
123
89
|
}
|
|
124
90
|
template = strings.slice();
|
|
125
|
-
order = parts.map(
|
|
91
|
+
order = parts.map((_, i) => i);
|
|
126
92
|
}
|
|
127
93
|
if (parts.length !== template.length - 1) {
|
|
128
|
-
throw new Error(
|
|
94
|
+
throw new Error(`translate template parts length does not match. locale: ${locale}, key: ${key}`);
|
|
129
95
|
}
|
|
130
|
-
|
|
96
|
+
const runes = template.reduce((runes, template, idx) => {
|
|
131
97
|
runes.push(template);
|
|
132
|
-
|
|
98
|
+
const orderIdx = order[idx];
|
|
133
99
|
if (orderIdx >= 0) {
|
|
134
|
-
|
|
100
|
+
const part = parts[orderIdx];
|
|
135
101
|
if (typeof part === "function") {
|
|
136
102
|
runes.push(part(locale));
|
|
137
103
|
}
|
|
@@ -144,60 +110,59 @@ var Translation = /** @class */ (function () {
|
|
|
144
110
|
return runes;
|
|
145
111
|
};
|
|
146
112
|
/**
|
|
147
|
-
*
|
|
113
|
+
* Use regex extract template and vars
|
|
148
114
|
* @param {*} locale
|
|
149
115
|
* @param {*} regexString
|
|
150
116
|
* @param {*} matchString
|
|
151
117
|
* @returns {Runes}
|
|
152
118
|
*/
|
|
153
|
-
this.translateByRegEx =
|
|
154
|
-
|
|
155
|
-
return
|
|
119
|
+
this.translateByRegEx = (locale, regexString, matchString) => {
|
|
120
|
+
const { parts, strings } = regexTemplate(regexString, matchString);
|
|
121
|
+
return this.translate(locale, strings, ...parts);
|
|
122
|
+
};
|
|
123
|
+
this.l10n = (strings, ...parts) => {
|
|
124
|
+
return translation.translate(this.defaultLocale, strings, ...parts);
|
|
156
125
|
};
|
|
126
|
+
this.l10nRegExp = (regexString, matchString) => {
|
|
127
|
+
return translation.translateByRegEx(this.defaultLocale, regexString, matchString);
|
|
128
|
+
};
|
|
129
|
+
this.defaultLocale = locale;
|
|
157
130
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
return true;
|
|
169
|
-
},
|
|
170
|
-
enumerable: false,
|
|
171
|
-
configurable: true
|
|
172
|
-
});
|
|
173
|
-
return Translation;
|
|
174
|
-
}());
|
|
175
|
-
export { Translation };
|
|
131
|
+
get templates() {
|
|
132
|
+
return __classPrivateFieldGet(this, _Translation_templates, "f");
|
|
133
|
+
}
|
|
134
|
+
set templates(value) {
|
|
135
|
+
Object.entries(value).forEach(([locale, regionTemplates]) => {
|
|
136
|
+
__classPrivateFieldGet(this, _Translation_templates, "f")[locale] = regionTemplates;
|
|
137
|
+
});
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
176
141
|
_Translation_templates = new WeakMap();
|
|
177
|
-
|
|
142
|
+
const translation = new Translation();
|
|
178
143
|
export default translation;
|
|
179
|
-
export
|
|
180
|
-
export
|
|
144
|
+
export const l10n = translation.l10n;
|
|
145
|
+
export const l10nRegExp = translation.l10nRegExp;
|
|
181
146
|
/**
|
|
182
147
|
* @param {string} regexString
|
|
183
148
|
* @param {string} matchString
|
|
184
149
|
* @returns {{ strings: Array<string>, parts: Array<string> }}
|
|
185
150
|
*/
|
|
186
151
|
export function regexTemplate(regexString, matchString) {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
152
|
+
let parts = [];
|
|
153
|
+
const strings = [];
|
|
154
|
+
const matches = new RegExp(regexString).exec(matchString);
|
|
190
155
|
if (!matches) {
|
|
191
|
-
return { parts: [], strings
|
|
156
|
+
return { parts: [], strings };
|
|
192
157
|
}
|
|
193
158
|
parts = matches.slice(1);
|
|
194
159
|
regexString
|
|
195
160
|
.split(/(\([^)]+\))/) // split by regex group
|
|
196
|
-
.forEach(
|
|
161
|
+
.forEach(part => {
|
|
197
162
|
if (!part.startsWith("(") || !part.endsWith(")")) {
|
|
198
163
|
strings.push(part);
|
|
199
164
|
}
|
|
200
165
|
});
|
|
201
|
-
return { strings
|
|
166
|
+
return { strings, parts };
|
|
202
167
|
}
|
|
203
168
|
//# sourceMappingURL=intl.js.map
|
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,MAAM,OAAO,KAAM,SAAQ,KAAK;IAC/B,QAAQ;QACP,OAAO,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IACrB,CAAC;CACD;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,cAAc;IAC3C,MAAM,KAAK,GAAG,EAAE,CAAC;IACjB,MAAM,QAAQ,GAAG,EAAE,CAAC;IACpB,MAAM,KAAK,GAAG,cAAc,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;IAC7C,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;QACtB,IAAI,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC;YAC3B,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;gBACnB,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;YACzB,CAAC;iBAAM,CAAC;gBACP,0CAA0C;gBAC1C,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAA;YAC5C,CAAC;QACF,CAAC;aAAM,CAAC;YACP,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACpB,CAAC;IACF,CAAC,CAAC,CAAA;IAEF,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAA;AAC3B,CAAC;AAED;;GAEG;AACH,MAAM,OAAO,WAAW;IACvB,YAAY,MAAM;QAIlB;;;WAGG;QACH,iCAAa,IAAI,KAAK,CAAC,EAAE,EAAE;YAC1B,GAAG,CAAC,SAAS,EAAE,MAAM;gBACpB,OAAO,IAAI,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,EAAE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,CAAC,MAAM,UAAU,GAAG,WAAW,CAAC,UAAU,CAAA;AAEhD;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,WAAW,EAAE,WAAW;IACrD,IAAI,KAAK,GAAG,EAAE,CAAC;IACf,MAAM,OAAO,GAAG,EAAE,CAAC;IAEnB,MAAM,OAAO,GAAG,IAAI,MAAM,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAA;IACzD,IAAI,CAAC,OAAO,EAAE,CAAC;QACd,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,OAAO,EAAE,CAAA;IAC9B,CAAC;IAED,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;IACxB,WAAW;SACT,KAAK,CAAC,aAAa,CAAC,CAAC,uBAAuB;SAC5C,OAAO,CAAC,IAAI,CAAC,EAAE;QACf,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;YAClD,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACnB,CAAC;IACF,CAAC,CAAC,CAAA;IAEH,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;AAC3B,CAAC"}
|
package/package.json
CHANGED
package/src/intl.js
CHANGED
|
@@ -33,6 +33,10 @@ export function parseTemplate(templateString) {
|
|
|
33
33
|
* Represents a Translation object that handles string translation based on locale and templates.
|
|
34
34
|
*/
|
|
35
35
|
export class Translation {
|
|
36
|
+
constructor(locale) {
|
|
37
|
+
this.defaultLocale = locale
|
|
38
|
+
}
|
|
39
|
+
|
|
36
40
|
/**
|
|
37
41
|
* Templates object that stores the translation templates for each locale.
|
|
38
42
|
* @type {Proxy}
|
|
@@ -89,7 +93,10 @@ export class Translation {
|
|
|
89
93
|
}
|
|
90
94
|
|
|
91
95
|
const key = strings.join("{}")
|
|
92
|
-
|
|
96
|
+
let translation = this.#templates?.[locale]
|
|
97
|
+
if (!translation) {
|
|
98
|
+
translation = this.#templates[this.defaultLocale]
|
|
99
|
+
}
|
|
93
100
|
let { template, order } = translation?.[key] || {}
|
|
94
101
|
if (!template) {
|
|
95
102
|
if (import.meta?.env?.MODE === "development") {
|
|
@@ -123,7 +130,7 @@ export class Translation {
|
|
|
123
130
|
}
|
|
124
131
|
|
|
125
132
|
/**
|
|
126
|
-
*
|
|
133
|
+
* Use regex extract template and vars
|
|
127
134
|
* @param {*} locale
|
|
128
135
|
* @param {*} regexString
|
|
129
136
|
* @param {*} matchString
|
|
@@ -133,15 +140,23 @@ export class Translation {
|
|
|
133
140
|
const { parts, strings } = regexTemplate(regexString, matchString)
|
|
134
141
|
return this.translate(locale, strings, ...parts);
|
|
135
142
|
}
|
|
143
|
+
|
|
144
|
+
l10n = (strings, ...parts) => {
|
|
145
|
+
return translation.translate(this.defaultLocale, strings, ...parts)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
l10nRegExp = (regexString, matchString) => {
|
|
149
|
+
return translation.translateByRegEx(this.defaultLocale, regexString, matchString)
|
|
150
|
+
}
|
|
136
151
|
}
|
|
137
152
|
|
|
138
153
|
const translation = new Translation()
|
|
139
154
|
|
|
140
155
|
export default translation
|
|
141
156
|
|
|
142
|
-
export const l10n = translation.
|
|
157
|
+
export const l10n = translation.l10n
|
|
143
158
|
|
|
144
|
-
export const l10nRegExp = translation.
|
|
159
|
+
export const l10nRegExp = translation.l10nRegExp
|
|
145
160
|
|
|
146
161
|
/**
|
|
147
162
|
* @param {string} regexString
|