@aidc-toolkit/core 0.9.21-beta → 1.0.23-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/locale/i18n.d.ts +2 -2
- package/dist/locale/i18n.d.ts.map +1 -1
- package/dist/locale/i18n.js +38 -3
- package/dist/locale/i18n.js.map +1 -1
- package/package.json +1 -1
- package/src/locale/i18n.ts +48 -5
package/dist/locale/i18n.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ import type { i18n, Resource } from "i18next";
|
|
|
2
2
|
/**
|
|
3
3
|
* Locale strings type for generic manipulation.
|
|
4
4
|
*/
|
|
5
|
-
export interface
|
|
6
|
-
[key: string]:
|
|
5
|
+
export interface LocaleResources {
|
|
6
|
+
[key: string]: LocaleResources | string;
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
9
|
* Internationalization operating environments.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"i18n.d.ts","sourceRoot":"","sources":["../../src/locale/i18n.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAA0B,QAAQ,EAAE,MAAM,SAAS,CAAC;AAItE;;GAEG;AACH,MAAM,WAAW,
|
|
1
|
+
{"version":3,"file":"i18n.d.ts","sourceRoot":"","sources":["../../src/locale/i18n.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAA0B,QAAQ,EAAE,MAAM,SAAS,CAAC;AAItE;;GAEG;AACH,MAAM,WAAW,eAAe;IAC5B,CAAC,GAAG,EAAE,MAAM,GAAG,eAAe,GAAG,MAAM,CAAC;CAC3C;AAED;;GAEG;AACH,eAAO,MAAM,gBAAgB;IACzB;;OAEG;;IAGH;;OAEG;;IAGH;;OAEG;;CAEG,CAAC;AAEX;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,OAAO,gBAAgB,CAAC,MAAM,OAAO,gBAAgB,CAAC,CAAC;AAyBrF;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,wBAAwB,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAmDtH;AAgBD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAsB,YAAY,CAAC,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,eAAe,EAAE,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,SAAS,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAiD1J"}
|
package/dist/locale/i18n.js
CHANGED
|
@@ -17,6 +17,24 @@ export const I18nEnvironments = {
|
|
|
17
17
|
*/
|
|
18
18
|
Browser: 2
|
|
19
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* Parse parameter names in a resource string.
|
|
22
|
+
*
|
|
23
|
+
* @param s
|
|
24
|
+
* Resource string.
|
|
25
|
+
*
|
|
26
|
+
* @returns
|
|
27
|
+
* Array of parameter names.
|
|
28
|
+
*/
|
|
29
|
+
function parseParameterNames(s) {
|
|
30
|
+
const parameterRegExp = /\{\{.+?}}/g;
|
|
31
|
+
const parameterNames = [];
|
|
32
|
+
let match;
|
|
33
|
+
while ((match = parameterRegExp.exec(s)) !== null) {
|
|
34
|
+
parameterNames.push(match[1]);
|
|
35
|
+
}
|
|
36
|
+
return parameterNames;
|
|
37
|
+
}
|
|
20
38
|
/**
|
|
21
39
|
* Assert that language resources are a type match for English (default) resources.
|
|
22
40
|
*
|
|
@@ -37,20 +55,37 @@ export function i18nAssertValidResources(enResources, lng, lngResources, parent)
|
|
|
37
55
|
const lngResourcesMap = new Map(Object.entries(lngResources));
|
|
38
56
|
const isLocale = lng.includes("-");
|
|
39
57
|
for (const [enKey, enValue] of enResourcesMap) {
|
|
58
|
+
const enFullKey = `${parent === undefined ? "" : `${parent}.`}${enKey}`;
|
|
40
59
|
const lngValue = lngResourcesMap.get(enKey);
|
|
41
60
|
if (lngValue !== undefined) {
|
|
42
61
|
const enValueType = typeof enValue;
|
|
43
62
|
const lngValueType = typeof lngValue;
|
|
44
63
|
if (lngValueType !== enValueType) {
|
|
45
|
-
throw new Error(`
|
|
64
|
+
throw new Error(`Mismatched value type ${lngValueType} for key ${enFullKey} in ${lng} resources (expected ${enValueType})`);
|
|
65
|
+
}
|
|
66
|
+
if (enValueType === "string") {
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Value is known to be string.
|
|
68
|
+
const enParameterNames = parseParameterNames(enValue);
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Value is known to be string.
|
|
70
|
+
const lngParameterNames = parseParameterNames(lngValue);
|
|
71
|
+
for (const enParameterName of enParameterNames) {
|
|
72
|
+
if (!lngParameterNames.includes(enParameterName)) {
|
|
73
|
+
throw new Error(`Missing parameter ${enParameterName} for key ${enFullKey} in ${lng} resources`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
for (const lngParameterName of lngParameterNames) {
|
|
77
|
+
if (!enParameterNames.includes(lngParameterName)) {
|
|
78
|
+
throw new Error(`Extraneous parameter ${lngParameterName} for key ${enFullKey} in ${lng} resources`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
46
81
|
}
|
|
47
|
-
if (enValueType === "object") {
|
|
82
|
+
else if (enValueType === "object") {
|
|
48
83
|
i18nAssertValidResources(enValue, lng, lngValue, `${parent === undefined ? "" : `${parent}.`}${enKey}`);
|
|
49
84
|
}
|
|
50
85
|
// Locale falls back to raw language so ignore if missing.
|
|
51
86
|
}
|
|
52
87
|
else if (!isLocale) {
|
|
53
|
-
throw new Error(`Missing key ${
|
|
88
|
+
throw new Error(`Missing key ${enFullKey} in ${lng} resources`);
|
|
54
89
|
}
|
|
55
90
|
}
|
|
56
91
|
for (const [lngKey] of lngResourcesMap) {
|
package/dist/locale/i18n.js.map
CHANGED
|
@@ -1 +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,CAAC,MAAM,gBAAgB,GAAG;IAC5B;;OAEG;IACH,GAAG,EAAE,CAAC;IAEN;;OAEG;IACH,MAAM,EAAE,CAAC;IAET;;OAEG;IACH,OAAO,EAAE,CAAC;CACJ,CAAC;AAOX;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,wBAAwB,CAAC,WAAmB,EAAE,GAAW,EAAE,YAAoB,EAAE,MAAe;IAC5G,MAAM,cAAc,GAAG,IAAI,GAAG,CAAiB,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC;IAC5E,MAAM,eAAe,GAAG,IAAI,GAAG,CAAiB,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC;IAE9E,MAAM,QAAQ,GAAG,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;IAEnC,KAAK,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,IAAI,cAAc,EAAE,CAAC;QAC5C,MAAM,QAAQ,GAAG,eAAe,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QAE5C,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YACzB,MAAM,WAAW,GAAG,OAAO,OAAO,CAAC;YACnC,MAAM,YAAY,GAAG,OAAO,QAAQ,CAAC;YAErC,IAAI,YAAY,KAAK,WAAW,EAAE,CAAC;gBAC/B,MAAM,IAAI,KAAK,CAAC,
|
|
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,CAAC,MAAM,gBAAgB,GAA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AG,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,gBAAgB,CAAC,GAAG;gBACrB,gGAAgG;gBAChG,qFAAqF;gBACrF,MAAM,GAAG,0BAA+D,CAAC;gBACzE,MAAM;YAEV,KAAK,gBAAgB,CAAC,OAAO;gBACzB,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
package/src/locale/i18n.ts
CHANGED
|
@@ -5,8 +5,8 @@ import I18nextCLILanguageDetector from "i18next-cli-language-detector";
|
|
|
5
5
|
/**
|
|
6
6
|
* Locale strings type for generic manipulation.
|
|
7
7
|
*/
|
|
8
|
-
export interface
|
|
9
|
-
[key: string]:
|
|
8
|
+
export interface LocaleResources {
|
|
9
|
+
[key: string]: LocaleResources | string;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -34,6 +34,29 @@ export const I18nEnvironments = {
|
|
|
34
34
|
*/
|
|
35
35
|
export type I18nEnvironment = typeof I18nEnvironments[keyof typeof I18nEnvironments];
|
|
36
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Parse parameter names in a resource string.
|
|
39
|
+
*
|
|
40
|
+
* @param s
|
|
41
|
+
* Resource string.
|
|
42
|
+
*
|
|
43
|
+
* @returns
|
|
44
|
+
* Array of parameter names.
|
|
45
|
+
*/
|
|
46
|
+
function parseParameterNames(s: string): string[] {
|
|
47
|
+
const parameterRegExp = /\{\{.+?}}/g;
|
|
48
|
+
|
|
49
|
+
const parameterNames: string[] = [];
|
|
50
|
+
|
|
51
|
+
let match: RegExpExecArray | null;
|
|
52
|
+
|
|
53
|
+
while ((match = parameterRegExp.exec(s)) !== null) {
|
|
54
|
+
parameterNames.push(match[1]);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return parameterNames;
|
|
58
|
+
}
|
|
59
|
+
|
|
37
60
|
/**
|
|
38
61
|
* Assert that language resources are a type match for English (default) resources.
|
|
39
62
|
*
|
|
@@ -56,6 +79,8 @@ export function i18nAssertValidResources(enResources: object, lng: string, lngRe
|
|
|
56
79
|
const isLocale = lng.includes("-");
|
|
57
80
|
|
|
58
81
|
for (const [enKey, enValue] of enResourcesMap) {
|
|
82
|
+
const enFullKey = `${parent === undefined ? "" : `${parent}.`}${enKey}`;
|
|
83
|
+
|
|
59
84
|
const lngValue = lngResourcesMap.get(enKey);
|
|
60
85
|
|
|
61
86
|
if (lngValue !== undefined) {
|
|
@@ -63,15 +88,33 @@ export function i18nAssertValidResources(enResources: object, lng: string, lngRe
|
|
|
63
88
|
const lngValueType = typeof lngValue;
|
|
64
89
|
|
|
65
90
|
if (lngValueType !== enValueType) {
|
|
66
|
-
throw new Error(`
|
|
91
|
+
throw new Error(`Mismatched value type ${lngValueType} for key ${enFullKey} in ${lng} resources (expected ${enValueType})`);
|
|
67
92
|
}
|
|
68
93
|
|
|
69
|
-
if (enValueType === "
|
|
94
|
+
if (enValueType === "string") {
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Value is known to be string.
|
|
96
|
+
const enParameterNames = parseParameterNames(enValue as unknown as string);
|
|
97
|
+
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Value is known to be string.
|
|
99
|
+
const lngParameterNames = parseParameterNames(lngValue as unknown as string);
|
|
100
|
+
|
|
101
|
+
for (const enParameterName of enParameterNames) {
|
|
102
|
+
if (!lngParameterNames.includes(enParameterName)) {
|
|
103
|
+
throw new Error(`Missing parameter ${enParameterName} for key ${enFullKey} in ${lng} resources`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
for (const lngParameterName of lngParameterNames) {
|
|
108
|
+
if (!enParameterNames.includes(lngParameterName)) {
|
|
109
|
+
throw new Error(`Extraneous parameter ${lngParameterName} for key ${enFullKey} in ${lng} resources`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
} else if (enValueType === "object") {
|
|
70
113
|
i18nAssertValidResources(enValue, lng, lngValue, `${parent === undefined ? "" : `${parent}.`}${enKey}`);
|
|
71
114
|
}
|
|
72
115
|
// Locale falls back to raw language so ignore if missing.
|
|
73
116
|
} else if (!isLocale) {
|
|
74
|
-
throw new Error(`Missing key ${
|
|
117
|
+
throw new Error(`Missing key ${enFullKey} in ${lng} resources`);
|
|
75
118
|
}
|
|
76
119
|
}
|
|
77
120
|
|