@xlsx-editor.dev/i18n 0.0.0-stage → 0.1.0-beta.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/LICENSE +201 -0
- package/README.md +89 -2
- package/THIRD_PARTY_NOTICES.md +3 -0
- package/dist/chunk-RRWZTWUW.js +556 -0
- package/dist/en.d.ts +33 -0
- package/dist/en.js +11 -0
- package/dist/index.d.ts +730 -0
- package/dist/index.js +86 -0
- package/package.json +49 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import {
|
|
2
|
+
en_default
|
|
3
|
+
} from "./chunk-RRWZTWUW.js";
|
|
4
|
+
|
|
5
|
+
// src/index.ts
|
|
6
|
+
var en = en_default;
|
|
7
|
+
var locales = {
|
|
8
|
+
en
|
|
9
|
+
};
|
|
10
|
+
function isRecord(v) {
|
|
11
|
+
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
12
|
+
}
|
|
13
|
+
function deepMerge(base, override) {
|
|
14
|
+
if (!override) return base;
|
|
15
|
+
const result = { ...base };
|
|
16
|
+
for (const key of Object.keys(override)) {
|
|
17
|
+
const overVal = override[key];
|
|
18
|
+
if (overVal === null) continue;
|
|
19
|
+
if (isRecord(base[key]) && isRecord(overVal)) {
|
|
20
|
+
result[key] = deepMerge(base[key], overVal);
|
|
21
|
+
} else if (overVal !== void 0) {
|
|
22
|
+
result[key] = overVal;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
function lookupKey(obj, path) {
|
|
28
|
+
let current = obj;
|
|
29
|
+
for (const part of path.split(".")) {
|
|
30
|
+
if (!isRecord(current)) return void 0;
|
|
31
|
+
current = current[part];
|
|
32
|
+
}
|
|
33
|
+
return typeof current === "string" ? current : void 0;
|
|
34
|
+
}
|
|
35
|
+
function parseBranches(branchStr) {
|
|
36
|
+
const parsed = {};
|
|
37
|
+
const regex = /(=\d+|\w+)\s*\{([^}]*)\}/g;
|
|
38
|
+
let match;
|
|
39
|
+
while ((match = regex.exec(branchStr)) !== null) {
|
|
40
|
+
parsed[match[1]] = match[2];
|
|
41
|
+
}
|
|
42
|
+
return parsed;
|
|
43
|
+
}
|
|
44
|
+
function formatMessage(template, vars, lang) {
|
|
45
|
+
if (!vars) return template;
|
|
46
|
+
const result = template.replace(
|
|
47
|
+
// Linear on hostile templates: the branch group keeps the single-char
|
|
48
|
+
// `[^{}]` alternative (using `[^{}]+` would form `(X+)*`, an exponential
|
|
49
|
+
// pattern), and there is no `\s*` before the group — that `\s*` overlapped
|
|
50
|
+
// with the group's leading whitespace and let a run of spaces be
|
|
51
|
+
// partitioned many ways (the polynomial-ReDoS source). parseBranches
|
|
52
|
+
// already tolerates the leading whitespace now folded into the capture.
|
|
53
|
+
/\{(\w+),\s*plural,((?:[^{}]|\{[^{}]*\})*)\}/g,
|
|
54
|
+
(full, varName, branchStr) => {
|
|
55
|
+
const count = Number(vars[varName]);
|
|
56
|
+
if (isNaN(count)) return full;
|
|
57
|
+
const parsed = parseBranches(branchStr);
|
|
58
|
+
const exact = parsed[`=${count}`];
|
|
59
|
+
if (exact !== void 0) return exact.replace(/#/g, String(count));
|
|
60
|
+
let category;
|
|
61
|
+
try {
|
|
62
|
+
category = new Intl.PluralRules(lang || "en").select(count);
|
|
63
|
+
} catch {
|
|
64
|
+
category = count === 1 ? "one" : "other";
|
|
65
|
+
}
|
|
66
|
+
const text = parsed[category] ?? parsed["other"] ?? "";
|
|
67
|
+
return text.replace(/#/g, String(count));
|
|
68
|
+
}
|
|
69
|
+
);
|
|
70
|
+
return result.replace(/\{(\w+)\}/g, (_, key) => {
|
|
71
|
+
const val = vars[key];
|
|
72
|
+
return val !== void 0 ? String(val) : `{${key}}`;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
function createT(strings, lang = "en") {
|
|
76
|
+
return (key, vars) => {
|
|
77
|
+
const value = lookupKey(strings, key);
|
|
78
|
+
return formatMessage(value ?? key, vars, lang);
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export {
|
|
82
|
+
createT,
|
|
83
|
+
deepMerge,
|
|
84
|
+
en,
|
|
85
|
+
locales
|
|
86
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xlsx-editor.dev/i18n",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "Shared user-facing strings for the XLSX Editor chrome. English is the source of truth.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./en": {
|
|
13
|
+
"types": "./dist/en.d.ts",
|
|
14
|
+
"import": "./dist/en.js"
|
|
15
|
+
},
|
|
16
|
+
"./package.json": "./package.json"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"THIRD_PARTY_NOTICES.md",
|
|
23
|
+
"!dist/metafile-*.json"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "node ../../scripts/check-built-siblings.mjs && tsup",
|
|
27
|
+
"typecheck": "bun check",
|
|
28
|
+
"api:extract": "node ../../scripts/api-extractor.mjs --package @xlsx-editor.dev/i18n --local",
|
|
29
|
+
"api:check": "node ../../scripts/api-extractor.mjs --package @xlsx-editor.dev/i18n"
|
|
30
|
+
},
|
|
31
|
+
"license": "Apache-2.0",
|
|
32
|
+
"keywords": [
|
|
33
|
+
"xlsx",
|
|
34
|
+
"editor",
|
|
35
|
+
"i18n",
|
|
36
|
+
"locales",
|
|
37
|
+
"translations"
|
|
38
|
+
],
|
|
39
|
+
"homepage": "https://xlsx-editor.dev/",
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/eigenpal/xlsx-editor.git",
|
|
43
|
+
"directory": "packages/i18n"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"typescript7": "npm:typescript@^7.0.2"
|
|
50
|
+
}
|
|
51
|
+
}
|