@rdyl/react-i18n-connect 0.0.8 → 0.1.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.0.8 (2025-06-09)
1
+ # 0.1.0 (2025-09-04)
2
2
 
3
3
 
4
4
 
package/README.md CHANGED
@@ -8,13 +8,16 @@ $ yarn add @rdyl/react-i18n-connect
8
8
 
9
9
  ## Getting started
10
10
 
11
- 1. 配置tsconfig.json
12
- ```json
13
- {
14
- "compilerOptions": {
15
- "types": [
16
- "@rdyl/react-i18n-connect/types/type.d.ts"
17
- ],
18
- },
19
- }
11
+ 1. App 入口文件添加
12
+ ```tsx
13
+ <I18nConnect initial={"zh-CN"} locales={{...}}>
14
+ ...
15
+ </I18nConnect>
16
+ ```
17
+
18
+ 2. 使用
19
+ ```tsx
20
+ const {t} = useI18n()
21
+
22
+ t('key')
20
23
  ```
package/dist/index.js CHANGED
@@ -32,64 +32,68 @@ var __importStar = (this && this.__importStar) || (function () {
32
32
  return result;
33
33
  };
34
34
  })();
35
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
36
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
37
+ };
35
38
  Object.defineProperty(exports, "__esModule", { value: true });
36
39
  exports.useI18n = void 0;
37
40
  var react_1 = __importStar(require("react"));
38
41
  var utils_1 = require("./utils");
39
42
  var I18nCtx = (0, react_1.createContext)({});
40
43
  var I18nConnect = function (_a) {
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__]);
44
+ 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;
45
+ var _d = (0, react_1.useState)(!onLoadInitial), ready = _d[0], setReady = _d[1];
46
+ var _e = (0, react_1.useState)(initial), lang = _e[0], _setLang = _e[1];
47
+ var _f = (0, react_1.useState)({}), loaded = _f[0], setLoaded = _f[1];
52
48
  var records = (0, react_1.useMemo)(function () {
53
49
  // 异步加载
54
- if (onLoad) {
50
+ if (onLoadLocales) {
55
51
  return loaded;
56
52
  }
57
53
  var current = __locales__[lang] || {};
58
54
  return current;
59
- }, [lang, loaded, onLoad]);
55
+ }, [lang, loaded, onLoadLocales]);
60
56
  var t = (0, react_1.useCallback)((0, utils_1.ParserI18nFn)(records), [records]);
61
57
  var setLang = (0, react_1.useCallback)(function (e) {
62
58
  var cb = function () {
63
59
  _setLang(e);
64
- onAfterChange && onAfterChange(e);
60
+ onAfterChange && onAfterChange(e, t);
65
61
  };
66
62
  if (onBeforeChange) {
67
- if (!!onBeforeChange(e)) {
68
- cb();
69
- }
63
+ Promise.resolve(onBeforeChange(e)).then(function (res) {
64
+ if (res) {
65
+ cb();
66
+ }
67
+ });
70
68
  }
71
69
  else {
72
70
  cb();
73
71
  }
74
- }, [onBeforeChange, onAfterChange]);
72
+ }, [onBeforeChange, onAfterChange, t]);
75
73
  (0, react_1.useEffect)(function () {
76
- if (onLoad) {
74
+ if (onLoadLocales) {
77
75
  setLoaded({});
78
- onLoad(lang).then(setLoaded);
76
+ onLoadLocales(lang).then(setLoaded);
79
77
  }
80
- }, [lang, onLoad]);
78
+ }, [lang, onLoadLocales]);
81
79
  (0, react_1.useEffect)(function () {
82
- window.$t = t;
83
- }, [t]);
80
+ if (onLoadInitial) {
81
+ onLoadInitial()
82
+ .then(setLang)
83
+ .finally(function () {
84
+ setReady(true);
85
+ });
86
+ }
87
+ }, []);
84
88
  return (react_1.default.createElement(I18nCtx.Provider, { value: {
85
89
  __locales__: __locales__,
86
90
  records: records,
87
- options: options,
88
91
  lang: lang,
89
92
  t: t,
90
93
  setLang: setLang,
91
- } }, children));
94
+ } }, ready && children));
92
95
  };
93
96
  var useI18n = function () { return (0, react_1.useContext)(I18nCtx); };
94
97
  exports.useI18n = useI18n;
98
+ __exportStar(require("./utils"), exports);
95
99
  exports.default = I18nConnect;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rdyl/react-i18n-connect",
3
- "version": "0.0.8",
3
+ "version": "0.1.0",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "types/index.d.ts",
package/types/index.d.ts CHANGED
@@ -1,17 +1,12 @@
1
1
  import { FC, PropsWithChildren } from "react";
2
+ import { GenI18nTokenFn, I18nConfig, I18nConnectValue, I18nTokenValue } from "./utils";
2
3
  declare const I18nConnect: FC<PropsWithChildren<I18nConnectProps>>;
3
- export interface I18nConnectValue {
4
- __locales__: Record<string, Record<string, TRValue>>;
5
- lang: string;
6
- options: I18nLangOption[];
7
- records: Record<string, TRValue>;
8
- setLang(name: string): void;
9
- t: TR;
10
- }
11
- type I18nConnectProps = Partial<I18nConfig> & {
12
- onLoad?(t: string): Promise<Record<string, TRValue>>;
13
- onAfterChange?(t: string): void;
14
- onBeforeChange?(t: string): unknown;
4
+ export type I18nConnectProps = Partial<I18nConfig> & {
5
+ onLoadInitial?(): Promise<string>;
6
+ onLoadLocales?(lang: string): Promise<Record<string, I18nTokenValue>>;
7
+ onAfterChange?(lang: string, t: GenI18nTokenFn): void;
8
+ onBeforeChange?(lang: string): boolean | Promise<boolean>;
15
9
  };
16
10
  export declare const useI18n: () => I18nConnectValue;
11
+ export * from "./utils";
17
12
  export default I18nConnect;
package/types/type.d.ts CHANGED
@@ -4,11 +4,6 @@ interface I18nConfig {
4
4
  initial: string;
5
5
  }
6
6
 
7
- interface I18nLangOption {
8
- name: string;
9
- label: string;
10
- }
11
-
12
7
  type TRValue = string | number | boolean;
13
8
 
14
9
  type TR = (k: string, o?: Record<string, unknown>) => string;
package/types/utils.d.ts CHANGED
@@ -1 +1,14 @@
1
- export declare function ParserI18nFn(data: Record<string, TRValue>): (k: string, named?: Record<string, unknown>) => string;
1
+ export declare function ParserI18nFn(data: Record<string, I18nTokenValue>): (k: string, named?: Record<string, unknown>) => string;
2
+ export type I18nTokenValue = string | number | boolean;
3
+ export interface I18nConfig {
4
+ locales: Record<string, Record<string, I18nTokenValue>>;
5
+ initial: string;
6
+ }
7
+ export type GenI18nTokenFn = (k: string, o?: Record<string, unknown>) => string;
8
+ export interface I18nConnectValue {
9
+ __locales__: Record<string, Record<string, I18nTokenValue>>;
10
+ lang: string;
11
+ records: Record<string, I18nTokenValue>;
12
+ setLang(name: string): void;
13
+ t: GenI18nTokenFn;
14
+ }