@rdyl/react-i18n-connect 0.0.1 → 0.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/CHANGELOG.md +1 -1
- package/dist/index.js +32 -33
- package/package.json +1 -1
- package/types/index.d.ts +6 -12
- package/types/type.d.ts +7 -7
package/CHANGELOG.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -33,28 +33,45 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.useI18n =
|
|
36
|
+
exports.useI18n = void 0;
|
|
37
37
|
var react_1 = __importStar(require("react"));
|
|
38
38
|
var utils_1 = require("./utils");
|
|
39
39
|
var I18nCtx = (0, react_1.createContext)({});
|
|
40
40
|
var I18nConnect = function (_a) {
|
|
41
|
-
var children = _a.children,
|
|
42
|
-
var
|
|
43
|
-
var
|
|
44
|
-
var
|
|
45
|
-
|
|
46
|
-
|
|
41
|
+
var children = _a.children, _b = _a.initial, initial = _b === void 0 ? "" : _b, _c = _a.locales, __locales__ = _c === void 0 ? {} : _c, onAfterChange = _a.onAfterChange, onBeforeChange = _a.onBeforeChange, onLoad = _a.onLoad;
|
|
42
|
+
var _d = (0, react_1.useState)(initial), lang = _d[0], _setLang = _d[1];
|
|
43
|
+
var _e = (0, react_1.useState)({}), loaded = _e[0], setLoaded = _e[1];
|
|
44
|
+
var options = (0, react_1.useMemo)(function () {
|
|
45
|
+
return Object.keys(__locales__).map(function (name) {
|
|
46
|
+
return {
|
|
47
|
+
name: name,
|
|
48
|
+
label: __locales__[name]["$"] || name,
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
}, [__locales__]);
|
|
52
|
+
var records = (0, react_1.useMemo)(function () {
|
|
47
53
|
// 异步加载
|
|
48
54
|
if (onLoad) {
|
|
49
55
|
return loaded;
|
|
50
56
|
}
|
|
51
|
-
|
|
57
|
+
var current = __locales__[lang] || {};
|
|
58
|
+
return current;
|
|
52
59
|
}, [lang, loaded, onLoad]);
|
|
53
|
-
var t = (0, react_1.useCallback)((0, utils_1.ParserI18nFn)(
|
|
60
|
+
var t = (0, react_1.useCallback)((0, utils_1.ParserI18nFn)(records), [records]);
|
|
54
61
|
var setLang = (0, react_1.useCallback)(function (e) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
62
|
+
var cb = function () {
|
|
63
|
+
_setLang(e);
|
|
64
|
+
onAfterChange && onAfterChange(e);
|
|
65
|
+
};
|
|
66
|
+
if (onBeforeChange) {
|
|
67
|
+
if (!!onBeforeChange(e)) {
|
|
68
|
+
cb();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
cb();
|
|
73
|
+
}
|
|
74
|
+
}, [onBeforeChange, onAfterChange]);
|
|
58
75
|
(0, react_1.useEffect)(function () {
|
|
59
76
|
if (onLoad) {
|
|
60
77
|
setLoaded({});
|
|
@@ -65,32 +82,14 @@ var I18nConnect = function (_a) {
|
|
|
65
82
|
window.$t = t;
|
|
66
83
|
}, [t]);
|
|
67
84
|
return (react_1.default.createElement(I18nCtx.Provider, { value: {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
85
|
+
__locales__: __locales__,
|
|
86
|
+
records: records,
|
|
87
|
+
options: options,
|
|
71
88
|
lang: lang,
|
|
72
89
|
t: t,
|
|
73
90
|
setLang: setLang,
|
|
74
91
|
} }, children));
|
|
75
92
|
};
|
|
76
|
-
exports.LangToken = {
|
|
77
|
-
name: "LANG-TOKEN",
|
|
78
|
-
value: "",
|
|
79
|
-
get: function (t) {
|
|
80
|
-
if (t === void 0) { t = ""; }
|
|
81
|
-
var lang = localStorage.getItem(this.name) || t;
|
|
82
|
-
exports.LangToken.value = lang;
|
|
83
|
-
return lang;
|
|
84
|
-
},
|
|
85
|
-
set: function (v) {
|
|
86
|
-
exports.LangToken.value = v;
|
|
87
|
-
localStorage.setItem(this.name, v);
|
|
88
|
-
},
|
|
89
|
-
remove: function () {
|
|
90
|
-
exports.LangToken.value = "";
|
|
91
|
-
localStorage.removeItem(this.name);
|
|
92
|
-
},
|
|
93
|
-
};
|
|
94
93
|
var useI18n = function () { return (0, react_1.useContext)(I18nCtx); };
|
|
95
94
|
exports.useI18n = useI18n;
|
|
96
95
|
exports.default = I18nConnect;
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -1,23 +1,17 @@
|
|
|
1
1
|
import { FC, PropsWithChildren } from "react";
|
|
2
2
|
declare const I18nConnect: FC<PropsWithChildren<I18nConnectProps>>;
|
|
3
3
|
export interface I18nConnectValue {
|
|
4
|
-
|
|
4
|
+
__locales__: Record<string, Record<string, TRValue>>;
|
|
5
5
|
lang: string;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
options: I18nLangOption[];
|
|
7
|
+
records: Record<string, TRValue>;
|
|
8
8
|
setLang(name: string): void;
|
|
9
9
|
t: TR;
|
|
10
10
|
}
|
|
11
|
-
type I18nConnectProps = {
|
|
12
|
-
i18nConfig: I18nConfig;
|
|
11
|
+
type I18nConnectProps = Partial<I18nConfig> & {
|
|
13
12
|
onLoad?(t: string): Promise<Record<string, TRValue>>;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
name: string;
|
|
17
|
-
value: string;
|
|
18
|
-
get(t?: string): string;
|
|
19
|
-
set(v: string): void;
|
|
20
|
-
remove(): void;
|
|
13
|
+
onAfterChange?(t: string): void;
|
|
14
|
+
onBeforeChange?(t: string): unknown;
|
|
21
15
|
};
|
|
22
16
|
export declare const useI18n: () => I18nConnectValue;
|
|
23
17
|
export default I18nConnect;
|
package/types/type.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
/** @i18n */
|
|
2
2
|
interface I18nConfig {
|
|
3
|
-
locales: Record<string,
|
|
4
|
-
|
|
3
|
+
locales: Record<Record<string, TRValue>>;
|
|
4
|
+
initial: string;
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
title: string;
|
|
11
|
-
messages?: Record<string, TRValue>;
|
|
7
|
+
interface I18nLangOption {
|
|
8
|
+
name: string;
|
|
9
|
+
label: string;
|
|
12
10
|
}
|
|
13
11
|
|
|
12
|
+
type TRValue = string | number | boolean;
|
|
13
|
+
|
|
14
14
|
type TR = (k: string, o?: Record<string, unknown>) => string;
|
|
15
15
|
|
|
16
16
|
declare var $t: TR;
|