@rdyl/react-i18n-connect 0.0.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 ADDED
@@ -0,0 +1,4 @@
1
+ ## 0.0.1 (2025-01-10)
2
+
3
+
4
+
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ ## Installation
2
+
3
+ ```bash
4
+ $ npm install @rdyl/react-i18n-connect
5
+ # or
6
+ $ yarn add @rdyl/react-i18n-connect
7
+ ```
8
+
9
+ ## Getting started
10
+
11
+ 1. 配置tsconfig.json
12
+ ```json
13
+ {
14
+ "compilerOptions": {
15
+ "types": [
16
+ "@rdyl/react-i18n-connect/types/type.d.ts"
17
+ ],
18
+ },
19
+ }
20
+ ```
package/dist/index.js ADDED
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.useI18n = exports.LangToken = void 0;
37
+ var react_1 = __importStar(require("react"));
38
+ var utils_1 = require("./utils");
39
+ var I18nCtx = (0, react_1.createContext)({});
40
+ var I18nConnect = function (_a) {
41
+ var children = _a.children, i18nConfig = _a.i18nConfig, onLoad = _a.onLoad;
42
+ var locales = i18nConfig.locales, initialLang = i18nConfig.initialLang;
43
+ var _b = (0, react_1.useState)(exports.LangToken.get(initialLang)), lang = _b[0], _setLang = _b[1];
44
+ var _c = (0, react_1.useState)({}), loaded = _c[0], setLoaded = _c[1];
45
+ var title = (0, react_1.useMemo)(function () { return (locales[lang] ? locales[lang].title : ""); }, [lang]);
46
+ var dataSource = (0, react_1.useMemo)(function () {
47
+ // 异步加载
48
+ if (onLoad) {
49
+ return loaded;
50
+ }
51
+ return locales[lang] ? locales[lang].messages || {} : {};
52
+ }, [lang, loaded, onLoad]);
53
+ var t = (0, react_1.useCallback)((0, utils_1.ParserI18nFn)(dataSource), [dataSource]);
54
+ var setLang = (0, react_1.useCallback)(function (e) {
55
+ exports.LangToken.set(e);
56
+ _setLang(e);
57
+ }, []);
58
+ (0, react_1.useEffect)(function () {
59
+ if (onLoad) {
60
+ setLoaded({});
61
+ onLoad(lang).then(setLoaded);
62
+ }
63
+ }, [lang, onLoad]);
64
+ (0, react_1.useEffect)(function () {
65
+ window.$t = t;
66
+ }, [t]);
67
+ return (react_1.default.createElement(I18nCtx.Provider, { value: {
68
+ locales: locales,
69
+ dataSource: dataSource,
70
+ title: title,
71
+ lang: lang,
72
+ t: t,
73
+ setLang: setLang,
74
+ } }, children));
75
+ };
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
+ var useI18n = function () { return (0, react_1.useContext)(I18nCtx); };
95
+ exports.useI18n = useI18n;
96
+ exports.default = I18nConnect;
package/dist/utils.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ParserI18nFn = ParserI18nFn;
4
+ function ParserI18nFn(data) {
5
+ return function (k, named) {
6
+ var text = data[k];
7
+ if (!text) {
8
+ return k;
9
+ }
10
+ if (named) {
11
+ for (var key in named) {
12
+ var varRegExp = new RegExp("{".concat(key, "}"), "g");
13
+ text = "".concat(text).replace(varRegExp, "".concat(named[key]));
14
+ }
15
+ }
16
+ return text;
17
+ };
18
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@rdyl/react-i18n-connect",
3
+ "version": "0.0.1",
4
+ "license": "MIT",
5
+ "main": "dist/index.js",
6
+ "types": "types/index.d.ts",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "files": [
11
+ "dist",
12
+ "types",
13
+ "CHANGELOG.md",
14
+ "README.md"
15
+ ],
16
+ "scripts": {
17
+ "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
18
+ "build": "tsc && copyfiles -u 1 src/**/*.d.ts src/*.d.ts types && npm run changelog && git add CHANGELOG.md"
19
+ },
20
+ "devDependencies": {
21
+ "@types/node": "^22.10.1",
22
+ "@types/react": "^18.3.12",
23
+ "copyfiles": "^2.4.1",
24
+ "react": "^18.3.1",
25
+ "typescript": "^5.6.3",
26
+ "@commitlint/cli": "^19.6.1",
27
+ "@commitlint/config-conventional": "^19.6.0",
28
+ "conventional-changelog-cli": "^5.0.0"
29
+ },
30
+ "peerDependencies": {
31
+ "react": ">=18.0.0"
32
+ }
33
+ }
@@ -0,0 +1,23 @@
1
+ import { FC, PropsWithChildren } from "react";
2
+ declare const I18nConnect: FC<PropsWithChildren<I18nConnectProps>>;
3
+ export interface I18nConnectValue {
4
+ title: string;
5
+ lang: string;
6
+ dataSource: Record<string, TRValue>;
7
+ locales: Record<string, I18nLangEntry>;
8
+ setLang(name: string): void;
9
+ t: TR;
10
+ }
11
+ type I18nConnectProps = {
12
+ i18nConfig: I18nConfig;
13
+ onLoad?(t: string): Promise<Record<string, TRValue>>;
14
+ };
15
+ export declare const LangToken: {
16
+ name: string;
17
+ value: string;
18
+ get(t?: string): string;
19
+ set(v: string): void;
20
+ remove(): void;
21
+ };
22
+ export declare const useI18n: () => I18nConnectValue;
23
+ export default I18nConnect;
@@ -0,0 +1,16 @@
1
+ /** @i18n */
2
+ interface I18nConfig {
3
+ locales: Record<string, I18nLangEntry>;
4
+ initialLang: string;
5
+ }
6
+
7
+ type TRValue = string | number | boolean;
8
+
9
+ interface I18nLangEntry {
10
+ title: string;
11
+ messages?: Record<string, TRValue>;
12
+ }
13
+
14
+ type TR = (k: string, o?: Record<string, unknown>) => string;
15
+
16
+ declare var $t: TR;
@@ -0,0 +1 @@
1
+ export declare function ParserI18nFn(data: Record<string, TRValue>): (k: string, named?: Record<string, unknown>) => string;