@hairy/react-i18-lib 1.26.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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-PRESENT Hairyf <https://github.com/antfu>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # @hairy/react-lib
2
+
3
+ [![npm version][npm-version-src]][npm-version-href]
4
+ [![npm downloads][npm-downloads-src]][npm-downloads-href]
5
+ [![bundle][bundle-src]][bundle-href]
6
+ [![JSDocs][jsdocs-src]][jsdocs-href]
7
+ [![License][license-src]][license-href]
8
+
9
+ ## Install
10
+
11
+ ```
12
+ ni @hairy/react-lib
13
+ ```
14
+
15
+ ## CDN
16
+
17
+ ```html
18
+ <script src="https://unpkg.com/@hairy/react-lib"></script>
19
+ ```
20
+
21
+ ## License
22
+
23
+ [MIT](./LICENSE) License © [Hairyf](https://github.com/hairyf)
24
+
25
+ <!-- Badges -->
26
+
27
+ [npm-version-src]: https://img.shields.io/npm/v/@hairy/react-lib?style=flat&colorA=080f12&colorB=1fa669
28
+ [npm-version-href]: https://npmjs.com/package/@hairy/react-lib
29
+ [npm-downloads-src]: https://img.shields.io/npm/dm/@hairy/react-lib?style=flat&colorA=080f12&colorB=1fa669
30
+ [npm-downloads-href]: https://npmjs.com/package/@hairy/react-lib
31
+ [bundle-src]: https://img.shields.io/bundlephobia/minzip/@hairy/react-lib?style=flat&colorA=080f12&colorB=1fa669&label=minzip
32
+ [bundle-href]: https://bundlephobia.com/result?p=@hairy/react-lib
33
+ [license-src]: https://img.shields.io/github/license/hairyf/hairylib.svg?style=flat&colorA=080f12&colorB=1fa669
34
+ [license-href]: https://github.com/hairyf/hairylib/blob/main/LICENSE
35
+ [jsdocs-src]: https://img.shields.io/badge/jsdocs-reference-080f12?style=flat&colorA=080f12&colorB=1fa669
36
+ [jsdocs-href]: https://www.jsdocs.io/package/@hairy/react-lib
package/dist/index.cjs ADDED
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ Trans: () => Trans
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+ var import_html_parse_stringify = __toESM(require("html-parse-stringify"), 1);
37
+ var import_react = require("react");
38
+ var import_react_i18next = require("react-i18next");
39
+ function Trans({ i18nKey, ...additionalProps }) {
40
+ const translation = (0, import_react_i18next.useTranslation)().t(i18nKey, additionalProps);
41
+ return renderNodes(import_html_parse_stringify.default.parse(translation), additionalProps);
42
+ }
43
+ function renderNodes(tokens, values) {
44
+ let index = 0;
45
+ return tokens.map((token) => {
46
+ if (token.type === "text")
47
+ return token.content;
48
+ index++;
49
+ const props = { ...token.attrs, key: index };
50
+ return token.voidElement ? values[token.name] ? (0, import_react.createElement)("span", { key: index }, values[token.name]) : (0, import_react.createElement)(token.name, props) : (0, import_react.createElement)(token.name, props, renderNodes(token.children, {}));
51
+ });
52
+ }
53
+ // Annotate the CommonJS export names for ESM import in node:
54
+ 0 && (module.exports = {
55
+ Trans
56
+ });
@@ -0,0 +1,9 @@
1
+ import { ReactNode } from 'react';
2
+
3
+ interface TransProps {
4
+ i18nKey: string;
5
+ [key: string]: ReactNode;
6
+ }
7
+ declare function Trans({ i18nKey, ...additionalProps }: TransProps): ReactNode[];
8
+
9
+ export { Trans, type TransProps };
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ var LibReact = (() => {
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __commonJS = (cb, mod) => function __require() {
10
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
11
+ };
12
+ var __export = (target, all) => {
13
+ for (var name in all)
14
+ __defProp(target, name, { get: all[name], enumerable: true });
15
+ };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from))
19
+ if (!__hasOwnProp.call(to, key) && key !== except)
20
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
+ }
22
+ return to;
23
+ };
24
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
25
+ // If the importer is in node compatibility mode or this is not an ESM
26
+ // file that has been converted to a CommonJS file using a Babel-
27
+ // compatible transform (i.e. "__esModule" has not been set), then set
28
+ // "default" to the CommonJS "module.exports" for node compatibility.
29
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
30
+ mod
31
+ ));
32
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
33
+
34
+ // ../../node_modules/.pnpm/void-elements@3.1.0/node_modules/void-elements/index.js
35
+ var require_void_elements = __commonJS({
36
+ "../../node_modules/.pnpm/void-elements@3.1.0/node_modules/void-elements/index.js"(exports, module) {
37
+ "use strict";
38
+ module.exports = {
39
+ "area": true,
40
+ "base": true,
41
+ "br": true,
42
+ "col": true,
43
+ "embed": true,
44
+ "hr": true,
45
+ "img": true,
46
+ "input": true,
47
+ "link": true,
48
+ "meta": true,
49
+ "param": true,
50
+ "source": true,
51
+ "track": true,
52
+ "wbr": true
53
+ };
54
+ }
55
+ });
56
+
57
+ // globals:react
58
+ var require_react = __commonJS({
59
+ "globals:react"(exports, module) {
60
+ "use strict";
61
+ module.exports = React;
62
+ }
63
+ });
64
+
65
+ // globals:react-i18next
66
+ var require_react_i18next = __commonJS({
67
+ "globals:react-i18next"(exports, module) {
68
+ "use strict";
69
+ module.exports = reactI18next;
70
+ }
71
+ });
72
+
73
+ // src/index.ts
74
+ var index_exports = {};
75
+ __export(index_exports, {
76
+ Trans: () => Trans
77
+ });
78
+
79
+ // ../../node_modules/.pnpm/html-parse-stringify@3.0.1/node_modules/html-parse-stringify/dist/html-parse-stringify.module.js
80
+ var import_void_elements = __toESM(require_void_elements());
81
+ var t = /\s([^'"/\s><]+?)[\s/>]|([^\s=]+)=\s?(".*?"|'.*?')/g;
82
+ function n(n2) {
83
+ var r2 = { type: "tag", name: "", voidElement: false, attrs: {}, children: [] }, i2 = n2.match(/<\/?([^\s]+?)[/\s>]/);
84
+ if (i2 && (r2.name = i2[1], (import_void_elements.default[i2[1]] || "/" === n2.charAt(n2.length - 2)) && (r2.voidElement = true), r2.name.startsWith("!--"))) {
85
+ var s2 = n2.indexOf("-->");
86
+ return { type: "comment", comment: -1 !== s2 ? n2.slice(4, s2) : "" };
87
+ }
88
+ for (var a2 = new RegExp(t), c2 = null; null !== (c2 = a2.exec(n2)); ) if (c2[0].trim()) if (c2[1]) {
89
+ var o = c2[1].trim(), l = [o, ""];
90
+ o.indexOf("=") > -1 && (l = o.split("=")), r2.attrs[l[0]] = l[1], a2.lastIndex--;
91
+ } else c2[2] && (r2.attrs[c2[2]] = c2[3].trim().substring(1, c2[3].length - 1));
92
+ return r2;
93
+ }
94
+ var r = /<[a-zA-Z0-9\-\!\/](?:"[^"]*"|'[^']*'|[^'">])*>/g;
95
+ var i = /^\s*$/;
96
+ var s = /* @__PURE__ */ Object.create(null);
97
+ function a(e2, t2) {
98
+ switch (t2.type) {
99
+ case "text":
100
+ return e2 + t2.content;
101
+ case "tag":
102
+ return e2 += "<" + t2.name + (t2.attrs ? function(e3) {
103
+ var t3 = [];
104
+ for (var n2 in e3) t3.push(n2 + '="' + e3[n2] + '"');
105
+ return t3.length ? " " + t3.join(" ") : "";
106
+ }(t2.attrs) : "") + (t2.voidElement ? "/>" : ">"), t2.voidElement ? e2 : e2 + t2.children.reduce(a, "") + "</" + t2.name + ">";
107
+ case "comment":
108
+ return e2 + "<!--" + t2.comment + "-->";
109
+ }
110
+ }
111
+ var c = { parse: function(e2, t2) {
112
+ t2 || (t2 = {}), t2.components || (t2.components = s);
113
+ var a2, c2 = [], o = [], l = -1, m = false;
114
+ if (0 !== e2.indexOf("<")) {
115
+ var u = e2.indexOf("<");
116
+ c2.push({ type: "text", content: -1 === u ? e2 : e2.substring(0, u) });
117
+ }
118
+ return e2.replace(r, function(r2, s2) {
119
+ if (m) {
120
+ if (r2 !== "</" + a2.name + ">") return;
121
+ m = false;
122
+ }
123
+ var u2, f = "/" !== r2.charAt(1), h = r2.startsWith("<!--"), p = s2 + r2.length, d = e2.charAt(p);
124
+ if (h) {
125
+ var v = n(r2);
126
+ return l < 0 ? (c2.push(v), c2) : ((u2 = o[l]).children.push(v), c2);
127
+ }
128
+ if (f && (l++, "tag" === (a2 = n(r2)).type && t2.components[a2.name] && (a2.type = "component", m = true), a2.voidElement || m || !d || "<" === d || a2.children.push({ type: "text", content: e2.slice(p, e2.indexOf("<", p)) }), 0 === l && c2.push(a2), (u2 = o[l - 1]) && u2.children.push(a2), o[l] = a2), (!f || a2.voidElement) && (l > -1 && (a2.voidElement || a2.name === r2.slice(2, -1)) && (l--, a2 = -1 === l ? c2 : o[l]), !m && "<" !== d && d)) {
129
+ u2 = -1 === l ? c2 : o[l].children;
130
+ var x = e2.indexOf("<", p), g = e2.slice(p, -1 === x ? void 0 : x);
131
+ i.test(g) && (g = " "), (x > -1 && l + u2.length >= 0 || " " !== g) && u2.push({ type: "text", content: g });
132
+ }
133
+ }), c2;
134
+ }, stringify: function(e2) {
135
+ return e2.reduce(function(e3, t2) {
136
+ return e3 + a("", t2);
137
+ }, "");
138
+ } };
139
+ var html_parse_stringify_module_default = c;
140
+
141
+ // src/index.ts
142
+ var import_react = __toESM(require_react(), 1);
143
+ var import_react_i18next = __toESM(require_react_i18next(), 1);
144
+ function Trans({ i18nKey, ...additionalProps }) {
145
+ const translation = (0, import_react_i18next.useTranslation)().t(i18nKey, additionalProps);
146
+ return renderNodes(html_parse_stringify_module_default.parse(translation), additionalProps);
147
+ }
148
+ function renderNodes(tokens, values) {
149
+ let index = 0;
150
+ return tokens.map((token) => {
151
+ if (token.type === "text")
152
+ return token.content;
153
+ index++;
154
+ const props = { ...token.attrs, key: index };
155
+ return token.voidElement ? values[token.name] ? (0, import_react.createElement)("span", { key: index }, values[token.name]) : (0, import_react.createElement)(token.name, props) : (0, import_react.createElement)(token.name, props, renderNodes(token.children, {}));
156
+ });
157
+ }
158
+ return __toCommonJS(index_exports);
159
+ })();
package/dist/index.js ADDED
@@ -0,0 +1,21 @@
1
+ // src/index.ts
2
+ import HTML from "html-parse-stringify";
3
+ import { createElement } from "react";
4
+ import { useTranslation } from "react-i18next";
5
+ function Trans({ i18nKey, ...additionalProps }) {
6
+ const translation = useTranslation().t(i18nKey, additionalProps);
7
+ return renderNodes(HTML.parse(translation), additionalProps);
8
+ }
9
+ function renderNodes(tokens, values) {
10
+ let index = 0;
11
+ return tokens.map((token) => {
12
+ if (token.type === "text")
13
+ return token.content;
14
+ index++;
15
+ const props = { ...token.attrs, key: index };
16
+ return token.voidElement ? values[token.name] ? createElement("span", { key: index }, values[token.name]) : createElement(token.name, props) : createElement(token.name, props, renderNodes(token.children, {}));
17
+ });
18
+ }
19
+ export {
20
+ Trans
21
+ };
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@hairy/react-i18-lib",
3
+ "type": "module",
4
+ "version": "1.26.0",
5
+ "description": "Library for react",
6
+ "author": "Hairyf <wwu710632@gmail.com>",
7
+ "license": "MIT",
8
+ "funding": "https://github.com/sponsors/hairyf",
9
+ "homepage": "https://github.com/hairyf/hairylib#readme",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/hairyf/hairylib.git"
13
+ },
14
+ "bugs": "https://github.com/hairyf/hairylib/issues",
15
+ "keywords": [],
16
+ "sideEffects": false,
17
+ "main": "./dist/index.js",
18
+ "publishConfig": {
19
+ "jsdelivr": "./dist/index.global.js"
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "peerDependencies": {
25
+ "react": "^18.2.0",
26
+ "react-dom": "^18.2.0",
27
+ "react-i18next": "^14.1.2"
28
+ },
29
+ "dependencies": {
30
+ "html-parse-stringify": "^3.0.1"
31
+ },
32
+ "devDependencies": {
33
+ "@types/react": "^18.2.43",
34
+ "@types/react-dom": "^18.2.17",
35
+ "react": "^18.2.0",
36
+ "react-dom": "^18.2.0",
37
+ "react-i18next": "^14.1.2"
38
+ },
39
+ "scripts": {
40
+ "build": "tsup",
41
+ "dev": "tsup --watch",
42
+ "start": "tsx src/index.ts"
43
+ },
44
+ "module": "./dist/index.js",
45
+ "types": "./dist/index.d.ts",
46
+ "unpkg": "./dist/index.global.js",
47
+ "exports": {
48
+ ".": {
49
+ "import": "./dist/index.js",
50
+ "require": "./dist/index.cjs",
51
+ "types": "./dist/index.d.ts"
52
+ },
53
+ "./*": "./*"
54
+ }
55
+ }