@aidc-toolkit/core 0.9.15-beta → 0.9.16-beta
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/index.js +2 -77
- package/dist/index.js.map +1 -0
- package/dist/locale/i18n.js +140 -0
- package/dist/locale/i18n.js.map +1 -0
- package/package.json +5 -5
- package/dist/index.cjs +0 -132
- package/dist/index.d.cts +0 -66
package/dist/index.js
CHANGED
|
@@ -1,80 +1,3 @@
|
|
|
1
|
-
// src/locale/i18n.ts
|
|
2
|
-
import I18nextBrowserLanguageDetector from "i18next-browser-languagedetector";
|
|
3
|
-
import I18nextCLILanguageDetector from "i18next-cli-language-detector";
|
|
4
|
-
var I18NEnvironment = /* @__PURE__ */ ((I18NEnvironment2) => {
|
|
5
|
-
I18NEnvironment2[I18NEnvironment2["CLI"] = 0] = "CLI";
|
|
6
|
-
I18NEnvironment2[I18NEnvironment2["Server"] = 1] = "Server";
|
|
7
|
-
I18NEnvironment2[I18NEnvironment2["Browser"] = 2] = "Browser";
|
|
8
|
-
return I18NEnvironment2;
|
|
9
|
-
})(I18NEnvironment || {});
|
|
10
|
-
function i18nAssertValidResources(enResources, lng, lngResources, parent) {
|
|
11
|
-
const enResourcesMap = new Map(Object.entries(enResources));
|
|
12
|
-
const lngResourcesMap = new Map(Object.entries(lngResources));
|
|
13
|
-
const isLocale = lng.includes("-");
|
|
14
|
-
for (const [enKey, enValue] of enResourcesMap) {
|
|
15
|
-
const lngValue = lngResourcesMap.get(enKey);
|
|
16
|
-
if (lngValue !== void 0) {
|
|
17
|
-
const enValueType = typeof enValue;
|
|
18
|
-
const lngValueType = typeof lngValue;
|
|
19
|
-
if (lngValueType !== enValueType) {
|
|
20
|
-
throw new Error(`Invalid value type ${lngValueType} for key ${parent === void 0 ? "" : `${parent}.`}${enKey} in ${lng} resources`);
|
|
21
|
-
}
|
|
22
|
-
if (enValueType === "object") {
|
|
23
|
-
i18nAssertValidResources(enValue, lng, lngValue, `${parent === void 0 ? "" : `${parent}.`}${enKey}`);
|
|
24
|
-
}
|
|
25
|
-
} else if (!isLocale) {
|
|
26
|
-
throw new Error(`Missing key ${parent === void 0 ? "" : `${parent}.`}${enKey} from ${lng} resources`);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
for (const [lngKey] of lngResourcesMap) {
|
|
30
|
-
if (!enResourcesMap.has(lngKey)) {
|
|
31
|
-
throw new Error(`Extraneous key ${parent === void 0 ? "" : `${parent}.`}${lngKey} in ${lng} resources`);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
function toLowerCase(s) {
|
|
36
|
-
return s.split(" ").map((word) => /[a-z]/.test(word) ? word.toLowerCase() : word).join(" ");
|
|
37
|
-
}
|
|
38
|
-
async function i18nCoreInit(i18next, environment, debug, defaultNS, ...resources) {
|
|
39
|
-
if (!i18next.isInitialized) {
|
|
40
|
-
const mergedResource = {};
|
|
41
|
-
for (const resource of resources) {
|
|
42
|
-
for (const [language, resourceLanguage] of Object.entries(resource)) {
|
|
43
|
-
if (!(language in mergedResource)) {
|
|
44
|
-
mergedResource[language] = {};
|
|
45
|
-
}
|
|
46
|
-
const mergedResourceLanguage = mergedResource[language];
|
|
47
|
-
for (const [namespace, resourceKey] of Object.entries(resourceLanguage)) {
|
|
48
|
-
mergedResourceLanguage[namespace] = resourceKey;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
let module;
|
|
53
|
-
switch (environment) {
|
|
54
|
-
case 0 /* CLI */:
|
|
55
|
-
module = I18nextCLILanguageDetector;
|
|
56
|
-
break;
|
|
57
|
-
case 2 /* Browser */:
|
|
58
|
-
module = I18nextBrowserLanguageDetector;
|
|
59
|
-
break;
|
|
60
|
-
default:
|
|
61
|
-
throw new Error("Not supported");
|
|
62
|
-
}
|
|
63
|
-
await i18next.use(module).init({
|
|
64
|
-
debug,
|
|
65
|
-
resources: mergedResource,
|
|
66
|
-
fallbackLng: "en",
|
|
67
|
-
defaultNS
|
|
68
|
-
}).then(() => {
|
|
69
|
-
i18next.services.formatter?.add("toLowerCase", (value) => typeof value === "string" ? toLowerCase(value) : String(value));
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
export {
|
|
74
|
-
I18NEnvironment,
|
|
75
|
-
i18nAssertValidResources,
|
|
76
|
-
i18nCoreInit
|
|
77
|
-
};
|
|
78
1
|
/*!
|
|
79
2
|
* Copyright © 2024-2025 Dolphin Data Development Ltd. and AIDC Toolkit
|
|
80
3
|
* contributors
|
|
@@ -91,3 +14,5 @@ export {
|
|
|
91
14
|
* See the License for the specific language governing permissions and
|
|
92
15
|
* limitations under the License.
|
|
93
16
|
*/
|
|
17
|
+
export * from "./locale/i18n.js";
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,cAAc,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import I18nextBrowserLanguageDetector from "i18next-browser-languagedetector";
|
|
2
|
+
import I18nextCLILanguageDetector from "i18next-cli-language-detector";
|
|
3
|
+
/**
|
|
4
|
+
* Internationalization operating environment.
|
|
5
|
+
*/
|
|
6
|
+
export var I18NEnvironment;
|
|
7
|
+
(function (I18NEnvironment) {
|
|
8
|
+
/**
|
|
9
|
+
* Command-line interface (e.g., unit tests).
|
|
10
|
+
*/
|
|
11
|
+
I18NEnvironment[I18NEnvironment["CLI"] = 0] = "CLI";
|
|
12
|
+
/**
|
|
13
|
+
* Web server.
|
|
14
|
+
*/
|
|
15
|
+
I18NEnvironment[I18NEnvironment["Server"] = 1] = "Server";
|
|
16
|
+
/**
|
|
17
|
+
* Web browser.
|
|
18
|
+
*/
|
|
19
|
+
I18NEnvironment[I18NEnvironment["Browser"] = 2] = "Browser";
|
|
20
|
+
})(I18NEnvironment || (I18NEnvironment = {}));
|
|
21
|
+
/**
|
|
22
|
+
* Assert that language resources are a type match for English (default) resources.
|
|
23
|
+
*
|
|
24
|
+
* @param enResources
|
|
25
|
+
* English resources.
|
|
26
|
+
*
|
|
27
|
+
* @param lng
|
|
28
|
+
* Language.
|
|
29
|
+
*
|
|
30
|
+
* @param lngResources
|
|
31
|
+
* Language resources.
|
|
32
|
+
*
|
|
33
|
+
* @param parent
|
|
34
|
+
* Parent key name (set recursively).
|
|
35
|
+
*/
|
|
36
|
+
export function i18nAssertValidResources(enResources, lng, lngResources, parent) {
|
|
37
|
+
const enResourcesMap = new Map(Object.entries(enResources));
|
|
38
|
+
const lngResourcesMap = new Map(Object.entries(lngResources));
|
|
39
|
+
const isLocale = lng.includes("-");
|
|
40
|
+
for (const [enKey, enValue] of enResourcesMap) {
|
|
41
|
+
const lngValue = lngResourcesMap.get(enKey);
|
|
42
|
+
if (lngValue !== undefined) {
|
|
43
|
+
const enValueType = typeof enValue;
|
|
44
|
+
const lngValueType = typeof lngValue;
|
|
45
|
+
if (lngValueType !== enValueType) {
|
|
46
|
+
throw new Error(`Invalid value type ${lngValueType} for key ${parent === undefined ? "" : `${parent}.`}${enKey} in ${lng} resources`);
|
|
47
|
+
}
|
|
48
|
+
if (enValueType === "object") {
|
|
49
|
+
i18nAssertValidResources(enValue, lng, lngValue, `${parent === undefined ? "" : `${parent}.`}${enKey}`);
|
|
50
|
+
}
|
|
51
|
+
// Locale falls back to raw language so ignore if missing.
|
|
52
|
+
}
|
|
53
|
+
else if (!isLocale) {
|
|
54
|
+
throw new Error(`Missing key ${parent === undefined ? "" : `${parent}.`}${enKey} from ${lng} resources`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
for (const [lngKey] of lngResourcesMap) {
|
|
58
|
+
if (!enResourcesMap.has(lngKey)) {
|
|
59
|
+
throw new Error(`Extraneous key ${parent === undefined ? "" : `${parent}.`}${lngKey} in ${lng} resources`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Convert a string to lower case, skipping words that are all upper case.
|
|
65
|
+
*
|
|
66
|
+
* @param s
|
|
67
|
+
* String.
|
|
68
|
+
*
|
|
69
|
+
* @returns
|
|
70
|
+
* Lower case string.
|
|
71
|
+
*/
|
|
72
|
+
function toLowerCase(s) {
|
|
73
|
+
// Words with no lower case letters are preserved as they are likely mnemonics.
|
|
74
|
+
return s.split(" ").map(word => /[a-z]/.test(word) ? word.toLowerCase() : word).join(" ");
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Initialize internationalization.
|
|
78
|
+
*
|
|
79
|
+
* @param i18next
|
|
80
|
+
* Internationalization object. As multiple objects exists, this parameter represents the one for the module for which
|
|
81
|
+
* internationalization is being initialized.
|
|
82
|
+
*
|
|
83
|
+
* @param environment
|
|
84
|
+
* Environment in which the application is running.
|
|
85
|
+
*
|
|
86
|
+
* @param debug
|
|
87
|
+
* Debug setting.
|
|
88
|
+
*
|
|
89
|
+
* @param defaultNS
|
|
90
|
+
* Default namespace.
|
|
91
|
+
*
|
|
92
|
+
* @param resources
|
|
93
|
+
* Resources.
|
|
94
|
+
*
|
|
95
|
+
* @returns
|
|
96
|
+
* Void promise.
|
|
97
|
+
*/
|
|
98
|
+
export async function i18nCoreInit(i18next, environment, debug, defaultNS, ...resources) {
|
|
99
|
+
// Initialization may be called more than once.
|
|
100
|
+
if (!i18next.isInitialized) {
|
|
101
|
+
const mergedResource = {};
|
|
102
|
+
// Merge resources.
|
|
103
|
+
for (const resource of resources) {
|
|
104
|
+
// Merge languages.
|
|
105
|
+
for (const [language, resourceLanguage] of Object.entries(resource)) {
|
|
106
|
+
if (!(language in mergedResource)) {
|
|
107
|
+
mergedResource[language] = {};
|
|
108
|
+
}
|
|
109
|
+
const mergedResourceLanguage = mergedResource[language];
|
|
110
|
+
// Merge namespaces.
|
|
111
|
+
for (const [namespace, resourceKey] of Object.entries(resourceLanguage)) {
|
|
112
|
+
mergedResourceLanguage[namespace] = resourceKey;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
let module;
|
|
117
|
+
switch (environment) {
|
|
118
|
+
case I18NEnvironment.CLI:
|
|
119
|
+
// TODO Refactor when https://github.com/neet/i18next-cli-language-detector/issues/281 resolved.
|
|
120
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Per above.
|
|
121
|
+
module = I18nextCLILanguageDetector;
|
|
122
|
+
break;
|
|
123
|
+
case I18NEnvironment.Browser:
|
|
124
|
+
module = I18nextBrowserLanguageDetector;
|
|
125
|
+
break;
|
|
126
|
+
default:
|
|
127
|
+
throw new Error("Not supported");
|
|
128
|
+
}
|
|
129
|
+
await i18next.use(module).init({
|
|
130
|
+
debug,
|
|
131
|
+
resources: mergedResource,
|
|
132
|
+
fallbackLng: "en",
|
|
133
|
+
defaultNS
|
|
134
|
+
}).then(() => {
|
|
135
|
+
// Add toLowerCase function.
|
|
136
|
+
i18next.services.formatter?.add("toLowerCase", value => typeof value === "string" ? toLowerCase(value) : String(value));
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=i18n.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"i18n.js","sourceRoot":"","sources":["../../src/locale/i18n.ts"],"names":[],"mappings":"AACA,OAAO,8BAA8B,MAAM,kCAAkC,CAAC;AAC9E,OAAO,0BAA0B,MAAM,+BAA+B,CAAC;AASvE;;GAEG;AACH,MAAM,CAAN,IAAY,eAeX;AAfD,WAAY,eAAe;IACvB;;OAEG;IACH,mDAAG,CAAA;IAEH;;OAEG;IACH,yDAAM,CAAA;IAEN;;OAEG;IACH,2DAAO,CAAA;AACX,CAAC,EAfW,eAAe,KAAf,eAAe,QAe1B;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,wBAAwB,CAAC,WAAmB,EAAE,GAAW,EA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G,cAAc,CAAC,QAAQ,CAAC,CAAC;gBAExD,oBAAoB;gBACpB,KAAK,MAAM,CAAC,SAAS,EAAE,WAAW,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,gBAAgB,CAAC,EAAE,CAAC;oBACtE,sBAAsB,CAAC,SAAS,CAAC,GAAG,WAAW,CAAC;gBACpD,CAAC;YACL,CAAC;QACL,CAAC;QAED,IAAI,MAAyC,CAAC;QAE9C,QAAQ,WAAW,EAAE,CAAC;YAClB,KAAK,eAAe,CAAC,GAAG;gBACpB,gGAAgG;gBAChG,qFAAqF;gBACrF,MAAM,GAAG,0BAA+D,CAAC;gBACzE,MAAM;YAEV,KAAK,eAAe,CAAC,OAAO;gBACxB,MAAM,GAAG,8BAA8B,CAAC;gBACxC,MAAM;YAEV;gBACI,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC;QACzC,CAAC;QAED,MAAM,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC;YAC3B,KAAK;YACL,SAAS,EAAE,cAAc;YACzB,WAAW,EAAE,IAAI;YACjB,SAAS;SACZ,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE;YACT,4BAA4B;YAC5B,OAAO,CAAC,QAAQ,CAAC,SAAS,EAAE,GAAG,CAAC,aAAa,EAAE,KAAK,CAAC,EAAE,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;QAC5H,CAAC,CAAC,CAAC;IACP,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aidc-toolkit/core",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.16-beta",
|
|
4
4
|
"description": "Core functionality for AIDC Toolkit",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -21,14 +21,14 @@
|
|
|
21
21
|
"scripts": {
|
|
22
22
|
"update-aidc-toolkit": "npm update @aidc-toolkit/dev",
|
|
23
23
|
"lint": "eslint",
|
|
24
|
-
"build:core": "
|
|
25
|
-
"build:dev": "npm run build:core
|
|
26
|
-
"build:release": "npm run build:core --
|
|
24
|
+
"build:core": "rimraf dist && tsc --project",
|
|
25
|
+
"build:dev": "npm run build:core -- node_modules/@aidc-toolkit/dev/tsconfig-build-dev.json",
|
|
26
|
+
"build:release": "npm run build:core -- node_modules/@aidc-toolkit/dev/tsconfig-build.json",
|
|
27
27
|
"build:doc": "npm run build:dev",
|
|
28
28
|
"publish-dev": "publish-dev"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@aidc-toolkit/dev": "^0.9.
|
|
31
|
+
"@aidc-toolkit/dev": "^0.9.16-beta"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"i18next": "^24.2.2",
|
package/dist/index.cjs
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var index_exports = {};
|
|
32
|
-
__export(index_exports, {
|
|
33
|
-
I18NEnvironment: () => I18NEnvironment,
|
|
34
|
-
i18nAssertValidResources: () => i18nAssertValidResources,
|
|
35
|
-
i18nCoreInit: () => i18nCoreInit
|
|
36
|
-
});
|
|
37
|
-
module.exports = __toCommonJS(index_exports);
|
|
38
|
-
|
|
39
|
-
// src/locale/i18n.ts
|
|
40
|
-
var import_i18next_browser_languagedetector = __toESM(require("i18next-browser-languagedetector"), 1);
|
|
41
|
-
var import_i18next_cli_language_detector = __toESM(require("i18next-cli-language-detector"), 1);
|
|
42
|
-
var I18NEnvironment = /* @__PURE__ */ ((I18NEnvironment2) => {
|
|
43
|
-
I18NEnvironment2[I18NEnvironment2["CLI"] = 0] = "CLI";
|
|
44
|
-
I18NEnvironment2[I18NEnvironment2["Server"] = 1] = "Server";
|
|
45
|
-
I18NEnvironment2[I18NEnvironment2["Browser"] = 2] = "Browser";
|
|
46
|
-
return I18NEnvironment2;
|
|
47
|
-
})(I18NEnvironment || {});
|
|
48
|
-
function i18nAssertValidResources(enResources, lng, lngResources, parent) {
|
|
49
|
-
const enResourcesMap = new Map(Object.entries(enResources));
|
|
50
|
-
const lngResourcesMap = new Map(Object.entries(lngResources));
|
|
51
|
-
const isLocale = lng.includes("-");
|
|
52
|
-
for (const [enKey, enValue] of enResourcesMap) {
|
|
53
|
-
const lngValue = lngResourcesMap.get(enKey);
|
|
54
|
-
if (lngValue !== void 0) {
|
|
55
|
-
const enValueType = typeof enValue;
|
|
56
|
-
const lngValueType = typeof lngValue;
|
|
57
|
-
if (lngValueType !== enValueType) {
|
|
58
|
-
throw new Error(`Invalid value type ${lngValueType} for key ${parent === void 0 ? "" : `${parent}.`}${enKey} in ${lng} resources`);
|
|
59
|
-
}
|
|
60
|
-
if (enValueType === "object") {
|
|
61
|
-
i18nAssertValidResources(enValue, lng, lngValue, `${parent === void 0 ? "" : `${parent}.`}${enKey}`);
|
|
62
|
-
}
|
|
63
|
-
} else if (!isLocale) {
|
|
64
|
-
throw new Error(`Missing key ${parent === void 0 ? "" : `${parent}.`}${enKey} from ${lng} resources`);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
for (const [lngKey] of lngResourcesMap) {
|
|
68
|
-
if (!enResourcesMap.has(lngKey)) {
|
|
69
|
-
throw new Error(`Extraneous key ${parent === void 0 ? "" : `${parent}.`}${lngKey} in ${lng} resources`);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
function toLowerCase(s) {
|
|
74
|
-
return s.split(" ").map((word) => /[a-z]/.test(word) ? word.toLowerCase() : word).join(" ");
|
|
75
|
-
}
|
|
76
|
-
async function i18nCoreInit(i18next, environment, debug, defaultNS, ...resources) {
|
|
77
|
-
if (!i18next.isInitialized) {
|
|
78
|
-
const mergedResource = {};
|
|
79
|
-
for (const resource of resources) {
|
|
80
|
-
for (const [language, resourceLanguage] of Object.entries(resource)) {
|
|
81
|
-
if (!(language in mergedResource)) {
|
|
82
|
-
mergedResource[language] = {};
|
|
83
|
-
}
|
|
84
|
-
const mergedResourceLanguage = mergedResource[language];
|
|
85
|
-
for (const [namespace, resourceKey] of Object.entries(resourceLanguage)) {
|
|
86
|
-
mergedResourceLanguage[namespace] = resourceKey;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
let module2;
|
|
91
|
-
switch (environment) {
|
|
92
|
-
case 0 /* CLI */:
|
|
93
|
-
module2 = import_i18next_cli_language_detector.default;
|
|
94
|
-
break;
|
|
95
|
-
case 2 /* Browser */:
|
|
96
|
-
module2 = import_i18next_browser_languagedetector.default;
|
|
97
|
-
break;
|
|
98
|
-
default:
|
|
99
|
-
throw new Error("Not supported");
|
|
100
|
-
}
|
|
101
|
-
await i18next.use(module2).init({
|
|
102
|
-
debug,
|
|
103
|
-
resources: mergedResource,
|
|
104
|
-
fallbackLng: "en",
|
|
105
|
-
defaultNS
|
|
106
|
-
}).then(() => {
|
|
107
|
-
i18next.services.formatter?.add("toLowerCase", (value) => typeof value === "string" ? toLowerCase(value) : String(value));
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
112
|
-
0 && (module.exports = {
|
|
113
|
-
I18NEnvironment,
|
|
114
|
-
i18nAssertValidResources,
|
|
115
|
-
i18nCoreInit
|
|
116
|
-
});
|
|
117
|
-
/*!
|
|
118
|
-
* Copyright © 2024-2025 Dolphin Data Development Ltd. and AIDC Toolkit
|
|
119
|
-
* contributors
|
|
120
|
-
*
|
|
121
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
122
|
-
* you may not use this file except in compliance with the License.
|
|
123
|
-
* You may obtain a copy of the License at
|
|
124
|
-
*
|
|
125
|
-
* https://www.apache.org/licenses/LICENSE-2.0
|
|
126
|
-
*
|
|
127
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
128
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
129
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
130
|
-
* See the License for the specific language governing permissions and
|
|
131
|
-
* limitations under the License.
|
|
132
|
-
*/
|
package/dist/index.d.cts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { i18n, Resource } from 'i18next';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Locale strings type for generic manipulation.
|
|
5
|
-
*/
|
|
6
|
-
interface LocaleStrings {
|
|
7
|
-
[key: string]: LocaleStrings | string;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Internationalization operating environment.
|
|
11
|
-
*/
|
|
12
|
-
declare enum I18NEnvironment {
|
|
13
|
-
/**
|
|
14
|
-
* Command-line interface (e.g., unit tests).
|
|
15
|
-
*/
|
|
16
|
-
CLI = 0,
|
|
17
|
-
/**
|
|
18
|
-
* Web server.
|
|
19
|
-
*/
|
|
20
|
-
Server = 1,
|
|
21
|
-
/**
|
|
22
|
-
* Web browser.
|
|
23
|
-
*/
|
|
24
|
-
Browser = 2
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Assert that language resources are a type match for English (default) resources.
|
|
28
|
-
*
|
|
29
|
-
* @param enResources
|
|
30
|
-
* English resources.
|
|
31
|
-
*
|
|
32
|
-
* @param lng
|
|
33
|
-
* Language.
|
|
34
|
-
*
|
|
35
|
-
* @param lngResources
|
|
36
|
-
* Language resources.
|
|
37
|
-
*
|
|
38
|
-
* @param parent
|
|
39
|
-
* Parent key name (set recursively).
|
|
40
|
-
*/
|
|
41
|
-
declare function i18nAssertValidResources(enResources: object, lng: string, lngResources: object, parent?: string): void;
|
|
42
|
-
/**
|
|
43
|
-
* Initialize internationalization.
|
|
44
|
-
*
|
|
45
|
-
* @param i18next
|
|
46
|
-
* Internationalization object. As multiple objects exists, this parameter represents the one for the module for which
|
|
47
|
-
* internationalization is being initialized.
|
|
48
|
-
*
|
|
49
|
-
* @param environment
|
|
50
|
-
* Environment in which the application is running.
|
|
51
|
-
*
|
|
52
|
-
* @param debug
|
|
53
|
-
* Debug setting.
|
|
54
|
-
*
|
|
55
|
-
* @param defaultNS
|
|
56
|
-
* Default namespace.
|
|
57
|
-
*
|
|
58
|
-
* @param resources
|
|
59
|
-
* Resources.
|
|
60
|
-
*
|
|
61
|
-
* @returns
|
|
62
|
-
* Void promise.
|
|
63
|
-
*/
|
|
64
|
-
declare function i18nCoreInit(i18next: i18n, environment: I18NEnvironment, debug: boolean, defaultNS: string, ...resources: Resource[]): Promise<void>;
|
|
65
|
-
|
|
66
|
-
export { I18NEnvironment, type LocaleStrings, i18nAssertValidResources, i18nCoreInit };
|