@hkonda/loco-translate 1.0.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.
Files changed (92) hide show
  1. package/README.md +284 -0
  2. package/bin/loco.js +5 -0
  3. package/dist/assets/index-CGo6e-bA.js +59 -0
  4. package/dist/assets/index-DBcQDZ75.css +1 -0
  5. package/dist/index.html +13 -0
  6. package/dist-server/app.d.ts +3 -0
  7. package/dist-server/app.d.ts.map +1 -0
  8. package/dist-server/app.js +154 -0
  9. package/dist-server/app.js.map +1 -0
  10. package/dist-server/config.d.ts +16 -0
  11. package/dist-server/config.d.ts.map +1 -0
  12. package/dist-server/config.js +41 -0
  13. package/dist-server/config.js.map +1 -0
  14. package/dist-server/db/index.d.ts +8 -0
  15. package/dist-server/db/index.d.ts.map +1 -0
  16. package/dist-server/db/index.js +28 -0
  17. package/dist-server/db/index.js.map +1 -0
  18. package/dist-server/db/schema.d.ts +851 -0
  19. package/dist-server/db/schema.d.ts.map +1 -0
  20. package/dist-server/db/schema.js +65 -0
  21. package/dist-server/db/schema.js.map +1 -0
  22. package/dist-server/db/seed.d.ts +14 -0
  23. package/dist-server/db/seed.d.ts.map +1 -0
  24. package/dist-server/db/seed.js +229 -0
  25. package/dist-server/db/seed.js.map +1 -0
  26. package/dist-server/index.d.ts +2 -0
  27. package/dist-server/index.d.ts.map +1 -0
  28. package/dist-server/index.js +31 -0
  29. package/dist-server/index.js.map +1 -0
  30. package/dist-server/routes/ai-jobs.d.ts +5 -0
  31. package/dist-server/routes/ai-jobs.d.ts.map +1 -0
  32. package/dist-server/routes/ai-jobs.js +141 -0
  33. package/dist-server/routes/ai-jobs.js.map +1 -0
  34. package/dist-server/routes/backup.d.ts +5 -0
  35. package/dist-server/routes/backup.d.ts.map +1 -0
  36. package/dist-server/routes/backup.js +125 -0
  37. package/dist-server/routes/backup.js.map +1 -0
  38. package/dist-server/routes/chrome-extension.d.ts +5 -0
  39. package/dist-server/routes/chrome-extension.d.ts.map +1 -0
  40. package/dist-server/routes/chrome-extension.js +140 -0
  41. package/dist-server/routes/chrome-extension.js.map +1 -0
  42. package/dist-server/routes/export.d.ts +5 -0
  43. package/dist-server/routes/export.d.ts.map +1 -0
  44. package/dist-server/routes/export.js +95 -0
  45. package/dist-server/routes/export.js.map +1 -0
  46. package/dist-server/routes/languages.d.ts +5 -0
  47. package/dist-server/routes/languages.d.ts.map +1 -0
  48. package/dist-server/routes/languages.js +36 -0
  49. package/dist-server/routes/languages.js.map +1 -0
  50. package/dist-server/routes/project.d.ts +5 -0
  51. package/dist-server/routes/project.d.ts.map +1 -0
  52. package/dist-server/routes/project.js +151 -0
  53. package/dist-server/routes/project.js.map +1 -0
  54. package/dist-server/routes/prompts.d.ts +5 -0
  55. package/dist-server/routes/prompts.d.ts.map +1 -0
  56. package/dist-server/routes/prompts.js +90 -0
  57. package/dist-server/routes/prompts.js.map +1 -0
  58. package/dist-server/routes/screenshots.d.ts +5 -0
  59. package/dist-server/routes/screenshots.d.ts.map +1 -0
  60. package/dist-server/routes/screenshots.js +71 -0
  61. package/dist-server/routes/screenshots.js.map +1 -0
  62. package/dist-server/routes/textnodes.d.ts +5 -0
  63. package/dist-server/routes/textnodes.d.ts.map +1 -0
  64. package/dist-server/routes/textnodes.js +318 -0
  65. package/dist-server/routes/textnodes.js.map +1 -0
  66. package/dist-server/routes/translations.d.ts +5 -0
  67. package/dist-server/routes/translations.d.ts.map +1 -0
  68. package/dist-server/routes/translations.js +224 -0
  69. package/dist-server/routes/translations.js.map +1 -0
  70. package/dist-server/scripts/backup.d.ts +2 -0
  71. package/dist-server/scripts/backup.d.ts.map +1 -0
  72. package/dist-server/scripts/backup.js +26 -0
  73. package/dist-server/scripts/backup.js.map +1 -0
  74. package/dist-server/services/ai-translation.d.ts +21 -0
  75. package/dist-server/services/ai-translation.d.ts.map +1 -0
  76. package/dist-server/services/ai-translation.js +137 -0
  77. package/dist-server/services/ai-translation.js.map +1 -0
  78. package/dist-server/services/job-manager.d.ts +67 -0
  79. package/dist-server/services/job-manager.d.ts.map +1 -0
  80. package/dist-server/services/job-manager.js +159 -0
  81. package/dist-server/services/job-manager.js.map +1 -0
  82. package/dist-server/services/slot-pattern.d.ts +16 -0
  83. package/dist-server/services/slot-pattern.d.ts.map +1 -0
  84. package/dist-server/services/slot-pattern.js +68 -0
  85. package/dist-server/services/slot-pattern.js.map +1 -0
  86. package/dist-server/utils/network.d.ts +2 -0
  87. package/dist-server/utils/network.d.ts.map +1 -0
  88. package/dist-server/utils/network.js +16 -0
  89. package/dist-server/utils/network.js.map +1 -0
  90. package/package.json +47 -0
  91. package/public/loco.js +2386 -0
  92. package/public/loco.min.js +4 -0
package/README.md ADDED
@@ -0,0 +1,284 @@
1
+ <p align="center">
2
+ <img src="assets/loco_animated.svg" alt="Loco Logo" width="500" />
3
+ </p>
4
+
5
+ # Loco
6
+
7
+ Open-source translation engine for any website or app. Drop in a single `<script>` tag, manage translations from a dashboard, and let AI handle the heavy lifting.
8
+
9
+ Two integration modes: **Server Mode** (live API-driven) and **File Mode** (static JSON, zero server dependency).
10
+
11
+ ---
12
+
13
+ ## Getting Started (First-Time Setup)
14
+
15
+ ### 1. Install & Start the Dashboard
16
+
17
+ ```bash
18
+ git clone https://github.mieweb.com/hkonda/loco.git
19
+ cd loco
20
+ npm install
21
+ npm run dev
22
+ ```
23
+
24
+ This starts two processes:
25
+ - **Vite dev server** on port `6100` (dashboard + proxy)
26
+ - **Fastify API** on port `6101` (backend)
27
+
28
+ On startup the terminal prints your working URLs:
29
+
30
+ ```
31
+ 🌐 Loco Translation Manager
32
+
33
+ Dashboard : http://<your-ip>:6100/loco/dashboard/
34
+ CDN script: http://<your-ip>:6100/loco/cdn/loco.js
35
+ API key : <auto-generated>
36
+
37
+ Paste into any page:
38
+
39
+ <script src="http://<your-ip>:6100/loco/cdn/loco.js"></script>
40
+ <script>Loco.init({ apiUrl: 'http://<your-ip>:6100/loco', apiKey: '<your-key>' });</script>
41
+ ```
42
+
43
+ Open the **Dashboard** URL in your browser. You should see the Translation Manager with Pending / Approved / Trash tabs.
44
+
45
+ ### 2. Add the CDN Snippet to Your Page
46
+
47
+ Copy the snippet from the terminal output (or from **Dashboard → Settings → CDN Script Snippet**) and paste it into any HTML page you want to translate. When that page loads in a browser, Loco automatically discovers text nodes and sends them to the dashboard as "Pending" items.
48
+
49
+ ### 3. Install the Chrome Extension (Optional)
50
+
51
+ The Chrome extension adds a side panel for in-context translation editing on any page running the Loco snippet.
52
+
53
+ 1. Open Chrome and go to `chrome://extensions/`
54
+ 2. Enable **Developer mode** (toggle in the top-right corner)
55
+ 3. Click **Load unpacked**
56
+ 4. Select the `chrome-extension/` folder inside this repo
57
+ 5. The **Loco Translation Editor** extension should appear in your extensions list
58
+
59
+ #### Configure the Extension
60
+
61
+ 1. Click the Loco extension icon in the Chrome toolbar → **Options** (or right-click → Extension options)
62
+ 2. Fill in the two fields:
63
+ - **API Base URL** — the same base URL from the snippet, e.g. `http://<your-ip>:6100/loco`
64
+ - **API Key** — the API key from **Dashboard → Settings**
65
+ 3. Click **Save Settings**, then **Test Connection** to verify it works
66
+ 4. Navigate to any page running the Loco snippet, then click the extension icon to open the side panel
67
+
68
+ ### 4. Test Pages
69
+
70
+ Several test pages are included and served during development:
71
+
72
+ | Page | URL |
73
+ |---|---|
74
+ | Server Mode Test | `http://<your-ip>:6100/loco/dashboard/test-server-mode.html` |
75
+ | File Mode Test | `http://<your-ip>:6100/loco/dashboard/test-file-mode.html` |
76
+ | Pattern Test | `http://<your-ip>:6100/loco/dashboard/test-patterns.html` |
77
+ | Basic Test | `http://<your-ip>:6100/loco/dashboard/test-page.html` |
78
+
79
+ Open any server-mode test page — text nodes should appear in the dashboard under **Pending** within a few seconds.
80
+
81
+ ### 5. Configure BlueHive AI (for AI Translations)
82
+
83
+ To use AI-powered translations, you need a BlueHive API key:
84
+
85
+ 1. Go to **Dashboard → Settings**
86
+ 2. Under **BlueHive API Key**, paste your key (starts with `BHSK-...`)
87
+ 3. Click **Save**
88
+
89
+ If you don't have a key, sign up at [bluehive.com](https://bluehive.com) and generate one from your developer account.
90
+
91
+ ### 6. Translation Workflow
92
+
93
+ 1. **Pending** — Newly discovered text appears here. Review and approve items you want to translate.
94
+ 2. **Approved** — Approved phrases are ready for translation. Use **AI Translate** to batch-translate with BlueHive, or edit translations manually.
95
+ 3. **Export** — From the Approved tab, click **Export JSON** to download a translation file for File Mode.
96
+ 4. **Apply** — On any page with the Loco snippet, call `Loco.apply('zh-CN')` in the browser console to see translations live.
97
+
98
+ ---
99
+
100
+ ## Self-Hosting (Production)
101
+
102
+ ### Prerequisites
103
+
104
+ - Node.js 20+
105
+ - npm
106
+ - Linux server with systemd (for production deployment)
107
+
108
+ ### Build & Run
109
+
110
+ ```bash
111
+ git clone https://github.mieweb.com/hkonda/loco.git
112
+ cd loco
113
+ npm install
114
+ npm run build
115
+ npm start
116
+ ```
117
+
118
+ Dashboard available at `http://<your-ip>:6101/dashboard/`
119
+
120
+ An API key is auto-generated on first run — find it in the dashboard under Settings.
121
+
122
+ To enable AI translations, add your BlueHive API key in the dashboard under **Settings > BlueHive API Key**.
123
+
124
+ ### Deploy with systemd
125
+
126
+ The included `deploy.sh` handles everything:
127
+
128
+ ```bash
129
+ chmod +x deploy.sh
130
+
131
+ ./deploy.sh install # Build + install systemd service + start
132
+ ./deploy.sh stop # Stop service
133
+ ./deploy.sh start # Start service
134
+ ./deploy.sh restart # Restart service
135
+ ./deploy.sh status # Show status + recent logs
136
+ ./deploy.sh logs # Tail live logs
137
+ ./deploy.sh uninstall # Stop + remove service
138
+ ```
139
+
140
+ After `install`, the script prints your dashboard URL and CDN script URL.
141
+
142
+ ---
143
+
144
+ ## Using Loco in Your App
145
+
146
+ ### Server Mode
147
+
148
+ Server mode connects your site to a running Loco instance. It automatically discovers text on the page, sends it to the server for management, and applies translations in real-time.
149
+
150
+ ```html
151
+ <script src="https://your-loco-server.com/cdn/loco.js"></script>
152
+ <script>
153
+ Loco.init({
154
+ apiUrl: 'https://your-loco-server.com',
155
+ apiKey: 'YOUR_API_KEY'
156
+ });
157
+
158
+ // Apply a language
159
+ Loco.apply('fr');
160
+ </script>
161
+ ```
162
+
163
+ **What happens:**
164
+
165
+ 1. `loco.js` scans the DOM and extracts all translatable text nodes
166
+ 2. Text nodes are sent to the Loco server and appear in the dashboard as "pending"
167
+ 3. You approve phrases, add languages, and translate (manually or with AI) in the dashboard
168
+ 4. `Loco.apply('fr')` fetches approved translations from the server and swaps text in the DOM
169
+ 5. A `MutationObserver` watches for new DOM nodes and registers them automatically
170
+
171
+ ### File Mode
172
+
173
+ File mode uses a static JSON file — no server connection needed. Export the JSON from the dashboard or create it by hand.
174
+
175
+ ```html
176
+ <script src="/path/to/loco.js"></script>
177
+ <script>
178
+ Loco.init({ file: '/translations.json' });
179
+
180
+ // Apply a language
181
+ Loco.apply('zh-CN');
182
+
183
+ // Optional: render a floating language switcher widget
184
+ Loco.widget({ position: 'bottom-right' });
185
+ </script>
186
+ ```
187
+
188
+ **Translation JSON format:**
189
+
190
+ ```json
191
+ {
192
+ "languages": ["fr", "zh-CN"],
193
+ "translations": {
194
+ "fr": {
195
+ "Hello": "Bonjour",
196
+ "Sign up": "S'inscrire",
197
+ "Welcome back, {{var:0}}": "Bon retour, {{var:0}}"
198
+ },
199
+ "zh-CN": {
200
+ "Hello": "你好",
201
+ "Sign up": "注册",
202
+ "Welcome back, {{var:0}}": "欢迎回来,{{var:0}}"
203
+ }
204
+ }
205
+ }
206
+ ```
207
+
208
+ You can export this file from the dashboard at any time (Translations → Export JSON).
209
+
210
+ ### Client API
211
+
212
+ ```javascript
213
+ Loco.init(config) // Initialize (server mode or file mode)
214
+ Loco.apply('fr') // Apply translations for a language → Promise
215
+ Loco.restore() // Revert all text to original
216
+ Loco.rescan() // Re-scan DOM for new text nodes
217
+ Loco.widget({ position }) // Render language switcher ('bottom-right', 'bottom-left', 'top-right', 'top-left')
218
+ Loco.textnodes() // Get all discovered text nodes
219
+ Loco.stopScan() // Pause MutationObserver
220
+ Loco.startScan() // Resume MutationObserver
221
+ ```
222
+
223
+ ### Variable Handling
224
+
225
+ Loco automatically detects dynamic values in text and preserves them across translations:
226
+
227
+ ```
228
+ Original: "You have 5 new messages"
229
+ Key: "You have {{var:0}} new messages"
230
+ French: "Vous avez {{var:0}} nouveaux messages"
231
+ Rendered: "Vous avez 5 nouveaux messages"
232
+ ```
233
+
234
+ ---
235
+
236
+ ## Architecture
237
+
238
+ ```mermaid
239
+ flowchart TB
240
+ subgraph Your Website or App
241
+ A[loco.js Client Script]
242
+ end
243
+
244
+ subgraph Loco Server
245
+ B[Fastify API]
246
+ C[SQLite Database]
247
+ D[AI Translation Jobs]
248
+ E[Dashboard SPA]
249
+ end
250
+
251
+ subgraph External
252
+ F[BlueHive AI API]
253
+ end
254
+
255
+ A -->|Register text nodes & screenshots| B
256
+ A -->|Fetch translations| B
257
+ B <--> C
258
+ E -->|Manage phrases & languages & prompts| B
259
+ B -->|Queue translation jobs| D
260
+ D -->|Send text & context| F
261
+ F -->|Return translations| D
262
+ D -->|Store results| C
263
+ ```
264
+
265
+ ### How It Works
266
+
267
+ 1. **Discover** — The client script scans your page, extracts text nodes (respecting blocked/inline tag rules), and sends them to the server along with page context and screenshots
268
+ 2. **Manage** — In the dashboard, review pending phrases, approve them, add target languages, and configure AI prompt templates
269
+ 3. **Translate** — Run AI translation jobs that use page screenshots and context for accurate results, or edit translations manually
270
+ 4. **Deliver** — The client fetches approved translations and swaps DOM text in real-time. Or export a static JSON file for file mode
271
+
272
+ ---
273
+
274
+ ## Tech Stack
275
+
276
+ | Layer | Technology |
277
+ |---|---|
278
+ | Client Script | Vanilla JavaScript (no dependencies) |
279
+ | Dashboard | Svelte 4 + Tailwind CSS |
280
+ | Server | Fastify + Node.js |
281
+ | Database | SQLite (better-sqlite3, WAL mode) |
282
+ | Build Tool | Vite |
283
+ | AI | BlueHive API |
284
+ | Deployment | systemd + deploy.sh |
package/bin/loco.js ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+ // Loco Translation Manager — CLI entry point
3
+ // Usage: npx loco-translate OR loco (if installed globally)
4
+
5
+ import '../dist-server/index.js';