@rdyl/react-i18n-connect 0.1.3 → 0.2.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/CHANGELOG.md CHANGED
@@ -1,4 +1,4 @@
1
- ## 0.1.2 (2025-09-25)
1
+ ## 0.1.3 (2025-09-25)
2
2
 
3
3
 
4
4
 
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 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, onLoadLocales = _a.onLoadLocales, onLoadInitial = _a.onLoadInitial;
56
- var _d = (0, react_1.useState)(!onLoadInitial), ready = _d[0], setReady = _d[1];
57
- var _e = (0, react_1.useState)(initial), lang = _e[0], _setLang = _e[1];
58
- var _f = (0, react_1.useState)({}), loaded = _f[0], setLoaded = _f[1];
59
- var records = (0, react_1.useMemo)(function () {
60
- // 异步加载
61
- if (onLoadLocales) {
62
- return loaded;
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 children = _a.children, initial = _a.initial, 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
- if (onLoadLocales) {
87
- setLoaded({});
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 (onLoadInitial) {
93
- onLoadInitial()
94
- .then(setLang)
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
- __locales__: __locales__,
102
- records: records,
99
+ locales: locales,
103
100
  lang: lang,
104
101
  t: t,
105
102
  setLang: setLang,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rdyl/react-i18n-connect",
3
- "version": "0.1.3",
3
+ "version": "0.2.0",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "types/index.d.ts",
package/types/index.d.ts CHANGED
@@ -2,8 +2,9 @@ import { FC, PropsWithChildren } from "react";
2
2
  import { GenI18nTokenFn, I18nConfig, I18nConnectValue, I18nTokenValue } from "./utils";
3
3
  declare const I18nConnect: FC<PropsWithChildren<I18nConnectProps>>;
4
4
  export type I18nConnectProps = Partial<I18nConfig> & {
5
- onLoadInitial?(): Promise<string>;
6
- onLoadLocales?(lang: string): Promise<Record<string, I18nTokenValue>>;
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
@@ -6,9 +6,8 @@ export interface I18nConfig {
6
6
  }
7
7
  export type GenI18nTokenFn = (k: string, o?: Record<string, unknown>) => string;
8
8
  export interface I18nConnectValue {
9
- __locales__: Record<string, Record<string, I18nTokenValue>>;
10
9
  lang: string;
11
- records: Record<string, I18nTokenValue>;
10
+ locales: Record<string, I18nTokenValue>;
12
11
  setLang(name: string): void;
13
12
  t: GenI18nTokenFn;
14
13
  }