-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
350 lines (288 loc) · 9.61 KB
/
Copy pathindex.js
File metadata and controls
350 lines (288 loc) · 9.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// index.js - Main entry point
const fs = require("fs").promises;
const path = require("path");
const chokidar = require("chokidar");
const crypto = require("crypto");
class TranslationSync {
constructor(options = {}) {
this.directory = options.directory || "./translations";
this.sourceLanguage = options.sourceLanguage || "en";
this.targetLanguages = options.targetLanguages || [];
this.onTranslationComplete = options.onTranslationComplete || (() => {});
this.silent = options.silent || false;
this.verbose = options.verbose || false;
this.hashFile = path.join(this.directory, ".translation-hashes.json");
this.hashes = {};
this.translationService = new GoogleTranslateService();
}
log(message, force = false) {
if (!this.silent || force) {
console.log(message);
}
}
async init() {
try {
// Ensure directory exists
await fs.mkdir(this.directory, { recursive: true });
await this.loadHashes();
this.log("✅ Translation sync initialized");
this.log(`📁 Watching: ${this.directory}`);
this.log(`🔤 Source language: ${this.sourceLanguage}`);
this.log(`🌍 Target languages: ${this.targetLanguages.join(", ")}`);
} catch (error) {
console.error("❌ Failed to initialize:", error.message);
throw error;
}
}
async loadHashes() {
try {
const hashData = await fs.readFile(this.hashFile, "utf8");
this.hashes = JSON.parse(hashData);
} catch (error) {
this.hashes = {};
}
}
async saveHashes() {
await fs.writeFile(this.hashFile, JSON.stringify(this.hashes, null, 2));
}
calculateHash(content) {
return crypto
.createHash("md5")
.update(JSON.stringify(content))
.digest("hex");
}
async readTranslationFile(filePath) {
try {
const content = await fs.readFile(filePath, "utf8");
return JSON.parse(content);
} catch (error) {
console.error(`❌ Error reading ${filePath}:`, error.message);
return {};
}
}
async writeTranslationFile(filePath, content) {
await fs.writeFile(filePath, JSON.stringify(content, null, 2));
}
getLanguageFromFilename(filename) {
const match = filename.match(/([a-z]{2}(-[A-Z]{2})?)\.json$/);
return match ? match[1] : null;
}
findChangedKeys(oldData, newData, prefix = "") {
const changes = {};
for (const [key, value] of Object.entries(newData)) {
const fullKey = prefix ? `${prefix}.${key}` : key;
if (typeof value === "object" && value !== null) {
const oldValue = oldData[key] || {};
const nestedChanges = this.findChangedKeys(oldValue, value, fullKey);
Object.assign(changes, nestedChanges);
} else if (oldData[key] !== value) {
changes[fullKey] = value;
}
}
return changes;
}
setNestedValue(obj, path, value) {
const keys = path.split(".");
const lastKey = keys.pop();
let current = obj;
for (const key of keys) {
if (!current[key] || typeof current[key] !== "object") {
current[key] = {};
}
current = current[key];
}
current[lastKey] = value;
}
async translateChanges(changes, sourceLang, targetLang) {
const translations = {};
for (const [key, value] of Object.entries(changes)) {
try {
const translatedValue = await this.translationService.translate(
value,
sourceLang,
targetLang
);
translations[key] = translatedValue;
if (this.verbose) {
this.log(
`🔄 ${key}: "${value}" → "${translatedValue}" (${sourceLang} → ${targetLang})`
);
}
} catch (error) {
console.error(`❌ Failed to translate "${key}":`, error.message);
translations[key] = value; // Fallback to original value
}
}
return translations;
}
async processFileChange(filePath) {
const filename = path.basename(filePath);
const language = this.getLanguageFromFilename(filename);
if (!language) {
this.log(`⏭️ Skipping non-translation file: ${filename}`);
return;
}
this.log(`\n📝 Processing changes in ${filename}...`);
const currentData = await this.readTranslationFile(filePath);
const currentHash = this.calculateHash(currentData);
const oldHash = this.hashes[filename];
if (currentHash === oldHash) {
this.log(`✅ No changes detected in ${filename}`);
return;
}
// If this is not the source language file, skip it
if (language !== this.sourceLanguage) {
this.log(`⏭️ Skipping non-source language file: ${filename}`);
this.hashes[filename] = currentHash;
await this.saveHashes();
return;
}
// Find what changed
const oldData = this.hashes[filename]
? await this.getOldData(filePath)
: {};
const changes = this.findChangedKeys(oldData, currentData);
if (Object.keys(changes).length === 0) {
this.log(`✅ No translatable changes in ${filename}`);
this.hashes[filename] = currentHash;
await this.saveHashes();
return;
}
this.log(`🔍 Found ${Object.keys(changes).length} changes to translate`);
// Translate changes to all target languages
for (const targetLang of this.targetLanguages) {
if (targetLang === language) continue;
const targetFilename = filename.replace(
/[a-z]{2}(-[A-Z]{2})?\.json$/,
`${targetLang}.json`
);
const targetFilePath = path.join(path.dirname(filePath), targetFilename);
this.log(`\n🌍 Translating to ${targetLang}...`);
// Load existing target file
let targetData = {};
try {
targetData = await this.readTranslationFile(targetFilePath);
} catch (error) {
this.log(`📄 Creating new file: ${targetFilename}`);
}
// Translate changes
const translations = await this.translateChanges(
changes,
language,
targetLang
);
// Apply translations to target data
for (const [key, translatedValue] of Object.entries(translations)) {
this.setNestedValue(targetData, key, translatedValue);
}
// Save updated target file
await this.writeTranslationFile(targetFilePath, targetData);
// Update hash for target file
const targetHash = this.calculateHash(targetData);
this.hashes[targetFilename] = targetHash;
this.log(`✅ Updated ${targetFilename}`);
}
// Update hash for source file
this.hashes[filename] = currentHash;
await this.saveHashes();
this.log(`\n🎉 Translation sync completed for ${filename}`);
// Execute callback
if (this.onTranslationComplete) {
this.onTranslationComplete(filename, changes);
}
}
async getOldData(filePath) {
// This is a simplified version - in a real implementation,
// you might want to store the previous state more reliably
return {};
}
async watch() {
const watcher = chokidar.watch(`${this.directory}/**/*.json`, {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true,
ignoreInitial: true,
});
watcher.on("change", async (filePath) => {
try {
await this.processFileChange(filePath);
} catch (error) {
console.error(`❌ Error processing ${filePath}:`, error.message);
}
});
watcher.on("add", async (filePath) => {
this.log(`📁 New translation file detected: ${path.basename(filePath)}`);
});
this.log(`👀 Watching for changes in ${this.directory}...`);
return watcher;
}
async checkAndTranslate() {
this.log("🔍 Checking for translation changes...");
const files = await fs.readdir(this.directory);
const jsonFiles = files.filter(
(file) => file.endsWith(".json") && !file.startsWith(".")
);
for (const file of jsonFiles) {
const filePath = path.join(this.directory, file);
await this.processFileChange(filePath);
}
// Delete the .translation-hashes.json file
try {
await fs.unlink(this.hashFile);
this.log(`🗑️ Deleted hash file: ${this.hashFile}`);
} catch (error) {
this.log(`⚠️ Could not delete hash file: ${error.message}`);
}
this.log("✅ Check complete");
}
}
// Free translation service using Google Translate (unofficial)
class GoogleTranslateService {
async translate(text, from, to) {
// Using a free translation method via unofficial API
// In production, you might want to use a more reliable service
const https = require("https");
const querystring = require("querystring");
return new Promise((resolve, reject) => {
const params = querystring.stringify({
client: "gtx",
sl: from,
tl: to,
dt: "t",
q: text,
});
const options = {
hostname: "translate.googleapis.com",
port: 443,
path: `/translate_a/single?${params}`,
method: "GET",
headers: {
"User-Agent": "Mozilla/5.0",
},
};
const req = https.request(options, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
try {
const parsed = JSON.parse(data);
const translation = parsed[0][0][0];
resolve(translation);
} catch (error) {
reject(new Error("Failed to parse translation response"));
}
});
});
req.on("error", (error) => {
reject(error);
});
req.setTimeout(5000, () => {
req.destroy();
reject(new Error("Translation request timeout"));
});
req.end();
});
}
}
module.exports = TranslationSync;