@scoutello/i18n-magic 0.14.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/README.md +94 -0
- package/dist/commands/check-missing.d.ts +2 -0
- package/dist/commands/replace.d.ts +2 -0
- package/dist/commands/scan.d.ts +2 -0
- package/dist/commands/sync-locales.d.ts +2 -0
- package/dist/i18n-magic.cjs.development.js +1409 -0
- package/dist/i18n-magic.cjs.development.js.map +1 -0
- package/dist/i18n-magic.cjs.production.min.js +2 -0
- package/dist/i18n-magic.cjs.production.min.js.map +1 -0
- package/dist/i18n-magic.esm.js +1407 -0
- package/dist/i18n-magic.esm.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +9 -0
- package/dist/lib/languges.d.ts +5 -0
- package/dist/lib/types.d.ts +24 -0
- package/dist/lib/utils.d.ts +26 -0
- package/package.json +62 -0
- package/src/commands/check-missing.ts +11 -0
- package/src/commands/replace.ts +83 -0
- package/src/commands/scan.ts +95 -0
- package/src/commands/sync-locales.ts +129 -0
- package/src/index.ts +92 -0
- package/src/lib/languges.ts +142 -0
- package/src/lib/types.ts +38 -0
- package/src/lib/utils.ts +320 -0
|
@@ -0,0 +1,1407 @@
|
|
|
1
|
+
import { Command } from 'commander';
|
|
2
|
+
import dotenv from 'dotenv';
|
|
3
|
+
import OpenAI from 'openai';
|
|
4
|
+
import glob from 'fast-glob';
|
|
5
|
+
import { Parser } from 'i18next-scanner';
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import prompts from 'prompts';
|
|
9
|
+
|
|
10
|
+
function _construct(t, e, r) {
|
|
11
|
+
if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments);
|
|
12
|
+
var o = [null];
|
|
13
|
+
o.push.apply(o, e);
|
|
14
|
+
var p = new (t.bind.apply(t, o))();
|
|
15
|
+
return r && _setPrototypeOf(p, r.prototype), p;
|
|
16
|
+
}
|
|
17
|
+
function _isNativeReflectConstruct() {
|
|
18
|
+
try {
|
|
19
|
+
var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
|
|
20
|
+
} catch (t) {}
|
|
21
|
+
return (_isNativeReflectConstruct = function () {
|
|
22
|
+
return !!t;
|
|
23
|
+
})();
|
|
24
|
+
}
|
|
25
|
+
function _regeneratorRuntime() {
|
|
26
|
+
_regeneratorRuntime = function () {
|
|
27
|
+
return e;
|
|
28
|
+
};
|
|
29
|
+
var t,
|
|
30
|
+
e = {},
|
|
31
|
+
r = Object.prototype,
|
|
32
|
+
n = r.hasOwnProperty,
|
|
33
|
+
o = Object.defineProperty || function (t, e, r) {
|
|
34
|
+
t[e] = r.value;
|
|
35
|
+
},
|
|
36
|
+
i = "function" == typeof Symbol ? Symbol : {},
|
|
37
|
+
a = i.iterator || "@@iterator",
|
|
38
|
+
c = i.asyncIterator || "@@asyncIterator",
|
|
39
|
+
u = i.toStringTag || "@@toStringTag";
|
|
40
|
+
function define(t, e, r) {
|
|
41
|
+
return Object.defineProperty(t, e, {
|
|
42
|
+
value: r,
|
|
43
|
+
enumerable: !0,
|
|
44
|
+
configurable: !0,
|
|
45
|
+
writable: !0
|
|
46
|
+
}), t[e];
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
define({}, "");
|
|
50
|
+
} catch (t) {
|
|
51
|
+
define = function (t, e, r) {
|
|
52
|
+
return t[e] = r;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function wrap(t, e, r, n) {
|
|
56
|
+
var i = e && e.prototype instanceof Generator ? e : Generator,
|
|
57
|
+
a = Object.create(i.prototype),
|
|
58
|
+
c = new Context(n || []);
|
|
59
|
+
return o(a, "_invoke", {
|
|
60
|
+
value: makeInvokeMethod(t, r, c)
|
|
61
|
+
}), a;
|
|
62
|
+
}
|
|
63
|
+
function tryCatch(t, e, r) {
|
|
64
|
+
try {
|
|
65
|
+
return {
|
|
66
|
+
type: "normal",
|
|
67
|
+
arg: t.call(e, r)
|
|
68
|
+
};
|
|
69
|
+
} catch (t) {
|
|
70
|
+
return {
|
|
71
|
+
type: "throw",
|
|
72
|
+
arg: t
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
e.wrap = wrap;
|
|
77
|
+
var h = "suspendedStart",
|
|
78
|
+
l = "suspendedYield",
|
|
79
|
+
f = "executing",
|
|
80
|
+
s = "completed",
|
|
81
|
+
y = {};
|
|
82
|
+
function Generator() {}
|
|
83
|
+
function GeneratorFunction() {}
|
|
84
|
+
function GeneratorFunctionPrototype() {}
|
|
85
|
+
var p = {};
|
|
86
|
+
define(p, a, function () {
|
|
87
|
+
return this;
|
|
88
|
+
});
|
|
89
|
+
var d = Object.getPrototypeOf,
|
|
90
|
+
v = d && d(d(values([])));
|
|
91
|
+
v && v !== r && n.call(v, a) && (p = v);
|
|
92
|
+
var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p);
|
|
93
|
+
function defineIteratorMethods(t) {
|
|
94
|
+
["next", "throw", "return"].forEach(function (e) {
|
|
95
|
+
define(t, e, function (t) {
|
|
96
|
+
return this._invoke(e, t);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
function AsyncIterator(t, e) {
|
|
101
|
+
function invoke(r, o, i, a) {
|
|
102
|
+
var c = tryCatch(t[r], t, o);
|
|
103
|
+
if ("throw" !== c.type) {
|
|
104
|
+
var u = c.arg,
|
|
105
|
+
h = u.value;
|
|
106
|
+
return h && "object" == typeof h && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) {
|
|
107
|
+
invoke("next", t, i, a);
|
|
108
|
+
}, function (t) {
|
|
109
|
+
invoke("throw", t, i, a);
|
|
110
|
+
}) : e.resolve(h).then(function (t) {
|
|
111
|
+
u.value = t, i(u);
|
|
112
|
+
}, function (t) {
|
|
113
|
+
return invoke("throw", t, i, a);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
a(c.arg);
|
|
117
|
+
}
|
|
118
|
+
var r;
|
|
119
|
+
o(this, "_invoke", {
|
|
120
|
+
value: function (t, n) {
|
|
121
|
+
function callInvokeWithMethodAndArg() {
|
|
122
|
+
return new e(function (e, r) {
|
|
123
|
+
invoke(t, n, e, r);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
function makeInvokeMethod(e, r, n) {
|
|
131
|
+
var o = h;
|
|
132
|
+
return function (i, a) {
|
|
133
|
+
if (o === f) throw Error("Generator is already running");
|
|
134
|
+
if (o === s) {
|
|
135
|
+
if ("throw" === i) throw a;
|
|
136
|
+
return {
|
|
137
|
+
value: t,
|
|
138
|
+
done: !0
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
for (n.method = i, n.arg = a;;) {
|
|
142
|
+
var c = n.delegate;
|
|
143
|
+
if (c) {
|
|
144
|
+
var u = maybeInvokeDelegate(c, n);
|
|
145
|
+
if (u) {
|
|
146
|
+
if (u === y) continue;
|
|
147
|
+
return u;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) {
|
|
151
|
+
if (o === h) throw o = s, n.arg;
|
|
152
|
+
n.dispatchException(n.arg);
|
|
153
|
+
} else "return" === n.method && n.abrupt("return", n.arg);
|
|
154
|
+
o = f;
|
|
155
|
+
var p = tryCatch(e, r, n);
|
|
156
|
+
if ("normal" === p.type) {
|
|
157
|
+
if (o = n.done ? s : l, p.arg === y) continue;
|
|
158
|
+
return {
|
|
159
|
+
value: p.arg,
|
|
160
|
+
done: n.done
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
"throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
function maybeInvokeDelegate(e, r) {
|
|
168
|
+
var n = r.method,
|
|
169
|
+
o = e.iterator[n];
|
|
170
|
+
if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y;
|
|
171
|
+
var i = tryCatch(o, e.iterator, r.arg);
|
|
172
|
+
if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y;
|
|
173
|
+
var a = i.arg;
|
|
174
|
+
return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y);
|
|
175
|
+
}
|
|
176
|
+
function pushTryEntry(t) {
|
|
177
|
+
var e = {
|
|
178
|
+
tryLoc: t[0]
|
|
179
|
+
};
|
|
180
|
+
1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e);
|
|
181
|
+
}
|
|
182
|
+
function resetTryEntry(t) {
|
|
183
|
+
var e = t.completion || {};
|
|
184
|
+
e.type = "normal", delete e.arg, t.completion = e;
|
|
185
|
+
}
|
|
186
|
+
function Context(t) {
|
|
187
|
+
this.tryEntries = [{
|
|
188
|
+
tryLoc: "root"
|
|
189
|
+
}], t.forEach(pushTryEntry, this), this.reset(!0);
|
|
190
|
+
}
|
|
191
|
+
function values(e) {
|
|
192
|
+
if (e || "" === e) {
|
|
193
|
+
var r = e[a];
|
|
194
|
+
if (r) return r.call(e);
|
|
195
|
+
if ("function" == typeof e.next) return e;
|
|
196
|
+
if (!isNaN(e.length)) {
|
|
197
|
+
var o = -1,
|
|
198
|
+
i = function next() {
|
|
199
|
+
for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next;
|
|
200
|
+
return next.value = t, next.done = !0, next;
|
|
201
|
+
};
|
|
202
|
+
return i.next = i;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
throw new TypeError(typeof e + " is not iterable");
|
|
206
|
+
}
|
|
207
|
+
return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", {
|
|
208
|
+
value: GeneratorFunctionPrototype,
|
|
209
|
+
configurable: !0
|
|
210
|
+
}), o(GeneratorFunctionPrototype, "constructor", {
|
|
211
|
+
value: GeneratorFunction,
|
|
212
|
+
configurable: !0
|
|
213
|
+
}), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) {
|
|
214
|
+
var e = "function" == typeof t && t.constructor;
|
|
215
|
+
return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name));
|
|
216
|
+
}, e.mark = function (t) {
|
|
217
|
+
return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t;
|
|
218
|
+
}, e.awrap = function (t) {
|
|
219
|
+
return {
|
|
220
|
+
__await: t
|
|
221
|
+
};
|
|
222
|
+
}, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () {
|
|
223
|
+
return this;
|
|
224
|
+
}), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) {
|
|
225
|
+
void 0 === i && (i = Promise);
|
|
226
|
+
var a = new AsyncIterator(wrap(t, r, n, o), i);
|
|
227
|
+
return e.isGeneratorFunction(r) ? a : a.next().then(function (t) {
|
|
228
|
+
return t.done ? t.value : a.next();
|
|
229
|
+
});
|
|
230
|
+
}, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () {
|
|
231
|
+
return this;
|
|
232
|
+
}), define(g, "toString", function () {
|
|
233
|
+
return "[object Generator]";
|
|
234
|
+
}), e.keys = function (t) {
|
|
235
|
+
var e = Object(t),
|
|
236
|
+
r = [];
|
|
237
|
+
for (var n in e) r.push(n);
|
|
238
|
+
return r.reverse(), function next() {
|
|
239
|
+
for (; r.length;) {
|
|
240
|
+
var t = r.pop();
|
|
241
|
+
if (t in e) return next.value = t, next.done = !1, next;
|
|
242
|
+
}
|
|
243
|
+
return next.done = !0, next;
|
|
244
|
+
};
|
|
245
|
+
}, e.values = values, Context.prototype = {
|
|
246
|
+
constructor: Context,
|
|
247
|
+
reset: function (e) {
|
|
248
|
+
if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t);
|
|
249
|
+
},
|
|
250
|
+
stop: function () {
|
|
251
|
+
this.done = !0;
|
|
252
|
+
var t = this.tryEntries[0].completion;
|
|
253
|
+
if ("throw" === t.type) throw t.arg;
|
|
254
|
+
return this.rval;
|
|
255
|
+
},
|
|
256
|
+
dispatchException: function (e) {
|
|
257
|
+
if (this.done) throw e;
|
|
258
|
+
var r = this;
|
|
259
|
+
function handle(n, o) {
|
|
260
|
+
return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o;
|
|
261
|
+
}
|
|
262
|
+
for (var o = this.tryEntries.length - 1; o >= 0; --o) {
|
|
263
|
+
var i = this.tryEntries[o],
|
|
264
|
+
a = i.completion;
|
|
265
|
+
if ("root" === i.tryLoc) return handle("end");
|
|
266
|
+
if (i.tryLoc <= this.prev) {
|
|
267
|
+
var c = n.call(i, "catchLoc"),
|
|
268
|
+
u = n.call(i, "finallyLoc");
|
|
269
|
+
if (c && u) {
|
|
270
|
+
if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
|
|
271
|
+
if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
|
|
272
|
+
} else if (c) {
|
|
273
|
+
if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
|
|
274
|
+
} else {
|
|
275
|
+
if (!u) throw Error("try statement without catch or finally");
|
|
276
|
+
if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
},
|
|
281
|
+
abrupt: function (t, e) {
|
|
282
|
+
for (var r = this.tryEntries.length - 1; r >= 0; --r) {
|
|
283
|
+
var o = this.tryEntries[r];
|
|
284
|
+
if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) {
|
|
285
|
+
var i = o;
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null);
|
|
290
|
+
var a = i ? i.completion : {};
|
|
291
|
+
return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a);
|
|
292
|
+
},
|
|
293
|
+
complete: function (t, e) {
|
|
294
|
+
if ("throw" === t.type) throw t.arg;
|
|
295
|
+
return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y;
|
|
296
|
+
},
|
|
297
|
+
finish: function (t) {
|
|
298
|
+
for (var e = this.tryEntries.length - 1; e >= 0; --e) {
|
|
299
|
+
var r = this.tryEntries[e];
|
|
300
|
+
if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y;
|
|
301
|
+
}
|
|
302
|
+
},
|
|
303
|
+
catch: function (t) {
|
|
304
|
+
for (var e = this.tryEntries.length - 1; e >= 0; --e) {
|
|
305
|
+
var r = this.tryEntries[e];
|
|
306
|
+
if (r.tryLoc === t) {
|
|
307
|
+
var n = r.completion;
|
|
308
|
+
if ("throw" === n.type) {
|
|
309
|
+
var o = n.arg;
|
|
310
|
+
resetTryEntry(r);
|
|
311
|
+
}
|
|
312
|
+
return o;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
throw Error("illegal catch attempt");
|
|
316
|
+
},
|
|
317
|
+
delegateYield: function (e, r, n) {
|
|
318
|
+
return this.delegate = {
|
|
319
|
+
iterator: values(e),
|
|
320
|
+
resultName: r,
|
|
321
|
+
nextLoc: n
|
|
322
|
+
}, "next" === this.method && (this.arg = t), y;
|
|
323
|
+
}
|
|
324
|
+
}, e;
|
|
325
|
+
}
|
|
326
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
327
|
+
try {
|
|
328
|
+
var info = gen[key](arg);
|
|
329
|
+
var value = info.value;
|
|
330
|
+
} catch (error) {
|
|
331
|
+
reject(error);
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
if (info.done) {
|
|
335
|
+
resolve(value);
|
|
336
|
+
} else {
|
|
337
|
+
Promise.resolve(value).then(_next, _throw);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
function _asyncToGenerator(fn) {
|
|
341
|
+
return function () {
|
|
342
|
+
var self = this,
|
|
343
|
+
args = arguments;
|
|
344
|
+
return new Promise(function (resolve, reject) {
|
|
345
|
+
var gen = fn.apply(self, args);
|
|
346
|
+
function _next(value) {
|
|
347
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
348
|
+
}
|
|
349
|
+
function _throw(err) {
|
|
350
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
351
|
+
}
|
|
352
|
+
_next(undefined);
|
|
353
|
+
});
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
function _extends() {
|
|
357
|
+
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
358
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
359
|
+
var source = arguments[i];
|
|
360
|
+
for (var key in source) {
|
|
361
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
362
|
+
target[key] = source[key];
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return target;
|
|
367
|
+
};
|
|
368
|
+
return _extends.apply(this, arguments);
|
|
369
|
+
}
|
|
370
|
+
function _inheritsLoose(subClass, superClass) {
|
|
371
|
+
subClass.prototype = Object.create(superClass.prototype);
|
|
372
|
+
subClass.prototype.constructor = subClass;
|
|
373
|
+
_setPrototypeOf(subClass, superClass);
|
|
374
|
+
}
|
|
375
|
+
function _getPrototypeOf(o) {
|
|
376
|
+
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) {
|
|
377
|
+
return o.__proto__ || Object.getPrototypeOf(o);
|
|
378
|
+
};
|
|
379
|
+
return _getPrototypeOf(o);
|
|
380
|
+
}
|
|
381
|
+
function _setPrototypeOf(o, p) {
|
|
382
|
+
_setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
|
|
383
|
+
o.__proto__ = p;
|
|
384
|
+
return o;
|
|
385
|
+
};
|
|
386
|
+
return _setPrototypeOf(o, p);
|
|
387
|
+
}
|
|
388
|
+
function _isNativeFunction(fn) {
|
|
389
|
+
try {
|
|
390
|
+
return Function.toString.call(fn).indexOf("[native code]") !== -1;
|
|
391
|
+
} catch (e) {
|
|
392
|
+
return typeof fn === "function";
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
function _wrapNativeSuper(Class) {
|
|
396
|
+
var _cache = typeof Map === "function" ? new Map() : undefined;
|
|
397
|
+
_wrapNativeSuper = function _wrapNativeSuper(Class) {
|
|
398
|
+
if (Class === null || !_isNativeFunction(Class)) return Class;
|
|
399
|
+
if (typeof Class !== "function") {
|
|
400
|
+
throw new TypeError("Super expression must either be null or a function");
|
|
401
|
+
}
|
|
402
|
+
if (typeof _cache !== "undefined") {
|
|
403
|
+
if (_cache.has(Class)) return _cache.get(Class);
|
|
404
|
+
_cache.set(Class, Wrapper);
|
|
405
|
+
}
|
|
406
|
+
function Wrapper() {
|
|
407
|
+
return _construct(Class, arguments, _getPrototypeOf(this).constructor);
|
|
408
|
+
}
|
|
409
|
+
Wrapper.prototype = Object.create(Class.prototype, {
|
|
410
|
+
constructor: {
|
|
411
|
+
value: Wrapper,
|
|
412
|
+
enumerable: false,
|
|
413
|
+
writable: true,
|
|
414
|
+
configurable: true
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
return _setPrototypeOf(Wrapper, Class);
|
|
418
|
+
};
|
|
419
|
+
return _wrapNativeSuper(Class);
|
|
420
|
+
}
|
|
421
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
422
|
+
if (!o) return;
|
|
423
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
424
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
425
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
426
|
+
if (n === "Map" || n === "Set") return Array.from(o);
|
|
427
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
428
|
+
}
|
|
429
|
+
function _arrayLikeToArray(arr, len) {
|
|
430
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
431
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
432
|
+
return arr2;
|
|
433
|
+
}
|
|
434
|
+
function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
|
435
|
+
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
|
|
436
|
+
if (it) return (it = it.call(o)).next.bind(it);
|
|
437
|
+
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
|
438
|
+
if (it) o = it;
|
|
439
|
+
var i = 0;
|
|
440
|
+
return function () {
|
|
441
|
+
if (i >= o.length) return {
|
|
442
|
+
done: true
|
|
443
|
+
};
|
|
444
|
+
return {
|
|
445
|
+
done: false,
|
|
446
|
+
value: o[i++]
|
|
447
|
+
};
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
var languages = [{
|
|
454
|
+
label: "Deutsch",
|
|
455
|
+
name: "German",
|
|
456
|
+
value: "de"
|
|
457
|
+
}, {
|
|
458
|
+
label: "English",
|
|
459
|
+
name: "English",
|
|
460
|
+
value: "en"
|
|
461
|
+
}, {
|
|
462
|
+
label: "Español",
|
|
463
|
+
name: "Spanish",
|
|
464
|
+
value: "es"
|
|
465
|
+
}, {
|
|
466
|
+
label: "Français",
|
|
467
|
+
name: "French",
|
|
468
|
+
value: "fr"
|
|
469
|
+
}, {
|
|
470
|
+
label: "Dansk",
|
|
471
|
+
name: "Danish",
|
|
472
|
+
value: "dk"
|
|
473
|
+
}, {
|
|
474
|
+
label: "中文",
|
|
475
|
+
name: "Chinese",
|
|
476
|
+
value: "cn"
|
|
477
|
+
}, {
|
|
478
|
+
label: "Русский",
|
|
479
|
+
name: "Russian",
|
|
480
|
+
value: "ru"
|
|
481
|
+
}, {
|
|
482
|
+
label: "Italiano",
|
|
483
|
+
name: "Italian",
|
|
484
|
+
value: "it"
|
|
485
|
+
}, {
|
|
486
|
+
label: "Nederlands",
|
|
487
|
+
name: "Dutch",
|
|
488
|
+
value: "nl"
|
|
489
|
+
}, {
|
|
490
|
+
label: "Português",
|
|
491
|
+
name: "Portuguese",
|
|
492
|
+
value: "pt"
|
|
493
|
+
}, {
|
|
494
|
+
label: "Türkçe",
|
|
495
|
+
name: "Turkish",
|
|
496
|
+
value: "tr"
|
|
497
|
+
}, {
|
|
498
|
+
label: "Polski",
|
|
499
|
+
name: "Polish",
|
|
500
|
+
value: "pl"
|
|
501
|
+
}, {
|
|
502
|
+
label: "Українська",
|
|
503
|
+
name: "Ukrainian",
|
|
504
|
+
value: "ua"
|
|
505
|
+
}, {
|
|
506
|
+
label: "Suomi",
|
|
507
|
+
name: "Finnish",
|
|
508
|
+
value: "fi"
|
|
509
|
+
}, {
|
|
510
|
+
label: "Norsk",
|
|
511
|
+
name: "Norwegian",
|
|
512
|
+
value: "no"
|
|
513
|
+
}, {
|
|
514
|
+
label: "Svenska",
|
|
515
|
+
name: "Swedish",
|
|
516
|
+
value: "sv"
|
|
517
|
+
}, {
|
|
518
|
+
label: "Čeština",
|
|
519
|
+
name: "Czech",
|
|
520
|
+
value: "cz"
|
|
521
|
+
}, {
|
|
522
|
+
label: "Ελληνικά",
|
|
523
|
+
name: "Greek",
|
|
524
|
+
value: "gr"
|
|
525
|
+
}, {
|
|
526
|
+
label: "日本語",
|
|
527
|
+
name: "Japanese",
|
|
528
|
+
value: "jp"
|
|
529
|
+
}, {
|
|
530
|
+
label: "한국어",
|
|
531
|
+
name: "Korean",
|
|
532
|
+
value: "kr"
|
|
533
|
+
}, {
|
|
534
|
+
label: "Română",
|
|
535
|
+
name: "Romanian",
|
|
536
|
+
value: "ro"
|
|
537
|
+
}, {
|
|
538
|
+
label: "Hrvatski",
|
|
539
|
+
name: "Croatian",
|
|
540
|
+
value: "hr"
|
|
541
|
+
}, {
|
|
542
|
+
label: "Magyar",
|
|
543
|
+
name: "Hungarian",
|
|
544
|
+
value: "hu"
|
|
545
|
+
}, {
|
|
546
|
+
label: "Slovensky",
|
|
547
|
+
name: "Slovak",
|
|
548
|
+
value: "sk"
|
|
549
|
+
}, {
|
|
550
|
+
label: "हिन्दी",
|
|
551
|
+
name: "Hindi",
|
|
552
|
+
value: "hi"
|
|
553
|
+
}, {
|
|
554
|
+
label: "தமிழ்",
|
|
555
|
+
name: "Tamil",
|
|
556
|
+
value: "ta"
|
|
557
|
+
}, {
|
|
558
|
+
label: "Bahasa Indonesia",
|
|
559
|
+
name: "Indonesian",
|
|
560
|
+
value: "id"
|
|
561
|
+
}, {
|
|
562
|
+
label: "Tiếng Việt",
|
|
563
|
+
name: "Vietnamese",
|
|
564
|
+
value: "vn"
|
|
565
|
+
}];
|
|
566
|
+
|
|
567
|
+
var loadConfig = function loadConfig(_ref) {
|
|
568
|
+
var _ref$configPath = _ref.configPath,
|
|
569
|
+
configPath = _ref$configPath === void 0 ? "i18n-magic.js" : _ref$configPath;
|
|
570
|
+
var filePath = path.join(process.cwd(), configPath);
|
|
571
|
+
if (!fs.existsSync(filePath)) {
|
|
572
|
+
console.error("Config file does not exist:", filePath);
|
|
573
|
+
process.exit(1);
|
|
574
|
+
}
|
|
575
|
+
try {
|
|
576
|
+
var config = require(filePath);
|
|
577
|
+
// Validate config if needed
|
|
578
|
+
return config;
|
|
579
|
+
} catch (error) {
|
|
580
|
+
console.error("Error while loading config:", error);
|
|
581
|
+
process.exit(1);
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
function removeDuplicatesFromArray(arr) {
|
|
585
|
+
return arr.filter(function (item, index) {
|
|
586
|
+
return arr.indexOf(item) === index;
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
var translateKey = /*#__PURE__*/function () {
|
|
590
|
+
var _ref3 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref2) {
|
|
591
|
+
var inputLanguage, context, object, openai, outputLanguage, model, entries, chunks, i, result, existingInput, existingOutput, input, output, _i, _chunks, chunk, chunkObject, completion, translatedChunk;
|
|
592
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
593
|
+
while (1) switch (_context.prev = _context.next) {
|
|
594
|
+
case 0:
|
|
595
|
+
inputLanguage = _ref2.inputLanguage, context = _ref2.context, object = _ref2.object, openai = _ref2.openai, outputLanguage = _ref2.outputLanguage, model = _ref2.model;
|
|
596
|
+
// Split object into chunks of 100 keys
|
|
597
|
+
entries = Object.entries(object);
|
|
598
|
+
chunks = [];
|
|
599
|
+
for (i = 0; i < entries.length; i += 100) {
|
|
600
|
+
chunks.push(entries.slice(i, i + 100));
|
|
601
|
+
}
|
|
602
|
+
result = {};
|
|
603
|
+
existingInput = languages.find(function (l) {
|
|
604
|
+
return l.value === inputLanguage;
|
|
605
|
+
});
|
|
606
|
+
existingOutput = languages.find(function (l) {
|
|
607
|
+
return l.value === outputLanguage;
|
|
608
|
+
});
|
|
609
|
+
input = (existingInput == null ? void 0 : existingInput.label) || inputLanguage;
|
|
610
|
+
output = (existingOutput == null ? void 0 : existingOutput.label) || outputLanguage; // Translate each chunk
|
|
611
|
+
_i = 0, _chunks = chunks;
|
|
612
|
+
case 10:
|
|
613
|
+
if (!(_i < _chunks.length)) {
|
|
614
|
+
_context.next = 23;
|
|
615
|
+
break;
|
|
616
|
+
}
|
|
617
|
+
chunk = _chunks[_i];
|
|
618
|
+
chunkObject = Object.fromEntries(chunk);
|
|
619
|
+
_context.next = 15;
|
|
620
|
+
return openai.beta.chat.completions.parse({
|
|
621
|
+
model: model,
|
|
622
|
+
messages: [{
|
|
623
|
+
content: "You are a bot that translates the values of a locales JSON. " + (context ? "The user provided some additional context or guidelines about what to fill in the blanks: \"" + context + "\". " : "") + "The user provides you a JSON with a field named \"inputLanguage\", which defines the language the values of the JSON are defined in. It also has a field named \"outputLanguage\", which defines the language you should translate the values to. The last field is named \"data\", which includes the object with the values to translate. The keys of the values should never be changed. You output only a JSON, which has the same keys as the input, but with translated values. I give you an example input: {\"inputLanguage\": \"English\", outputLanguage: \"German\", \"keys\": {\"hello\": \"Hello\", \"world\": \"World\"}}. The output should be {\"hello\": \"Hallo\", \"world\": \"Welt\"}.",
|
|
624
|
+
role: "system"
|
|
625
|
+
}, {
|
|
626
|
+
content: JSON.stringify({
|
|
627
|
+
inputLanguage: input,
|
|
628
|
+
outputLanguage: output,
|
|
629
|
+
data: chunkObject
|
|
630
|
+
}),
|
|
631
|
+
role: "user"
|
|
632
|
+
}],
|
|
633
|
+
response_format: {
|
|
634
|
+
type: "json_object"
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
case 15:
|
|
638
|
+
completion = _context.sent;
|
|
639
|
+
translatedChunk = JSON.parse(completion.choices[0].message.content); // Merge translated chunk with result
|
|
640
|
+
result = _extends({}, result, translatedChunk);
|
|
641
|
+
// Optional: Add a small delay between chunks to avoid rate limiting
|
|
642
|
+
_context.next = 20;
|
|
643
|
+
return new Promise(function (resolve) {
|
|
644
|
+
return setTimeout(resolve, 100);
|
|
645
|
+
});
|
|
646
|
+
case 20:
|
|
647
|
+
_i++;
|
|
648
|
+
_context.next = 10;
|
|
649
|
+
break;
|
|
650
|
+
case 23:
|
|
651
|
+
return _context.abrupt("return", result);
|
|
652
|
+
case 24:
|
|
653
|
+
case "end":
|
|
654
|
+
return _context.stop();
|
|
655
|
+
}
|
|
656
|
+
}, _callee);
|
|
657
|
+
}));
|
|
658
|
+
return function translateKey(_x) {
|
|
659
|
+
return _ref3.apply(this, arguments);
|
|
660
|
+
};
|
|
661
|
+
}();
|
|
662
|
+
var loadLocalesFile = /*#__PURE__*/function () {
|
|
663
|
+
var _ref4 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(loadPath, locale, namespace) {
|
|
664
|
+
var resolvedPath, content, json;
|
|
665
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
666
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
667
|
+
case 0:
|
|
668
|
+
if (!(typeof loadPath === "string")) {
|
|
669
|
+
_context2.next = 5;
|
|
670
|
+
break;
|
|
671
|
+
}
|
|
672
|
+
resolvedPath = loadPath.replace("{{lng}}", locale).replace("{{ns}}", namespace);
|
|
673
|
+
content = fs.readFileSync(resolvedPath, "utf-8");
|
|
674
|
+
json = JSON.parse(content);
|
|
675
|
+
return _context2.abrupt("return", json);
|
|
676
|
+
case 5:
|
|
677
|
+
return _context2.abrupt("return", loadPath(locale, namespace));
|
|
678
|
+
case 6:
|
|
679
|
+
case "end":
|
|
680
|
+
return _context2.stop();
|
|
681
|
+
}
|
|
682
|
+
}, _callee2);
|
|
683
|
+
}));
|
|
684
|
+
return function loadLocalesFile(_x2, _x3, _x4) {
|
|
685
|
+
return _ref4.apply(this, arguments);
|
|
686
|
+
};
|
|
687
|
+
}();
|
|
688
|
+
var writeLocalesFile = /*#__PURE__*/function () {
|
|
689
|
+
var _ref5 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(savePath, locale, namespace, data) {
|
|
690
|
+
var resolvedSavePath;
|
|
691
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
692
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
693
|
+
case 0:
|
|
694
|
+
if (!(typeof savePath === "string")) {
|
|
695
|
+
_context3.next = 4;
|
|
696
|
+
break;
|
|
697
|
+
}
|
|
698
|
+
resolvedSavePath = savePath.replace("{{lng}}", locale).replace("{{ns}}", namespace);
|
|
699
|
+
fs.writeFileSync(resolvedSavePath, JSON.stringify(data, null, 2));
|
|
700
|
+
return _context3.abrupt("return");
|
|
701
|
+
case 4:
|
|
702
|
+
_context3.next = 6;
|
|
703
|
+
return savePath(locale, namespace, data);
|
|
704
|
+
case 6:
|
|
705
|
+
case "end":
|
|
706
|
+
return _context3.stop();
|
|
707
|
+
}
|
|
708
|
+
}, _callee3);
|
|
709
|
+
}));
|
|
710
|
+
return function writeLocalesFile(_x5, _x6, _x7, _x8) {
|
|
711
|
+
return _ref5.apply(this, arguments);
|
|
712
|
+
};
|
|
713
|
+
}();
|
|
714
|
+
var getPureKey = function getPureKey(key, namespace, isDefault) {
|
|
715
|
+
var splitted = key.split(":");
|
|
716
|
+
if (splitted.length === 1) {
|
|
717
|
+
if (isDefault) {
|
|
718
|
+
return key;
|
|
719
|
+
}
|
|
720
|
+
return null;
|
|
721
|
+
}
|
|
722
|
+
if (splitted[0] === namespace) {
|
|
723
|
+
return splitted[1];
|
|
724
|
+
}
|
|
725
|
+
return null;
|
|
726
|
+
};
|
|
727
|
+
var getMissingKeys = /*#__PURE__*/function () {
|
|
728
|
+
var _ref7 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(_ref6) {
|
|
729
|
+
var globPatterns, namespaces, defaultNamespace, defaultLocale, loadPath, parser, files, keys, _iterator, _step, file, content, uniqueKeys, newKeys, _iterator2, _step2, namespace, existingKeys, _iterator3, _step3, key, pureKey;
|
|
730
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
731
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
732
|
+
case 0:
|
|
733
|
+
globPatterns = _ref6.globPatterns, namespaces = _ref6.namespaces, defaultNamespace = _ref6.defaultNamespace, defaultLocale = _ref6.defaultLocale, loadPath = _ref6.loadPath;
|
|
734
|
+
parser = new Parser({
|
|
735
|
+
nsSeparator: false,
|
|
736
|
+
keySeparator: false
|
|
737
|
+
});
|
|
738
|
+
_context4.next = 4;
|
|
739
|
+
return glob([].concat(globPatterns, ["!**/node_modules/**"]));
|
|
740
|
+
case 4:
|
|
741
|
+
files = _context4.sent;
|
|
742
|
+
keys = [];
|
|
743
|
+
for (_iterator = _createForOfIteratorHelperLoose(files); !(_step = _iterator()).done;) {
|
|
744
|
+
file = _step.value;
|
|
745
|
+
content = fs.readFileSync(file, "utf-8");
|
|
746
|
+
parser.parseFuncFromString(content, {
|
|
747
|
+
list: ["t"]
|
|
748
|
+
}, function (key) {
|
|
749
|
+
keys.push(key);
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
uniqueKeys = removeDuplicatesFromArray(keys);
|
|
753
|
+
newKeys = [];
|
|
754
|
+
_iterator2 = _createForOfIteratorHelperLoose(namespaces);
|
|
755
|
+
case 10:
|
|
756
|
+
if ((_step2 = _iterator2()).done) {
|
|
757
|
+
_context4.next = 27;
|
|
758
|
+
break;
|
|
759
|
+
}
|
|
760
|
+
namespace = _step2.value;
|
|
761
|
+
_context4.next = 14;
|
|
762
|
+
return loadLocalesFile(loadPath, defaultLocale, namespace);
|
|
763
|
+
case 14:
|
|
764
|
+
existingKeys = _context4.sent;
|
|
765
|
+
console.log(Object.keys(existingKeys).length, "existing keys");
|
|
766
|
+
_iterator3 = _createForOfIteratorHelperLoose(uniqueKeys);
|
|
767
|
+
case 17:
|
|
768
|
+
if ((_step3 = _iterator3()).done) {
|
|
769
|
+
_context4.next = 25;
|
|
770
|
+
break;
|
|
771
|
+
}
|
|
772
|
+
key = _step3.value;
|
|
773
|
+
pureKey = getPureKey(key, namespace, namespace === defaultNamespace);
|
|
774
|
+
if (pureKey) {
|
|
775
|
+
_context4.next = 22;
|
|
776
|
+
break;
|
|
777
|
+
}
|
|
778
|
+
return _context4.abrupt("continue", 23);
|
|
779
|
+
case 22:
|
|
780
|
+
if (!existingKeys[pureKey]) {
|
|
781
|
+
newKeys.push({
|
|
782
|
+
key: pureKey,
|
|
783
|
+
namespace: namespace
|
|
784
|
+
});
|
|
785
|
+
}
|
|
786
|
+
case 23:
|
|
787
|
+
_context4.next = 17;
|
|
788
|
+
break;
|
|
789
|
+
case 25:
|
|
790
|
+
_context4.next = 10;
|
|
791
|
+
break;
|
|
792
|
+
case 27:
|
|
793
|
+
return _context4.abrupt("return", newKeys);
|
|
794
|
+
case 28:
|
|
795
|
+
case "end":
|
|
796
|
+
return _context4.stop();
|
|
797
|
+
}
|
|
798
|
+
}, _callee4);
|
|
799
|
+
}));
|
|
800
|
+
return function getMissingKeys(_x9) {
|
|
801
|
+
return _ref7.apply(this, arguments);
|
|
802
|
+
};
|
|
803
|
+
}();
|
|
804
|
+
var getTextInput = /*#__PURE__*/function () {
|
|
805
|
+
var _ref8 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(prompt) {
|
|
806
|
+
var input;
|
|
807
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
|
|
808
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
809
|
+
case 0:
|
|
810
|
+
_context5.next = 2;
|
|
811
|
+
return prompts({
|
|
812
|
+
name: "value",
|
|
813
|
+
type: "text",
|
|
814
|
+
message: prompt,
|
|
815
|
+
onState: function onState(state) {
|
|
816
|
+
if (state.aborted) {
|
|
817
|
+
process.nextTick(function () {
|
|
818
|
+
process.exit(0);
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
});
|
|
823
|
+
case 2:
|
|
824
|
+
input = _context5.sent;
|
|
825
|
+
return _context5.abrupt("return", input.value);
|
|
826
|
+
case 4:
|
|
827
|
+
case "end":
|
|
828
|
+
return _context5.stop();
|
|
829
|
+
}
|
|
830
|
+
}, _callee5);
|
|
831
|
+
}));
|
|
832
|
+
return function getTextInput(_x10) {
|
|
833
|
+
return _ref8.apply(this, arguments);
|
|
834
|
+
};
|
|
835
|
+
}();
|
|
836
|
+
var checkAllKeysExist = /*#__PURE__*/function () {
|
|
837
|
+
var _ref10 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(_ref9) {
|
|
838
|
+
var namespaces, defaultLocale, loadPath, locales, context, openai, savePath, disableTranslation, model, _iterator4, _step4, namespace, defaultLocaleKeys, _iterator5, _step5, locale, localeKeys, missingKeys, _i2, _Object$entries, _Object$entries$_i, key, value, translatedValues, updatedLocaleKeys;
|
|
839
|
+
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
|
|
840
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
841
|
+
case 0:
|
|
842
|
+
namespaces = _ref9.namespaces, defaultLocale = _ref9.defaultLocale, loadPath = _ref9.loadPath, locales = _ref9.locales, context = _ref9.context, openai = _ref9.openai, savePath = _ref9.savePath, disableTranslation = _ref9.disableTranslation, model = _ref9.model;
|
|
843
|
+
if (!disableTranslation) {
|
|
844
|
+
_context6.next = 3;
|
|
845
|
+
break;
|
|
846
|
+
}
|
|
847
|
+
return _context6.abrupt("return");
|
|
848
|
+
case 3:
|
|
849
|
+
_iterator4 = _createForOfIteratorHelperLoose(namespaces);
|
|
850
|
+
case 4:
|
|
851
|
+
if ((_step4 = _iterator4()).done) {
|
|
852
|
+
_context6.next = 31;
|
|
853
|
+
break;
|
|
854
|
+
}
|
|
855
|
+
namespace = _step4.value;
|
|
856
|
+
_context6.next = 8;
|
|
857
|
+
return loadLocalesFile(loadPath, defaultLocale, namespace);
|
|
858
|
+
case 8:
|
|
859
|
+
defaultLocaleKeys = _context6.sent;
|
|
860
|
+
_iterator5 = _createForOfIteratorHelperLoose(locales);
|
|
861
|
+
case 10:
|
|
862
|
+
if ((_step5 = _iterator5()).done) {
|
|
863
|
+
_context6.next = 29;
|
|
864
|
+
break;
|
|
865
|
+
}
|
|
866
|
+
locale = _step5.value;
|
|
867
|
+
if (!(locale === defaultLocale)) {
|
|
868
|
+
_context6.next = 14;
|
|
869
|
+
break;
|
|
870
|
+
}
|
|
871
|
+
return _context6.abrupt("continue", 27);
|
|
872
|
+
case 14:
|
|
873
|
+
_context6.next = 16;
|
|
874
|
+
return loadLocalesFile(loadPath, locale, namespace);
|
|
875
|
+
case 16:
|
|
876
|
+
localeKeys = _context6.sent;
|
|
877
|
+
missingKeys = {}; // Check which keys from default locale are missing in current locale
|
|
878
|
+
for (_i2 = 0, _Object$entries = Object.entries(defaultLocaleKeys); _i2 < _Object$entries.length; _i2++) {
|
|
879
|
+
_Object$entries$_i = _Object$entries[_i2], key = _Object$entries$_i[0], value = _Object$entries$_i[1];
|
|
880
|
+
if (!localeKeys[key]) {
|
|
881
|
+
missingKeys[key] = value;
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
// If there are missing keys, translate them
|
|
885
|
+
if (!(Object.keys(missingKeys).length > 0)) {
|
|
886
|
+
_context6.next = 27;
|
|
887
|
+
break;
|
|
888
|
+
}
|
|
889
|
+
console.log("Found " + Object.keys(missingKeys).length + " missing keys in " + locale + " (namespace: " + namespace + ")");
|
|
890
|
+
_context6.next = 23;
|
|
891
|
+
return translateKey({
|
|
892
|
+
inputLanguage: defaultLocale,
|
|
893
|
+
outputLanguage: locale,
|
|
894
|
+
context: context,
|
|
895
|
+
object: missingKeys,
|
|
896
|
+
openai: openai,
|
|
897
|
+
model: model
|
|
898
|
+
});
|
|
899
|
+
case 23:
|
|
900
|
+
translatedValues = _context6.sent;
|
|
901
|
+
// Merge translated values with existing ones
|
|
902
|
+
updatedLocaleKeys = _extends({}, localeKeys, translatedValues); // Save the updated translations
|
|
903
|
+
writeLocalesFile(savePath, locale, namespace, updatedLocaleKeys);
|
|
904
|
+
console.log("\u2713 Translated and saved missing keys for " + locale + " (namespace: " + namespace + ")");
|
|
905
|
+
case 27:
|
|
906
|
+
_context6.next = 10;
|
|
907
|
+
break;
|
|
908
|
+
case 29:
|
|
909
|
+
_context6.next = 4;
|
|
910
|
+
break;
|
|
911
|
+
case 31:
|
|
912
|
+
case "end":
|
|
913
|
+
return _context6.stop();
|
|
914
|
+
}
|
|
915
|
+
}, _callee6);
|
|
916
|
+
}));
|
|
917
|
+
return function checkAllKeysExist(_x11) {
|
|
918
|
+
return _ref10.apply(this, arguments);
|
|
919
|
+
};
|
|
920
|
+
}();
|
|
921
|
+
var TranslationError = /*#__PURE__*/function (_Error) {
|
|
922
|
+
function TranslationError(message, locale, namespace, cause) {
|
|
923
|
+
var _this;
|
|
924
|
+
_this = _Error.call(this, message) || this;
|
|
925
|
+
_this.locale = void 0;
|
|
926
|
+
_this.namespace = void 0;
|
|
927
|
+
_this.cause = void 0;
|
|
928
|
+
_this.locale = locale;
|
|
929
|
+
_this.namespace = namespace;
|
|
930
|
+
_this.cause = cause;
|
|
931
|
+
_this.name = "TranslationError";
|
|
932
|
+
return _this;
|
|
933
|
+
}
|
|
934
|
+
_inheritsLoose(TranslationError, _Error);
|
|
935
|
+
return TranslationError;
|
|
936
|
+
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
937
|
+
|
|
938
|
+
var checkMissing = /*#__PURE__*/function () {
|
|
939
|
+
var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config) {
|
|
940
|
+
var newKeys;
|
|
941
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
942
|
+
while (1) switch (_context.prev = _context.next) {
|
|
943
|
+
case 0:
|
|
944
|
+
_context.next = 2;
|
|
945
|
+
return getMissingKeys(config);
|
|
946
|
+
case 2:
|
|
947
|
+
newKeys = _context.sent;
|
|
948
|
+
if (newKeys.length > 0) {
|
|
949
|
+
console.error("Error: Missing translations found!");
|
|
950
|
+
process.exit(1);
|
|
951
|
+
}
|
|
952
|
+
case 4:
|
|
953
|
+
case "end":
|
|
954
|
+
return _context.stop();
|
|
955
|
+
}
|
|
956
|
+
}, _callee);
|
|
957
|
+
}));
|
|
958
|
+
return function checkMissing(_x) {
|
|
959
|
+
return _ref.apply(this, arguments);
|
|
960
|
+
};
|
|
961
|
+
}();
|
|
962
|
+
|
|
963
|
+
var getKeyToReplace = /*#__PURE__*/function () {
|
|
964
|
+
var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(keys) {
|
|
965
|
+
var keyToReplace;
|
|
966
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
967
|
+
while (1) switch (_context.prev = _context.next) {
|
|
968
|
+
case 0:
|
|
969
|
+
_context.next = 2;
|
|
970
|
+
return getTextInput("Enter the key to replace the translation for: ");
|
|
971
|
+
case 2:
|
|
972
|
+
keyToReplace = _context.sent;
|
|
973
|
+
if (keys[keyToReplace]) {
|
|
974
|
+
_context.next = 8;
|
|
975
|
+
break;
|
|
976
|
+
}
|
|
977
|
+
console.log("The key \"" + keyToReplace + "\" does not exist.");
|
|
978
|
+
_context.next = 7;
|
|
979
|
+
return getKeyToReplace(keys);
|
|
980
|
+
case 7:
|
|
981
|
+
return _context.abrupt("return", _context.sent);
|
|
982
|
+
case 8:
|
|
983
|
+
console.log("The key \"" + keyToReplace + "\" exists.");
|
|
984
|
+
return _context.abrupt("return", keyToReplace);
|
|
985
|
+
case 10:
|
|
986
|
+
case "end":
|
|
987
|
+
return _context.stop();
|
|
988
|
+
}
|
|
989
|
+
}, _callee);
|
|
990
|
+
}));
|
|
991
|
+
return function getKeyToReplace(_x) {
|
|
992
|
+
return _ref.apply(this, arguments);
|
|
993
|
+
};
|
|
994
|
+
}();
|
|
995
|
+
var replaceTranslation = /*#__PURE__*/function () {
|
|
996
|
+
var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(config) {
|
|
997
|
+
var loadPath, savePath, defaultLocale, defaultNamespace, locales, context, openai, keys, keyToReplace, newTranslation, _iterator, _step, locale, newValue, _object, translation, existingKeys;
|
|
998
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
999
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
1000
|
+
case 0:
|
|
1001
|
+
loadPath = config.loadPath, savePath = config.savePath, defaultLocale = config.defaultLocale, defaultNamespace = config.defaultNamespace, locales = config.locales, context = config.context, openai = config.openai;
|
|
1002
|
+
_context2.next = 3;
|
|
1003
|
+
return loadLocalesFile(config.loadPath, config.defaultLocale, config.defaultNamespace);
|
|
1004
|
+
case 3:
|
|
1005
|
+
keys = _context2.sent;
|
|
1006
|
+
_context2.next = 6;
|
|
1007
|
+
return getKeyToReplace(keys);
|
|
1008
|
+
case 6:
|
|
1009
|
+
keyToReplace = _context2.sent;
|
|
1010
|
+
console.log("The current translation in " + defaultLocale + " for \"" + keyToReplace + "\" is \"" + keys[keyToReplace] + "\".");
|
|
1011
|
+
_context2.next = 10;
|
|
1012
|
+
return getTextInput("Enter the new translation: ");
|
|
1013
|
+
case 10:
|
|
1014
|
+
newTranslation = _context2.sent;
|
|
1015
|
+
_iterator = _createForOfIteratorHelperLoose(locales);
|
|
1016
|
+
case 12:
|
|
1017
|
+
if ((_step = _iterator()).done) {
|
|
1018
|
+
_context2.next = 31;
|
|
1019
|
+
break;
|
|
1020
|
+
}
|
|
1021
|
+
locale = _step.value;
|
|
1022
|
+
newValue = "";
|
|
1023
|
+
if (!(locale === defaultLocale)) {
|
|
1024
|
+
_context2.next = 19;
|
|
1025
|
+
break;
|
|
1026
|
+
}
|
|
1027
|
+
newValue = newTranslation;
|
|
1028
|
+
_context2.next = 23;
|
|
1029
|
+
break;
|
|
1030
|
+
case 19:
|
|
1031
|
+
_context2.next = 21;
|
|
1032
|
+
return translateKey({
|
|
1033
|
+
context: context,
|
|
1034
|
+
inputLanguage: defaultLocale,
|
|
1035
|
+
outputLanguage: locale,
|
|
1036
|
+
object: (_object = {}, _object[keyToReplace] = newTranslation, _object),
|
|
1037
|
+
openai: openai,
|
|
1038
|
+
model: config.model
|
|
1039
|
+
});
|
|
1040
|
+
case 21:
|
|
1041
|
+
translation = _context2.sent;
|
|
1042
|
+
newValue = translation[keyToReplace];
|
|
1043
|
+
case 23:
|
|
1044
|
+
_context2.next = 25;
|
|
1045
|
+
return loadLocalesFile(loadPath, locale, defaultNamespace);
|
|
1046
|
+
case 25:
|
|
1047
|
+
existingKeys = _context2.sent;
|
|
1048
|
+
existingKeys[keyToReplace] = newValue;
|
|
1049
|
+
writeLocalesFile(savePath, locale, defaultNamespace, existingKeys);
|
|
1050
|
+
console.log("The new translation for \"" + keyToReplace + "\" in " + locale + " is \"" + newValue + "\".");
|
|
1051
|
+
case 29:
|
|
1052
|
+
_context2.next = 12;
|
|
1053
|
+
break;
|
|
1054
|
+
case 31:
|
|
1055
|
+
case "end":
|
|
1056
|
+
return _context2.stop();
|
|
1057
|
+
}
|
|
1058
|
+
}, _callee2);
|
|
1059
|
+
}));
|
|
1060
|
+
return function replaceTranslation(_x2) {
|
|
1061
|
+
return _ref2.apply(this, arguments);
|
|
1062
|
+
};
|
|
1063
|
+
}();
|
|
1064
|
+
|
|
1065
|
+
var translateMissing = /*#__PURE__*/function () {
|
|
1066
|
+
var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config) {
|
|
1067
|
+
var loadPath, savePath, defaultLocale, namespaces, locales, context, openai, disableTranslation, newKeys, newKeysWithDefaultLocale, _iterator, _step, newKey, answer, newKeysObject, allLocales, _iterator2, _step2, locale, translatedValues, _loop, _iterator3, _step3;
|
|
1068
|
+
return _regeneratorRuntime().wrap(function _callee$(_context2) {
|
|
1069
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
1070
|
+
case 0:
|
|
1071
|
+
loadPath = config.loadPath, savePath = config.savePath, defaultLocale = config.defaultLocale, namespaces = config.namespaces, locales = config.locales, context = config.context, openai = config.openai, disableTranslation = config.disableTranslation;
|
|
1072
|
+
_context2.next = 3;
|
|
1073
|
+
return getMissingKeys(config);
|
|
1074
|
+
case 3:
|
|
1075
|
+
newKeys = _context2.sent;
|
|
1076
|
+
if (!(newKeys.length === 0)) {
|
|
1077
|
+
_context2.next = 9;
|
|
1078
|
+
break;
|
|
1079
|
+
}
|
|
1080
|
+
console.log("No new keys found.");
|
|
1081
|
+
_context2.next = 8;
|
|
1082
|
+
return checkAllKeysExist(config);
|
|
1083
|
+
case 8:
|
|
1084
|
+
return _context2.abrupt("return");
|
|
1085
|
+
case 9:
|
|
1086
|
+
console.log(newKeys.length + " keys are missing. Please provide the values for the following keys in " + defaultLocale + ":");
|
|
1087
|
+
newKeysWithDefaultLocale = [];
|
|
1088
|
+
_iterator = _createForOfIteratorHelperLoose(newKeys);
|
|
1089
|
+
case 12:
|
|
1090
|
+
if ((_step = _iterator()).done) {
|
|
1091
|
+
_context2.next = 20;
|
|
1092
|
+
break;
|
|
1093
|
+
}
|
|
1094
|
+
newKey = _step.value;
|
|
1095
|
+
_context2.next = 16;
|
|
1096
|
+
return getTextInput(newKey.key);
|
|
1097
|
+
case 16:
|
|
1098
|
+
answer = _context2.sent;
|
|
1099
|
+
newKeysWithDefaultLocale.push({
|
|
1100
|
+
key: newKey.key,
|
|
1101
|
+
namespace: newKey.namespace,
|
|
1102
|
+
value: answer
|
|
1103
|
+
});
|
|
1104
|
+
case 18:
|
|
1105
|
+
_context2.next = 12;
|
|
1106
|
+
break;
|
|
1107
|
+
case 20:
|
|
1108
|
+
newKeysObject = newKeysWithDefaultLocale.reduce(function (prev, next) {
|
|
1109
|
+
prev[next.key] = next.value;
|
|
1110
|
+
return prev;
|
|
1111
|
+
}, {});
|
|
1112
|
+
allLocales = disableTranslation ? [defaultLocale] : locales;
|
|
1113
|
+
_iterator2 = _createForOfIteratorHelperLoose(allLocales);
|
|
1114
|
+
case 23:
|
|
1115
|
+
if ((_step2 = _iterator2()).done) {
|
|
1116
|
+
_context2.next = 43;
|
|
1117
|
+
break;
|
|
1118
|
+
}
|
|
1119
|
+
locale = _step2.value;
|
|
1120
|
+
translatedValues = {};
|
|
1121
|
+
if (!(locale === defaultLocale)) {
|
|
1122
|
+
_context2.next = 30;
|
|
1123
|
+
break;
|
|
1124
|
+
}
|
|
1125
|
+
translatedValues = newKeysObject;
|
|
1126
|
+
_context2.next = 33;
|
|
1127
|
+
break;
|
|
1128
|
+
case 30:
|
|
1129
|
+
_context2.next = 32;
|
|
1130
|
+
return translateKey({
|
|
1131
|
+
inputLanguage: defaultLocale,
|
|
1132
|
+
outputLanguage: locale,
|
|
1133
|
+
context: context,
|
|
1134
|
+
object: newKeysObject,
|
|
1135
|
+
openai: openai,
|
|
1136
|
+
model: config.model
|
|
1137
|
+
});
|
|
1138
|
+
case 32:
|
|
1139
|
+
translatedValues = _context2.sent;
|
|
1140
|
+
case 33:
|
|
1141
|
+
_loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
|
|
1142
|
+
var namespace, existingKeys, relevantKeys, _iterator4, _step4, key;
|
|
1143
|
+
return _regeneratorRuntime().wrap(function _loop$(_context) {
|
|
1144
|
+
while (1) switch (_context.prev = _context.next) {
|
|
1145
|
+
case 0:
|
|
1146
|
+
namespace = _step3.value;
|
|
1147
|
+
_context.next = 3;
|
|
1148
|
+
return loadLocalesFile(loadPath, locale, namespace);
|
|
1149
|
+
case 3:
|
|
1150
|
+
existingKeys = _context.sent;
|
|
1151
|
+
relevantKeys = newKeysWithDefaultLocale.filter(function (key) {
|
|
1152
|
+
return key.namespace === namespace;
|
|
1153
|
+
});
|
|
1154
|
+
if (!(relevantKeys.length === 0)) {
|
|
1155
|
+
_context.next = 7;
|
|
1156
|
+
break;
|
|
1157
|
+
}
|
|
1158
|
+
return _context.abrupt("return", 1);
|
|
1159
|
+
case 7:
|
|
1160
|
+
for (_iterator4 = _createForOfIteratorHelperLoose(relevantKeys); !(_step4 = _iterator4()).done;) {
|
|
1161
|
+
key = _step4.value;
|
|
1162
|
+
existingKeys[key.key] = translatedValues[key.key];
|
|
1163
|
+
}
|
|
1164
|
+
writeLocalesFile(savePath, locale, namespace, existingKeys);
|
|
1165
|
+
case 9:
|
|
1166
|
+
case "end":
|
|
1167
|
+
return _context.stop();
|
|
1168
|
+
}
|
|
1169
|
+
}, _loop);
|
|
1170
|
+
});
|
|
1171
|
+
_iterator3 = _createForOfIteratorHelperLoose(namespaces);
|
|
1172
|
+
case 35:
|
|
1173
|
+
if ((_step3 = _iterator3()).done) {
|
|
1174
|
+
_context2.next = 41;
|
|
1175
|
+
break;
|
|
1176
|
+
}
|
|
1177
|
+
return _context2.delegateYield(_loop(), "t0", 37);
|
|
1178
|
+
case 37:
|
|
1179
|
+
if (!_context2.t0) {
|
|
1180
|
+
_context2.next = 39;
|
|
1181
|
+
break;
|
|
1182
|
+
}
|
|
1183
|
+
return _context2.abrupt("continue", 39);
|
|
1184
|
+
case 39:
|
|
1185
|
+
_context2.next = 35;
|
|
1186
|
+
break;
|
|
1187
|
+
case 41:
|
|
1188
|
+
_context2.next = 23;
|
|
1189
|
+
break;
|
|
1190
|
+
case 43:
|
|
1191
|
+
_context2.next = 45;
|
|
1192
|
+
return checkAllKeysExist(config);
|
|
1193
|
+
case 45:
|
|
1194
|
+
console.log("Successfully translated " + newKeys.length + " keys.");
|
|
1195
|
+
case 46:
|
|
1196
|
+
case "end":
|
|
1197
|
+
return _context2.stop();
|
|
1198
|
+
}
|
|
1199
|
+
}, _callee);
|
|
1200
|
+
}));
|
|
1201
|
+
return function translateMissing(_x) {
|
|
1202
|
+
return _ref.apply(this, arguments);
|
|
1203
|
+
};
|
|
1204
|
+
}();
|
|
1205
|
+
|
|
1206
|
+
var syncLocales = /*#__PURE__*/function () {
|
|
1207
|
+
var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config) {
|
|
1208
|
+
var loadPath, savePath, defaultLocale, namespaces, locales, context, openai, _iterator, _step, namespace, defaultLocaleKeys, _iterator2, _step2, locale, localeKeys, missingKeys, _i, _Object$entries, _Object$entries$_i, key, value, translatedValues, updatedLocaleKeys;
|
|
1209
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
1210
|
+
while (1) switch (_context.prev = _context.next) {
|
|
1211
|
+
case 0:
|
|
1212
|
+
loadPath = config.loadPath, savePath = config.savePath, defaultLocale = config.defaultLocale, namespaces = config.namespaces, locales = config.locales, context = config.context, openai = config.openai;
|
|
1213
|
+
_context.prev = 1;
|
|
1214
|
+
_iterator = _createForOfIteratorHelperLoose(namespaces);
|
|
1215
|
+
case 3:
|
|
1216
|
+
if ((_step = _iterator()).done) {
|
|
1217
|
+
_context.next = 62;
|
|
1218
|
+
break;
|
|
1219
|
+
}
|
|
1220
|
+
namespace = _step.value;
|
|
1221
|
+
defaultLocaleKeys = void 0;
|
|
1222
|
+
_context.prev = 6;
|
|
1223
|
+
_context.next = 9;
|
|
1224
|
+
return loadLocalesFile(loadPath, defaultLocale, namespace);
|
|
1225
|
+
case 9:
|
|
1226
|
+
defaultLocaleKeys = _context.sent;
|
|
1227
|
+
_context.next = 15;
|
|
1228
|
+
break;
|
|
1229
|
+
case 12:
|
|
1230
|
+
_context.prev = 12;
|
|
1231
|
+
_context.t0 = _context["catch"](6);
|
|
1232
|
+
throw new TranslationError("Failed to load default locale file for namespace \"" + namespace + "\"", defaultLocale, namespace, _context.t0 instanceof Error ? _context.t0 : undefined);
|
|
1233
|
+
case 15:
|
|
1234
|
+
_iterator2 = _createForOfIteratorHelperLoose(locales);
|
|
1235
|
+
case 16:
|
|
1236
|
+
if ((_step2 = _iterator2()).done) {
|
|
1237
|
+
_context.next = 60;
|
|
1238
|
+
break;
|
|
1239
|
+
}
|
|
1240
|
+
locale = _step2.value;
|
|
1241
|
+
if (!(locale === defaultLocale)) {
|
|
1242
|
+
_context.next = 20;
|
|
1243
|
+
break;
|
|
1244
|
+
}
|
|
1245
|
+
return _context.abrupt("continue", 58);
|
|
1246
|
+
case 20:
|
|
1247
|
+
localeKeys = void 0;
|
|
1248
|
+
_context.prev = 21;
|
|
1249
|
+
_context.next = 24;
|
|
1250
|
+
return loadLocalesFile(loadPath, locale, namespace);
|
|
1251
|
+
case 24:
|
|
1252
|
+
localeKeys = _context.sent;
|
|
1253
|
+
_context.next = 31;
|
|
1254
|
+
break;
|
|
1255
|
+
case 27:
|
|
1256
|
+
_context.prev = 27;
|
|
1257
|
+
_context.t1 = _context["catch"](21);
|
|
1258
|
+
console.warn("Warning: Could not load locale file for " + locale + " (namespace: " + namespace + "). Creating new file.");
|
|
1259
|
+
localeKeys = {};
|
|
1260
|
+
case 31:
|
|
1261
|
+
missingKeys = {}; // Check which keys from default locale are missing in current locale
|
|
1262
|
+
for (_i = 0, _Object$entries = Object.entries(defaultLocaleKeys); _i < _Object$entries.length; _i++) {
|
|
1263
|
+
_Object$entries$_i = _Object$entries[_i], key = _Object$entries$_i[0], value = _Object$entries$_i[1];
|
|
1264
|
+
if (!localeKeys[key]) {
|
|
1265
|
+
missingKeys[key] = value;
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
// If there are missing keys, translate them
|
|
1269
|
+
if (!(Object.keys(missingKeys).length > 0)) {
|
|
1270
|
+
_context.next = 57;
|
|
1271
|
+
break;
|
|
1272
|
+
}
|
|
1273
|
+
console.log("Found " + Object.keys(missingKeys).length + " missing keys in " + locale + " (namespace: " + namespace + ")");
|
|
1274
|
+
translatedValues = void 0;
|
|
1275
|
+
_context.prev = 36;
|
|
1276
|
+
_context.next = 39;
|
|
1277
|
+
return translateKey({
|
|
1278
|
+
inputLanguage: defaultLocale,
|
|
1279
|
+
outputLanguage: locale,
|
|
1280
|
+
context: context,
|
|
1281
|
+
object: missingKeys,
|
|
1282
|
+
openai: openai,
|
|
1283
|
+
model: config.model
|
|
1284
|
+
});
|
|
1285
|
+
case 39:
|
|
1286
|
+
translatedValues = _context.sent;
|
|
1287
|
+
_context.next = 45;
|
|
1288
|
+
break;
|
|
1289
|
+
case 42:
|
|
1290
|
+
_context.prev = 42;
|
|
1291
|
+
_context.t2 = _context["catch"](36);
|
|
1292
|
+
throw new TranslationError("Failed to translate keys for locale \"" + locale + "\" (namespace: " + namespace + ")", locale, namespace, _context.t2 instanceof Error ? _context.t2 : undefined);
|
|
1293
|
+
case 45:
|
|
1294
|
+
// Merge translated values with existing ones
|
|
1295
|
+
updatedLocaleKeys = _extends({}, localeKeys, translatedValues);
|
|
1296
|
+
_context.prev = 46;
|
|
1297
|
+
_context.next = 49;
|
|
1298
|
+
return writeLocalesFile(savePath, locale, namespace, updatedLocaleKeys);
|
|
1299
|
+
case 49:
|
|
1300
|
+
_context.next = 54;
|
|
1301
|
+
break;
|
|
1302
|
+
case 51:
|
|
1303
|
+
_context.prev = 51;
|
|
1304
|
+
_context.t3 = _context["catch"](46);
|
|
1305
|
+
throw new TranslationError("Failed to save translations for locale \"" + locale + "\" (namespace: " + namespace + ")", locale, namespace, _context.t3 instanceof Error ? _context.t3 : undefined);
|
|
1306
|
+
case 54:
|
|
1307
|
+
console.log("Successfully translated and saved " + Object.keys(missingKeys).length + " keys for " + locale + " (namespace: " + namespace + ")");
|
|
1308
|
+
_context.next = 58;
|
|
1309
|
+
break;
|
|
1310
|
+
case 57:
|
|
1311
|
+
console.log("No missing keys found for " + locale + " (namespace: " + namespace + ")");
|
|
1312
|
+
case 58:
|
|
1313
|
+
_context.next = 16;
|
|
1314
|
+
break;
|
|
1315
|
+
case 60:
|
|
1316
|
+
_context.next = 3;
|
|
1317
|
+
break;
|
|
1318
|
+
case 62:
|
|
1319
|
+
_context.next = 69;
|
|
1320
|
+
break;
|
|
1321
|
+
case 64:
|
|
1322
|
+
_context.prev = 64;
|
|
1323
|
+
_context.t4 = _context["catch"](1);
|
|
1324
|
+
if (!(_context.t4 instanceof TranslationError)) {
|
|
1325
|
+
_context.next = 68;
|
|
1326
|
+
break;
|
|
1327
|
+
}
|
|
1328
|
+
throw _context.t4;
|
|
1329
|
+
case 68:
|
|
1330
|
+
throw new TranslationError("An unexpected error occurred during translation", undefined, undefined, _context.t4 instanceof Error ? _context.t4 : undefined);
|
|
1331
|
+
case 69:
|
|
1332
|
+
case "end":
|
|
1333
|
+
return _context.stop();
|
|
1334
|
+
}
|
|
1335
|
+
}, _callee, null, [[1, 64], [6, 12], [21, 27], [36, 42], [46, 51]]);
|
|
1336
|
+
}));
|
|
1337
|
+
return function syncLocales(_x) {
|
|
1338
|
+
return _ref.apply(this, arguments);
|
|
1339
|
+
};
|
|
1340
|
+
}();
|
|
1341
|
+
|
|
1342
|
+
// Only run CLI initialization when this file is executed directly
|
|
1343
|
+
var program = /*#__PURE__*/new Command();
|
|
1344
|
+
program.name("i18n-magic").description("CLI to help you manage your locales JSON with translations, replacements, etc. with OpenAI.").version("0.2.0").option("-c, --config <path>", "path to config file").option("-e, --env <path>", "path to .env file");
|
|
1345
|
+
var commands = [{
|
|
1346
|
+
name: "scan",
|
|
1347
|
+
description: "Scan for missing translations, get prompted for each, translate it to the other locales and save it to the JSON file.",
|
|
1348
|
+
action: translateMissing
|
|
1349
|
+
}, {
|
|
1350
|
+
name: "replace",
|
|
1351
|
+
description: "Replace a translation based on the key, and translate it to the other locales and save it to the JSON file.",
|
|
1352
|
+
action: replaceTranslation
|
|
1353
|
+
}, {
|
|
1354
|
+
name: "check-missing",
|
|
1355
|
+
description: "Check if there are any missing translations. Useful for a CI/CD pipeline or husky hook.",
|
|
1356
|
+
action: checkMissing
|
|
1357
|
+
}, {
|
|
1358
|
+
name: "sync",
|
|
1359
|
+
description: "Sync the translations from the default locale to the other locales. Useful for a CI/CD pipeline or husky hook.",
|
|
1360
|
+
action: syncLocales
|
|
1361
|
+
}];
|
|
1362
|
+
var _loop = function _loop() {
|
|
1363
|
+
var command = _commands[_i];
|
|
1364
|
+
program.command(command.name).description(command.description).action( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
1365
|
+
var _config$model;
|
|
1366
|
+
var res, config, isGemini, openaiKey, geminiKey, key, keyType, openai;
|
|
1367
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
1368
|
+
while (1) switch (_context.prev = _context.next) {
|
|
1369
|
+
case 0:
|
|
1370
|
+
res = dotenv.config({
|
|
1371
|
+
path: program.opts().env || ".env"
|
|
1372
|
+
});
|
|
1373
|
+
_context.next = 3;
|
|
1374
|
+
return loadConfig({
|
|
1375
|
+
configPath: program.opts().config
|
|
1376
|
+
});
|
|
1377
|
+
case 3:
|
|
1378
|
+
config = _context.sent;
|
|
1379
|
+
isGemini = (_config$model = config.model) == null ? void 0 : _config$model.includes("gemini"); // Get API key from environment or config
|
|
1380
|
+
openaiKey = res.parsed.OPENAI_API_KEY || config.OPENAI_API_KEY;
|
|
1381
|
+
geminiKey = res.parsed.GEMINI_API_KEY || config.GEMINI_API_KEY; // Select appropriate key based on model type
|
|
1382
|
+
key = isGemini ? geminiKey : openaiKey;
|
|
1383
|
+
if (!key) {
|
|
1384
|
+
keyType = isGemini ? "GEMINI_API_KEY" : "OPENAI_API_KEY";
|
|
1385
|
+
console.error("Please provide a" + (isGemini ? " Gemini" : "n OpenAI") + " API key in your .env file or config, called " + keyType + ".");
|
|
1386
|
+
process.exit(1);
|
|
1387
|
+
}
|
|
1388
|
+
openai = new OpenAI(_extends({
|
|
1389
|
+
apiKey: key
|
|
1390
|
+
}, isGemini && {
|
|
1391
|
+
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/"
|
|
1392
|
+
}));
|
|
1393
|
+
command.action(_extends({}, config, {
|
|
1394
|
+
openai: openai
|
|
1395
|
+
}));
|
|
1396
|
+
case 11:
|
|
1397
|
+
case "end":
|
|
1398
|
+
return _context.stop();
|
|
1399
|
+
}
|
|
1400
|
+
}, _callee);
|
|
1401
|
+
})));
|
|
1402
|
+
};
|
|
1403
|
+
for (var _i = 0, _commands = commands; _i < _commands.length; _i++) {
|
|
1404
|
+
_loop();
|
|
1405
|
+
}
|
|
1406
|
+
program.parse(process.argv);
|
|
1407
|
+
//# sourceMappingURL=i18n-magic.esm.js.map
|