@bgscore/react-i18n 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/LICENSE +21 -0
- package/README.md +135 -0
- package/dist/plugin.cjs +1 -0
- package/dist/plugin.d.ts +8 -0
- package/dist/plugin.mjs +1 -0
- package/package.json +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Andry Dharmawan
|
|
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,135 @@
|
|
|
1
|
+
# @bgscore/react-i18n 🚀
|
|
2
|
+
|
|
3
|
+
Vite plugin to automatically generate TypeScript-safe i18n namespaces and translation keys from JSON files in React projects.
|
|
4
|
+
|
|
5
|
+
## Main Features
|
|
6
|
+
|
|
7
|
+
* 🌐 Auto-detects i18n JSON files in your project
|
|
8
|
+
* 🔧 Generates `I18nNamespaceMap` interface automatically
|
|
9
|
+
* ⚡ Fully TypeScript-safe translation keys
|
|
10
|
+
* 🔄 Supports nested JSON objects (`a.b.c`) flattened for keys
|
|
11
|
+
* 💡 HMR support during development
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @bgscore/react-i18n
|
|
17
|
+
# or
|
|
18
|
+
pnpm add @bgscore/react-i18n
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### Vite Configuration
|
|
24
|
+
|
|
25
|
+
Add this plugin to your Vite configuration (`vite.config.js` or `vite.config.ts`):
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { defineConfig } from "vite"
|
|
29
|
+
import react from "@vitejs/plugin-react-swc"
|
|
30
|
+
import reactI18n from "@bgscore/react-i18n/plugin";
|
|
31
|
+
|
|
32
|
+
export default defineConfig({
|
|
33
|
+
plugins: [
|
|
34
|
+
react(),
|
|
35
|
+
reactI18n({
|
|
36
|
+
i18nDirName: "i18n",
|
|
37
|
+
outputFileName: "i18n.types.ts",
|
|
38
|
+
namespaceInterfaceName: "I18nNamespaceMap"
|
|
39
|
+
}),
|
|
40
|
+
],
|
|
41
|
+
})
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Folder Structure
|
|
45
|
+
|
|
46
|
+
The plugin automatically generates TypeScript types from your JSON files. Example structure:
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
src/
|
|
50
|
+
├── components/
|
|
51
|
+
│ ├── home/
|
|
52
|
+
│ │ ├── i18n/
|
|
53
|
+
│ │ │ ├── id.json
|
|
54
|
+
│ │ │ └── en.json
|
|
55
|
+
│ │ └── Home.tsx
|
|
56
|
+
│ ├── dashboard/
|
|
57
|
+
│ │ ├── i18n/
|
|
58
|
+
│ │ │ └── id.json
|
|
59
|
+
│ │ └── Dashboard.tsx
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Generated Output
|
|
63
|
+
|
|
64
|
+
For each `i18n` folder, a `i18n.types.ts` file will be generated containing a `I18nNamespaceMap` interface:
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
declare global {
|
|
68
|
+
interface I18nNamespaceMap {
|
|
69
|
+
"components.home": "title" | "desc" | "a.b";
|
|
70
|
+
"components.dashboard": "title" | "subtitle";
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export {};
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Using `useTranslation` Hook
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import useTranslation from "hooks/useTranslation";
|
|
81
|
+
|
|
82
|
+
export default function Home() {
|
|
83
|
+
const { t, ns } = useTranslation("components.home");
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<div>
|
|
87
|
+
<h1>{t("title")}</h1>
|
|
88
|
+
<p>{t("desc")}</p>
|
|
89
|
+
</div>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
The hook provides:
|
|
95
|
+
|
|
96
|
+
* **t(key: string, options?)**: Type-safe translation function
|
|
97
|
+
* **ns**: Current namespace
|
|
98
|
+
* All keys are inferred from the generated `I18nNamespaceMap` for autocomplete
|
|
99
|
+
|
|
100
|
+
### Example JSON (id.json)
|
|
101
|
+
|
|
102
|
+
```json
|
|
103
|
+
{
|
|
104
|
+
"title": "Beranda",
|
|
105
|
+
"desc": "Selamat datang",
|
|
106
|
+
"a": {
|
|
107
|
+
"b": "Nested key example"
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
This will generate TypeScript keys:
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
"title" | "desc" | "a.b"
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Configuration Options
|
|
119
|
+
|
|
120
|
+
* **i18nDirName**: The folder name where i18n JSON files are stored. Default: `"i18n"`.
|
|
121
|
+
* **outputFileName**: Name of the generated TypeScript file. Default: `"i18n.types.ts"`.
|
|
122
|
+
* **namespaceInterfaceName**: Name of the global interface for namespaces. Default: `"I18nNamespaceMap"`.
|
|
123
|
+
|
|
124
|
+
### Development Workflow
|
|
125
|
+
|
|
126
|
+
* Any changes, addition, or deletion of JSON files in `i18n` folders will automatically regenerate TypeScript types.
|
|
127
|
+
* HMR ensures type updates are available immediately during development.
|
|
128
|
+
|
|
129
|
+
### Contribution
|
|
130
|
+
|
|
131
|
+
If you want to contribute to this project, please fork the repository and create a pull request.
|
|
132
|
+
|
|
133
|
+
### License
|
|
134
|
+
|
|
135
|
+
This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for more information.
|
package/dist/plugin.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"t",{value:!0});var n=require("fs"),e=require("path");const t=e.resolve(process.cwd(),"src");function r(t,o){for(const c of n.readdirSync(t,{withFileTypes:!0})){const n=e.join(t,c.name);c.isDirectory()&&(o(n),r(n,o))}}function o(n,e="",t=new Set){for(const[r,c]of Object.entries(n)){const n=e?`${e}.${r}`:r;null===c||"object"!=typeof c||Array.isArray(c)?t.add(n):o(c,n,t)}return t}function c(c,s,f){r(t,r=>{e.basename(r)===c&&function(r,c,s,f){const i=n.readdirSync(r).filter(n=>n.endsWith(".json"));if(!i.length)return;const u=new Set;for(const t of i){const c=e.join(r,t),s=JSON.parse(n.readFileSync(c,"utf-8"));"object"!=typeof s||Array.isArray(s)||o(s).forEach(n=>u.add(n))}if(!u.size)return;const a=function(n,r){return e.relative(t,n).replace(/\\/g,"/").replace(`/${r}`,"").replace(/\//g,".")}(r,c),l=`\ndeclare global {\n interface ${f} {\n "${a}": ${Array.from(u).sort().map(n=>`"${n}"`).join(" | ")};\n }\n}\n\nexport {};\n`,p=e.join(r,s);n.existsSync(p)&&n.readFileSync(p,"utf-8").trim()===l.trim()||n.writeFileSync(p,l,"utf-8")}(r,c,s,f)})}exports.default=(n={})=>{const e=n.i18nDirName??"i18n",t=n.outputFileName??"i18n.types.ts",r=n.namespaceInterfaceName??"I18nNamespaceMap",o=()=>{c(e,t,r)};return{name:"bgs-react-i18n",enforce:"pre",configResolved(){o()},buildStart(){o()},configureServer(n){o(),n.watcher.on("add",o).on("change",o).on("unlink",o).on("addDir",o).on("unlinkDir",o)}}};
|
package/dist/plugin.d.ts
ADDED
package/dist/plugin.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import n from"fs";import t from"path";const o=t.resolve(process.cwd(),"src");function e(o,r){for(const c of n.readdirSync(o,{withFileTypes:!0})){const n=t.join(o,c.name);c.isDirectory()&&(r(n),e(n,r))}}function r(n,t="",o=new Set){for(const[e,c]of Object.entries(n)){const n=t?`${t}.${e}`:e;null===c||"object"!=typeof c||Array.isArray(c)?o.add(n):r(c,n,o)}return o}function c(c,f,s){e(o,e=>{t.basename(e)===c&&function(e,c,f,s){const i=n.readdirSync(e).filter(n=>n.endsWith(".json"));if(!i.length)return;const u=new Set;for(const o of i){const c=t.join(e,o),f=JSON.parse(n.readFileSync(c,"utf-8"));"object"!=typeof f||Array.isArray(f)||r(f).forEach(n=>u.add(n))}if(!u.size)return;const a=function(n,e){return t.relative(o,n).replace(/\\/g,"/").replace(`/${e}`,"").replace(/\//g,".")}(e,c),p=`\ndeclare global {\n interface ${s} {\n "${a}": ${Array.from(u).sort().map(n=>`"${n}"`).join(" | ")};\n }\n}\n\nexport {};\n`,l=t.join(e,f);n.existsSync(l)&&n.readFileSync(l,"utf-8").trim()===p.trim()||n.writeFileSync(l,p,"utf-8")}(e,c,f,s)})}const f=(n={})=>{const t=n.i18nDirName??"i18n",o=n.outputFileName??"i18n.types.ts",e=n.namespaceInterfaceName??"I18nNamespaceMap",r=()=>{c(t,o,e)};return{name:"bgs-react-i18n",enforce:"pre",configResolved(){r()},buildStart(){r()},configureServer(n){r(),n.watcher.on("add",r).on("change",r).on("unlink",r).on("addDir",r).on("unlinkDir",r)}}};export{f as default};
|
package/package.json
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bgscore/react-i18n",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Vite plugin for React that automatically generates TypeScript-safe i18n namespaces and translation keys from JSON files",
|
|
5
|
+
"author": "Andry Bagus Dharmawan",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/andrydharmawan/react-i18n.git"
|
|
10
|
+
},
|
|
11
|
+
"main": "dist/index.cjs",
|
|
12
|
+
"module": "dist/index.mjs",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
"./plugin": {
|
|
16
|
+
"import": "./dist/plugin.mjs",
|
|
17
|
+
"require": "./dist/plugin.cjs",
|
|
18
|
+
"types": "./dist/plugin.d.ts",
|
|
19
|
+
"default": "./dist/plugin.mjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"typesVersions": {
|
|
23
|
+
"*": {
|
|
24
|
+
"plugin": [
|
|
25
|
+
"./dist/plugin.d.ts"
|
|
26
|
+
]
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"types": "dist/index.d.ts",
|
|
30
|
+
"files": [
|
|
31
|
+
"dist"
|
|
32
|
+
],
|
|
33
|
+
"scripts": {
|
|
34
|
+
"clean": "rimraf dist",
|
|
35
|
+
"build": "npm run clean && rollup -c --bundleConfigAsCjs && tsc --emitDeclarationOnly",
|
|
36
|
+
"prepublishOnly": "npm run build"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"react": ">=17.0.0",
|
|
40
|
+
"react-router-dom": ">=6.22.0",
|
|
41
|
+
"vite": ">=5.1.0"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"lodash-es": "^4.17.21",
|
|
45
|
+
"qs": "^6.12.1"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@rollup/plugin-commonjs": "^28.0.3",
|
|
49
|
+
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
50
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
51
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
52
|
+
"@types/lodash-es": "^4.17.12",
|
|
53
|
+
"@types/node": "^20.11.0",
|
|
54
|
+
"@types/react": "^18.2.0",
|
|
55
|
+
"rimraf": "^6.0.1",
|
|
56
|
+
"rollup": "^4.12.0",
|
|
57
|
+
"rollup-plugin-cleanup": "^3.2.1",
|
|
58
|
+
"tslib": "^2.6.2",
|
|
59
|
+
"typescript": "^5.3.3",
|
|
60
|
+
"vite": "^5.1.0"
|
|
61
|
+
},
|
|
62
|
+
"keywords": [
|
|
63
|
+
"react",
|
|
64
|
+
"vite",
|
|
65
|
+
"i18n",
|
|
66
|
+
"internationalization",
|
|
67
|
+
"localization",
|
|
68
|
+
"react-i18n",
|
|
69
|
+
"vite-plugin",
|
|
70
|
+
"typescript",
|
|
71
|
+
"types",
|
|
72
|
+
"type-safety",
|
|
73
|
+
"json",
|
|
74
|
+
"translation",
|
|
75
|
+
"auto-generate",
|
|
76
|
+
"codegen",
|
|
77
|
+
"developer-experience"
|
|
78
|
+
]
|
|
79
|
+
}
|