intl-template 1.0.1 → 1.0.3
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/{src/intl.js → intl.js} +13 -2
- package/package.json +4 -5
- package/dist/intl.d.ts +0 -37
- package/dist/intl.js +0 -132
- /package/{src/intl.test.js → intl.test.js} +0 -0
package/{src/intl.js → intl.js}
RENAMED
|
@@ -33,9 +33,20 @@ 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
|
-
/** @type {"" | "react"} */
|
|
36
|
+
/** @type {"string" | "react"} */
|
|
37
37
|
mode = "react"
|
|
38
38
|
|
|
39
|
+
/**
|
|
40
|
+
* The current locale for translation.
|
|
41
|
+
* @type {string}
|
|
42
|
+
**/
|
|
43
|
+
locale = ""
|
|
44
|
+
|
|
45
|
+
constructor(defaultLocale, mode = "react") {
|
|
46
|
+
this.mode = mode
|
|
47
|
+
this.locale = defaultLocale || globalThis?.navigator?.language || "en"
|
|
48
|
+
}
|
|
49
|
+
|
|
39
50
|
/**
|
|
40
51
|
* Templates object that stores the translation templates for each locale.
|
|
41
52
|
* @type {Proxy}
|
|
@@ -134,4 +145,4 @@ const translation = new Translation()
|
|
|
134
145
|
|
|
135
146
|
export default translation
|
|
136
147
|
|
|
137
|
-
export const l10n = translation.translate.
|
|
148
|
+
export const l10n = (strings, ...parts) => translation.translate(translation.locale, strings, ...parts);
|
package/package.json
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "intl-template",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "l10n tagged template literals",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "
|
|
7
|
-
"module": "src/intl.js",
|
|
6
|
+
"main": "intl.js",
|
|
8
7
|
"repository": "github:performonkey/intl-template",
|
|
9
8
|
"scripts": {
|
|
10
|
-
"
|
|
11
|
-
"
|
|
9
|
+
"prepublish": "tsc --emitDeclarationOnly --allowJs -d --outDir . *.js",
|
|
10
|
+
"postpublish": "rm -rf *.d.ts"
|
|
12
11
|
},
|
|
13
12
|
"keywords": [
|
|
14
13
|
"i18n",
|
package/dist/intl.d.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Parses a template string and extracts the template parts and order of slots.
|
|
3
|
-
* @param {string} templateString - The template string to parse.
|
|
4
|
-
* @returns {{ template: string[], order: number[] }}
|
|
5
|
-
*/
|
|
6
|
-
export function parseTemplate(templateString: string): {
|
|
7
|
-
template: string[];
|
|
8
|
-
order: number[];
|
|
9
|
-
};
|
|
10
|
-
export class Runes extends Array<any> {
|
|
11
|
-
constructor(arrayLength?: number);
|
|
12
|
-
constructor(arrayLength: number);
|
|
13
|
-
constructor(...items: any[]);
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Represents a Translation object that handles string translation based on locale and templates.
|
|
17
|
-
*/
|
|
18
|
-
export class Translation {
|
|
19
|
-
/** @type {"" | "react"} */
|
|
20
|
-
mode: "" | "react";
|
|
21
|
-
set templates(value: Proxy);
|
|
22
|
-
get templates(): Proxy;
|
|
23
|
-
/**
|
|
24
|
-
* Translates a string based on the provided locale and strings.
|
|
25
|
-
*
|
|
26
|
-
* @param {string} locale - The locale to use for translation.
|
|
27
|
-
* @param {TemplateStringsArray | string} strings - The string or array of strings to be translated.
|
|
28
|
-
* @param {...any} parts - The dynamic parts to be inserted into the translated string.
|
|
29
|
-
* @returns {Runes} - The translated string with dynamic parts inserted.
|
|
30
|
-
* @throws {Error} - If the length of the template parts does not match the length of the template.
|
|
31
|
-
*/
|
|
32
|
-
translate: (locale: string, strings: TemplateStringsArray | string, ...parts: any[]) => Runes;
|
|
33
|
-
#private;
|
|
34
|
-
}
|
|
35
|
-
export default translation;
|
|
36
|
-
export const l10n: any;
|
|
37
|
-
declare const translation: Translation;
|
package/dist/intl.js
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
6
|
-
var __toCommonJS = (from) => {
|
|
7
|
-
var entry = __moduleCache.get(from), desc;
|
|
8
|
-
if (entry)
|
|
9
|
-
return entry;
|
|
10
|
-
entry = __defProp({}, "__esModule", { value: true });
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function")
|
|
12
|
-
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
13
|
-
get: () => from[key],
|
|
14
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
-
}));
|
|
16
|
-
__moduleCache.set(from, entry);
|
|
17
|
-
return entry;
|
|
18
|
-
};
|
|
19
|
-
var __export = (target, all) => {
|
|
20
|
-
for (var name in all)
|
|
21
|
-
__defProp(target, name, {
|
|
22
|
-
get: all[name],
|
|
23
|
-
enumerable: true,
|
|
24
|
-
configurable: true,
|
|
25
|
-
set: (newValue) => all[name] = () => newValue
|
|
26
|
-
});
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
// src/intl.js
|
|
30
|
-
var exports_intl = {};
|
|
31
|
-
__export(exports_intl, {
|
|
32
|
-
parseTemplate: () => parseTemplate,
|
|
33
|
-
l10n: () => l10n,
|
|
34
|
-
default: () => intl_default,
|
|
35
|
-
Translation: () => Translation,
|
|
36
|
-
Runes: () => Runes
|
|
37
|
-
});
|
|
38
|
-
module.exports = __toCommonJS(exports_intl);
|
|
39
|
-
|
|
40
|
-
class Runes extends Array {
|
|
41
|
-
toString() {
|
|
42
|
-
return this.join("");
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
function parseTemplate(templateString) {
|
|
46
|
-
const order = [];
|
|
47
|
-
const template = [];
|
|
48
|
-
const parts = templateString.split(/({\d*})/);
|
|
49
|
-
parts.forEach((part) => {
|
|
50
|
-
if (part.match(/^{\d*}$/)) {
|
|
51
|
-
if (part === "{}") {
|
|
52
|
-
order.push(order.length);
|
|
53
|
-
} else {
|
|
54
|
-
order.push(parseInt(part.slice(1, -1), 10));
|
|
55
|
-
}
|
|
56
|
-
} else {
|
|
57
|
-
template.push(part);
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
return { template, order };
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
class Translation {
|
|
64
|
-
mode = "react";
|
|
65
|
-
#templates = new Proxy({}, {
|
|
66
|
-
get(templates, locale) {
|
|
67
|
-
return new Proxy(templates[locale] || {}, {
|
|
68
|
-
set(region, key, value) {
|
|
69
|
-
if (typeof value !== "string") {
|
|
70
|
-
throw new Error("Template must be a string.");
|
|
71
|
-
}
|
|
72
|
-
region[key] = parseTemplate(value);
|
|
73
|
-
return true;
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
},
|
|
77
|
-
set(templates, locale, regionTemplates) {
|
|
78
|
-
templates[locale] = Object.entries(regionTemplates).reduce((region, [key, value]) => {
|
|
79
|
-
region[key] = parseTemplate(value);
|
|
80
|
-
return region;
|
|
81
|
-
}, {});
|
|
82
|
-
return true;
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
get templates() {
|
|
86
|
-
return this.#templates;
|
|
87
|
-
}
|
|
88
|
-
set templates(value) {
|
|
89
|
-
Object.entries(value).forEach(([locale, regionTemplates]) => {
|
|
90
|
-
this.#templates[locale] = regionTemplates;
|
|
91
|
-
});
|
|
92
|
-
return true;
|
|
93
|
-
}
|
|
94
|
-
translate = (locale, strings, ...parts) => {
|
|
95
|
-
if (typeof strings === "string") {
|
|
96
|
-
strings = strings.split("{}");
|
|
97
|
-
}
|
|
98
|
-
const key = strings.join("{}");
|
|
99
|
-
const translation = this.#templates?.[locale];
|
|
100
|
-
let { template, order } = translation?.[key] || {};
|
|
101
|
-
if (!template) {
|
|
102
|
-
if (import.meta?.env?.MODE === "development") {
|
|
103
|
-
console.warn(`not match translate key, ${key}`, { translation, locale, strings, parts });
|
|
104
|
-
}
|
|
105
|
-
template = strings.slice();
|
|
106
|
-
order = parts.map((_, i) => i);
|
|
107
|
-
}
|
|
108
|
-
if (parts.length !== template.length - 1) {
|
|
109
|
-
throw new Error(`translate template parts length does not match. locale: ${locale}, key: ${key}`);
|
|
110
|
-
}
|
|
111
|
-
const runes = template.reduce((runes2, template2, idx) => {
|
|
112
|
-
runes2.push(template2);
|
|
113
|
-
const orderIdx = order[idx];
|
|
114
|
-
if (orderIdx >= 0) {
|
|
115
|
-
const part = parts[orderIdx];
|
|
116
|
-
if (typeof part === "function") {
|
|
117
|
-
runes2.push(part(locale));
|
|
118
|
-
} else {
|
|
119
|
-
runes2.push(part);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
return runes2;
|
|
123
|
-
}, new Runes);
|
|
124
|
-
if (this.mode !== "react") {
|
|
125
|
-
return runes.toString();
|
|
126
|
-
}
|
|
127
|
-
return runes;
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
var translation = new Translation;
|
|
131
|
-
var intl_default = translation;
|
|
132
|
-
var l10n = translation.translate.bind(null, globalThis?.navigator?.language);
|
|
File without changes
|