@rdyl/react-i18n-connect 0.1.3 → 0.2.1
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 +22 -25
- package/package.json +1 -1
- package/types/index.d.ts +5 -4
- package/types/utils.d.ts +1 -6
package/CHANGELOG.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -52,20 +52,15 @@ var react_1 = __importStar(require("react"));
|
|
|
52
52
|
var utils_1 = require("./utils");
|
|
53
53
|
var I18nCtx = (0, react_1.createContext)({});
|
|
54
54
|
var I18nConnect = function (_a) {
|
|
55
|
-
var
|
|
56
|
-
var
|
|
57
|
-
var
|
|
58
|
-
var
|
|
59
|
-
var
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
var current = __locales__[lang] || {};
|
|
65
|
-
var initLocales = __locales__[initial] || {};
|
|
66
|
-
return __assign(__assign({}, initLocales), current);
|
|
67
|
-
}, [lang, loaded, initial, onLoadLocales]);
|
|
68
|
-
var t = (0, react_1.useCallback)((0, utils_1.ParserI18nFn)(records), [records]);
|
|
55
|
+
var initial = _a.initial, children = _a.children, defaultLang = _a.defaultLang, onAfterChange = _a.onAfterChange, onBeforeChange = _a.onBeforeChange, getLocales = _a.getLocales;
|
|
56
|
+
var _b = (0, react_1.useState)(false), ready = _b[0], setReady = _b[1];
|
|
57
|
+
var _c = (0, react_1.useState)(defaultLang), lang = _c[0], _setLang = _c[1];
|
|
58
|
+
var _d = (0, react_1.useState)({}), defaultLocales = _d[0], setDefaultLocales = _d[1];
|
|
59
|
+
var _e = (0, react_1.useState)({}), loaded = _e[0], setLoaded = _e[1];
|
|
60
|
+
var locales = (0, react_1.useMemo)(function () {
|
|
61
|
+
return __assign(__assign({}, defaultLocales), loaded);
|
|
62
|
+
}, [defaultLocales, loaded]);
|
|
63
|
+
var t = (0, react_1.useCallback)((0, utils_1.ParserI18nFn)(locales), [locales]);
|
|
69
64
|
var setLang = (0, react_1.useCallback)(function (e) {
|
|
70
65
|
var cb = function () {
|
|
71
66
|
_setLang(e);
|
|
@@ -83,23 +78,25 @@ var I18nConnect = function (_a) {
|
|
|
83
78
|
}
|
|
84
79
|
}, [onBeforeChange, onAfterChange, t]);
|
|
85
80
|
(0, react_1.useEffect)(function () {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
onLoadLocales(lang).then(setLoaded);
|
|
89
|
-
}
|
|
90
|
-
}, [lang, onLoadLocales]);
|
|
81
|
+
Promise.resolve(getLocales(defaultLang)).then(setDefaultLocales);
|
|
82
|
+
}, [defaultLang]);
|
|
91
83
|
(0, react_1.useEffect)(function () {
|
|
92
|
-
if (
|
|
93
|
-
|
|
94
|
-
.then(
|
|
84
|
+
if (initial) {
|
|
85
|
+
initial()
|
|
86
|
+
.then(function (l) {
|
|
87
|
+
setLang(l);
|
|
88
|
+
Promise.resolve(getLocales(l)).then(setLoaded);
|
|
89
|
+
})
|
|
95
90
|
.finally(function () {
|
|
96
91
|
setReady(true);
|
|
97
92
|
});
|
|
98
93
|
}
|
|
99
|
-
|
|
94
|
+
else {
|
|
95
|
+
setReady(true);
|
|
96
|
+
}
|
|
97
|
+
}, [initial]);
|
|
100
98
|
return (react_1.default.createElement(I18nCtx.Provider, { value: {
|
|
101
|
-
|
|
102
|
-
records: records,
|
|
99
|
+
locales: locales,
|
|
103
100
|
lang: lang,
|
|
104
101
|
t: t,
|
|
105
102
|
setLang: setLang,
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { FC, PropsWithChildren } from "react";
|
|
2
|
-
import { GenI18nTokenFn,
|
|
2
|
+
import { GenI18nTokenFn, I18nConnectValue, I18nTokenValue } from "./utils";
|
|
3
3
|
declare const I18nConnect: FC<PropsWithChildren<I18nConnectProps>>;
|
|
4
|
-
export type I18nConnectProps =
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
export type I18nConnectProps = {
|
|
5
|
+
defaultLang: string;
|
|
6
|
+
initial?(): Promise<string>;
|
|
7
|
+
getLocales(lang: string): Record<string, I18nTokenValue> | Promise<Record<string, I18nTokenValue>>;
|
|
7
8
|
onAfterChange?(lang: string, t: GenI18nTokenFn): void;
|
|
8
9
|
onBeforeChange?(lang: string): boolean | Promise<boolean>;
|
|
9
10
|
};
|
package/types/utils.d.ts
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
export declare function ParserI18nFn(data: Record<string, I18nTokenValue>): (k: string, named?: Record<string, unknown>) => string;
|
|
2
2
|
export type I18nTokenValue = string | number | boolean;
|
|
3
|
-
export interface I18nConfig {
|
|
4
|
-
locales: Record<string, Record<string, I18nTokenValue>>;
|
|
5
|
-
initial: string;
|
|
6
|
-
}
|
|
7
3
|
export type GenI18nTokenFn = (k: string, o?: Record<string, unknown>) => string;
|
|
8
4
|
export interface I18nConnectValue {
|
|
9
|
-
__locales__: Record<string, Record<string, I18nTokenValue>>;
|
|
10
5
|
lang: string;
|
|
11
|
-
|
|
6
|
+
locales: Record<string, I18nTokenValue>;
|
|
12
7
|
setLang(name: string): void;
|
|
13
8
|
t: GenI18nTokenFn;
|
|
14
9
|
}
|