@scoutello/i18n-magic 0.16.0 → 0.19.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 +207 -19
- package/dist/cli.d.ts +2 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +101 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands/check-missing.d.ts +1 -0
- package/dist/commands/check-missing.d.ts.map +1 -0
- package/dist/commands/check-missing.js +13 -0
- package/dist/commands/check-missing.js.map +1 -0
- package/dist/commands/clean.d.ts +1 -0
- package/dist/commands/clean.d.ts.map +1 -0
- package/dist/commands/clean.js +82 -0
- package/dist/commands/clean.js.map +1 -0
- package/dist/commands/create-pruned-namespace-automated.d.ts +20 -0
- package/dist/commands/create-pruned-namespace-automated.d.ts.map +1 -0
- package/dist/commands/create-pruned-namespace-automated.js +98 -0
- package/dist/commands/create-pruned-namespace-automated.js.map +1 -0
- package/dist/commands/create-pruned-namespace.d.ts +3 -0
- package/dist/commands/create-pruned-namespace.d.ts.map +1 -0
- package/dist/commands/create-pruned-namespace.js +123 -0
- package/dist/commands/create-pruned-namespace.js.map +1 -0
- package/dist/commands/replace.d.ts +1 -0
- package/dist/commands/replace.d.ts.map +1 -0
- package/dist/commands/replace.js +58 -0
- package/dist/commands/replace.js.map +1 -0
- package/dist/commands/scan.d.ts +1 -0
- package/dist/commands/scan.d.ts.map +1 -0
- package/dist/commands/scan.js +70 -0
- package/dist/commands/scan.js.map +1 -0
- package/dist/commands/sync-locales.d.ts +1 -0
- package/dist/commands/sync-locales.d.ts.map +1 -0
- package/dist/commands/sync-locales.js +78 -0
- package/dist/commands/sync-locales.js.map +1 -0
- package/dist/i18n-magic.cjs.development.js +348 -130
- package/dist/i18n-magic.cjs.development.js.map +1 -1
- package/dist/i18n-magic.cjs.production.min.js +1 -1
- package/dist/i18n-magic.cjs.production.min.js.map +1 -1
- package/dist/i18n-magic.esm.js +339 -130
- package/dist/i18n-magic.esm.js.map +1 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -9
- package/dist/index.js.map +1 -0
- package/dist/lib/languges.d.ts +1 -0
- package/dist/lib/languges.d.ts.map +1 -0
- package/dist/lib/languges.js +146 -0
- package/dist/lib/languges.js.map +1 -0
- package/dist/lib/types.d.ts +7 -1
- package/dist/lib/types.d.ts.map +1 -0
- package/dist/lib/types.js +3 -0
- package/dist/lib/types.js.map +1 -0
- package/dist/lib/utils.d.ts +20 -1
- package/dist/lib/utils.d.ts.map +1 -0
- package/dist/lib/utils.js +264 -0
- package/dist/lib/utils.js.map +1 -0
- package/package.json +38 -14
- package/src/cli.ts +117 -0
- package/src/commands/clean.ts +4 -1
- package/src/commands/create-pruned-namespace-automated.ts +165 -0
- package/src/commands/create-pruned-namespace.ts +168 -0
- package/src/commands/scan.ts +12 -0
- package/src/index.ts +23 -113
- package/src/lib/types.ts +7 -1
- package/src/lib/utils.ts +59 -2
package/dist/i18n-magic.esm.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import { Command } from 'commander';
|
|
2
|
-
import dotenv from 'dotenv';
|
|
3
|
-
import OpenAI from 'openai';
|
|
4
1
|
import glob from 'fast-glob';
|
|
5
2
|
import { Parser } from 'i18next-scanner';
|
|
6
3
|
import fs from 'node:fs';
|
|
7
4
|
import path from 'node:path';
|
|
8
5
|
import prompts from 'prompts';
|
|
6
|
+
import console$1 from 'console';
|
|
9
7
|
|
|
10
8
|
function _construct(t, e, r) {
|
|
11
9
|
if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments);
|
|
@@ -1075,6 +1073,293 @@ var removeUnusedKeys = /*#__PURE__*/function () {
|
|
|
1075
1073
|
};
|
|
1076
1074
|
}();
|
|
1077
1075
|
|
|
1076
|
+
var createPrunedNamespace = /*#__PURE__*/function () {
|
|
1077
|
+
var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config) {
|
|
1078
|
+
var namespaces, loadPath, savePath, locales, defaultNamespace, sourceNamespaceResponse, sourceNamespace, newNamespaceResponse, newNamespace, globPatternsResponse, selectedGlobPatterns, parser, files, extractedKeys, _iterator, _step, file, content, uniqueExtractedKeys, relevantKeys, _iterator2, _step2, key, pureKey, _iterator3, _step3, locale, sourceTranslations, newNamespaceTranslations, _iterator4, _step4, _key;
|
|
1079
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
1080
|
+
while (1) switch (_context.prev = _context.next) {
|
|
1081
|
+
case 0:
|
|
1082
|
+
namespaces = config.namespaces, loadPath = config.loadPath, savePath = config.savePath, locales = config.locales, defaultNamespace = config.defaultNamespace; // Step 1: Ask for source namespace
|
|
1083
|
+
_context.next = 3;
|
|
1084
|
+
return prompts({
|
|
1085
|
+
type: "select",
|
|
1086
|
+
name: "value",
|
|
1087
|
+
message: "Select source namespace to create pruned version from:",
|
|
1088
|
+
choices: namespaces.map(function (namespace) {
|
|
1089
|
+
return {
|
|
1090
|
+
title: namespace,
|
|
1091
|
+
value: namespace
|
|
1092
|
+
};
|
|
1093
|
+
}),
|
|
1094
|
+
onState: function onState(state) {
|
|
1095
|
+
if (state.aborted) {
|
|
1096
|
+
process.nextTick(function () {
|
|
1097
|
+
process.exit(0);
|
|
1098
|
+
});
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
});
|
|
1102
|
+
case 3:
|
|
1103
|
+
sourceNamespaceResponse = _context.sent;
|
|
1104
|
+
sourceNamespace = sourceNamespaceResponse.value; // Step 2: Ask for new namespace name
|
|
1105
|
+
_context.next = 7;
|
|
1106
|
+
return prompts({
|
|
1107
|
+
type: "text",
|
|
1108
|
+
name: "value",
|
|
1109
|
+
message: "Enter the name for the new namespace:",
|
|
1110
|
+
validate: function validate(value) {
|
|
1111
|
+
if (!value) return "Namespace name cannot be empty";
|
|
1112
|
+
if (namespaces.includes(value)) return "Namespace already exists";
|
|
1113
|
+
return true;
|
|
1114
|
+
},
|
|
1115
|
+
onState: function onState(state) {
|
|
1116
|
+
if (state.aborted) {
|
|
1117
|
+
process.nextTick(function () {
|
|
1118
|
+
process.exit(0);
|
|
1119
|
+
});
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
});
|
|
1123
|
+
case 7:
|
|
1124
|
+
newNamespaceResponse = _context.sent;
|
|
1125
|
+
newNamespace = newNamespaceResponse.value; // Step 3: Ask for glob patterns to find relevant keys
|
|
1126
|
+
_context.next = 11;
|
|
1127
|
+
return prompts({
|
|
1128
|
+
type: "list",
|
|
1129
|
+
name: "value",
|
|
1130
|
+
message: "Enter glob patterns to find relevant keys (comma separated):",
|
|
1131
|
+
initial: config.globPatterns.join(","),
|
|
1132
|
+
separator: ",",
|
|
1133
|
+
onState: function onState(state) {
|
|
1134
|
+
if (state.aborted) {
|
|
1135
|
+
process.nextTick(function () {
|
|
1136
|
+
process.exit(0);
|
|
1137
|
+
});
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
});
|
|
1141
|
+
case 11:
|
|
1142
|
+
globPatternsResponse = _context.sent;
|
|
1143
|
+
selectedGlobPatterns = globPatternsResponse.value;
|
|
1144
|
+
console.log("Finding keys used in files matching: " + selectedGlobPatterns.join(", "));
|
|
1145
|
+
// Extract keys from files matching the glob patterns
|
|
1146
|
+
parser = new Parser({
|
|
1147
|
+
nsSeparator: false,
|
|
1148
|
+
keySeparator: false
|
|
1149
|
+
});
|
|
1150
|
+
_context.next = 17;
|
|
1151
|
+
return glob([].concat(selectedGlobPatterns, ["!**/node_modules/**"]));
|
|
1152
|
+
case 17:
|
|
1153
|
+
files = _context.sent;
|
|
1154
|
+
console.log("Found " + files.length + " files to scan");
|
|
1155
|
+
extractedKeys = [];
|
|
1156
|
+
for (_iterator = _createForOfIteratorHelperLoose(files); !(_step = _iterator()).done;) {
|
|
1157
|
+
file = _step.value;
|
|
1158
|
+
content = fs.readFileSync(file, "utf-8");
|
|
1159
|
+
parser.parseFuncFromString(content, {
|
|
1160
|
+
list: ["t"]
|
|
1161
|
+
}, function (key) {
|
|
1162
|
+
extractedKeys.push(key);
|
|
1163
|
+
});
|
|
1164
|
+
}
|
|
1165
|
+
uniqueExtractedKeys = removeDuplicatesFromArray(extractedKeys);
|
|
1166
|
+
console.log("Found " + uniqueExtractedKeys.length + " unique translation keys");
|
|
1167
|
+
// Filter keys that belong to the source namespace
|
|
1168
|
+
relevantKeys = [];
|
|
1169
|
+
for (_iterator2 = _createForOfIteratorHelperLoose(uniqueExtractedKeys); !(_step2 = _iterator2()).done;) {
|
|
1170
|
+
key = _step2.value;
|
|
1171
|
+
pureKey = getPureKey(key, sourceNamespace, sourceNamespace === defaultNamespace);
|
|
1172
|
+
if (pureKey) {
|
|
1173
|
+
relevantKeys.push(pureKey);
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
console.log("Found " + relevantKeys.length + " keys from namespace '" + sourceNamespace + "'");
|
|
1177
|
+
if (!(relevantKeys.length === 0)) {
|
|
1178
|
+
_context.next = 29;
|
|
1179
|
+
break;
|
|
1180
|
+
}
|
|
1181
|
+
console.log("No relevant keys found. Exiting...");
|
|
1182
|
+
return _context.abrupt("return");
|
|
1183
|
+
case 29:
|
|
1184
|
+
_iterator3 = _createForOfIteratorHelperLoose(locales);
|
|
1185
|
+
case 30:
|
|
1186
|
+
if ((_step3 = _iterator3()).done) {
|
|
1187
|
+
_context.next = 48;
|
|
1188
|
+
break;
|
|
1189
|
+
}
|
|
1190
|
+
locale = _step3.value;
|
|
1191
|
+
_context.prev = 32;
|
|
1192
|
+
_context.next = 35;
|
|
1193
|
+
return loadLocalesFile(loadPath, locale, sourceNamespace);
|
|
1194
|
+
case 35:
|
|
1195
|
+
sourceTranslations = _context.sent;
|
|
1196
|
+
// Create new namespace with only the keys used in the glob pattern files
|
|
1197
|
+
newNamespaceTranslations = {};
|
|
1198
|
+
for (_iterator4 = _createForOfIteratorHelperLoose(relevantKeys); !(_step4 = _iterator4()).done;) {
|
|
1199
|
+
_key = _step4.value;
|
|
1200
|
+
if (sourceTranslations[_key]) {
|
|
1201
|
+
newNamespaceTranslations[_key] = sourceTranslations[_key];
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
// Write the new namespace file
|
|
1205
|
+
_context.next = 40;
|
|
1206
|
+
return writeLocalesFile(savePath, locale, newNamespace, newNamespaceTranslations);
|
|
1207
|
+
case 40:
|
|
1208
|
+
console.log("Created pruned namespace '" + newNamespace + "' for locale '" + locale + "' with " + Object.keys(newNamespaceTranslations).length + " keys");
|
|
1209
|
+
_context.next = 46;
|
|
1210
|
+
break;
|
|
1211
|
+
case 43:
|
|
1212
|
+
_context.prev = 43;
|
|
1213
|
+
_context.t0 = _context["catch"](32);
|
|
1214
|
+
console.error("Error creating pruned namespace for locale '" + locale + "':", _context.t0);
|
|
1215
|
+
case 46:
|
|
1216
|
+
_context.next = 30;
|
|
1217
|
+
break;
|
|
1218
|
+
case 48:
|
|
1219
|
+
console.log("\u2705 Successfully created pruned namespace '" + newNamespace + "'");
|
|
1220
|
+
case 49:
|
|
1221
|
+
case "end":
|
|
1222
|
+
return _context.stop();
|
|
1223
|
+
}
|
|
1224
|
+
}, _callee, null, [[32, 43]]);
|
|
1225
|
+
}));
|
|
1226
|
+
return function createPrunedNamespace(_x) {
|
|
1227
|
+
return _ref.apply(this, arguments);
|
|
1228
|
+
};
|
|
1229
|
+
}();
|
|
1230
|
+
|
|
1231
|
+
var createPrunedNamespaceAutomated = /*#__PURE__*/function () {
|
|
1232
|
+
var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, options) {
|
|
1233
|
+
var namespaces, loadPath, savePath, locales, defaultNamespace, sourceNamespace, newNamespace, globPatterns, parser, files, extractedKeys, _iterator, _step, file, content, uniqueExtractedKeys, relevantKeys, _iterator2, _step2, key, pureKey, results, _iterator3, _step3, locale, sourceTranslations, newNamespaceTranslations, _iterator4, _step4, _key, keyCount;
|
|
1234
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
1235
|
+
while (1) switch (_context.prev = _context.next) {
|
|
1236
|
+
case 0:
|
|
1237
|
+
namespaces = config.namespaces, loadPath = config.loadPath, savePath = config.savePath, locales = config.locales, defaultNamespace = config.defaultNamespace;
|
|
1238
|
+
sourceNamespace = options.sourceNamespace, newNamespace = options.newNamespace, globPatterns = options.globPatterns; // Validate inputs
|
|
1239
|
+
if (namespaces.includes(sourceNamespace)) {
|
|
1240
|
+
_context.next = 4;
|
|
1241
|
+
break;
|
|
1242
|
+
}
|
|
1243
|
+
throw new Error("Source namespace '" + sourceNamespace + "' not found in configuration");
|
|
1244
|
+
case 4:
|
|
1245
|
+
if (!namespaces.includes(newNamespace)) {
|
|
1246
|
+
_context.next = 6;
|
|
1247
|
+
break;
|
|
1248
|
+
}
|
|
1249
|
+
throw new Error("Namespace '" + newNamespace + "' already exists");
|
|
1250
|
+
case 6:
|
|
1251
|
+
console.log("Creating pruned namespace '" + newNamespace + "' from '" + sourceNamespace + "'");
|
|
1252
|
+
console.log("Using glob patterns: " + globPatterns.join(", "));
|
|
1253
|
+
// Extract keys from files matching the glob patterns
|
|
1254
|
+
parser = new Parser({
|
|
1255
|
+
nsSeparator: false,
|
|
1256
|
+
keySeparator: false
|
|
1257
|
+
});
|
|
1258
|
+
_context.next = 11;
|
|
1259
|
+
return glob([].concat(globPatterns, ["!**/node_modules/**"]));
|
|
1260
|
+
case 11:
|
|
1261
|
+
files = _context.sent;
|
|
1262
|
+
console.log("Found " + files.length + " files to scan");
|
|
1263
|
+
extractedKeys = [];
|
|
1264
|
+
for (_iterator = _createForOfIteratorHelperLoose(files); !(_step = _iterator()).done;) {
|
|
1265
|
+
file = _step.value;
|
|
1266
|
+
content = fs.readFileSync(file, "utf-8");
|
|
1267
|
+
parser.parseFuncFromString(content, {
|
|
1268
|
+
list: ["t"]
|
|
1269
|
+
}, function (key) {
|
|
1270
|
+
extractedKeys.push(key);
|
|
1271
|
+
});
|
|
1272
|
+
}
|
|
1273
|
+
uniqueExtractedKeys = removeDuplicatesFromArray(extractedKeys);
|
|
1274
|
+
console.log("Found " + uniqueExtractedKeys.length + " unique translation keys");
|
|
1275
|
+
// Filter keys that belong to the source namespace
|
|
1276
|
+
relevantKeys = [];
|
|
1277
|
+
for (_iterator2 = _createForOfIteratorHelperLoose(uniqueExtractedKeys); !(_step2 = _iterator2()).done;) {
|
|
1278
|
+
key = _step2.value;
|
|
1279
|
+
pureKey = getPureKey(key, sourceNamespace, sourceNamespace === defaultNamespace);
|
|
1280
|
+
if (pureKey) {
|
|
1281
|
+
relevantKeys.push(pureKey);
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
console.log("Found " + relevantKeys.length + " keys from namespace '" + sourceNamespace + "'");
|
|
1285
|
+
if (!(relevantKeys.length === 0)) {
|
|
1286
|
+
_context.next = 23;
|
|
1287
|
+
break;
|
|
1288
|
+
}
|
|
1289
|
+
console.log("No relevant keys found. Exiting...");
|
|
1290
|
+
return _context.abrupt("return", {
|
|
1291
|
+
success: false,
|
|
1292
|
+
message: "No relevant keys found",
|
|
1293
|
+
keysCount: 0
|
|
1294
|
+
});
|
|
1295
|
+
case 23:
|
|
1296
|
+
// Get translations from source namespace and create new namespace files
|
|
1297
|
+
results = [];
|
|
1298
|
+
_iterator3 = _createForOfIteratorHelperLoose(locales);
|
|
1299
|
+
case 25:
|
|
1300
|
+
if ((_step3 = _iterator3()).done) {
|
|
1301
|
+
_context.next = 46;
|
|
1302
|
+
break;
|
|
1303
|
+
}
|
|
1304
|
+
locale = _step3.value;
|
|
1305
|
+
_context.prev = 27;
|
|
1306
|
+
_context.next = 30;
|
|
1307
|
+
return loadLocalesFile(loadPath, locale, sourceNamespace);
|
|
1308
|
+
case 30:
|
|
1309
|
+
sourceTranslations = _context.sent;
|
|
1310
|
+
// Create new namespace with only the keys used in the glob pattern files
|
|
1311
|
+
newNamespaceTranslations = {};
|
|
1312
|
+
for (_iterator4 = _createForOfIteratorHelperLoose(relevantKeys); !(_step4 = _iterator4()).done;) {
|
|
1313
|
+
_key = _step4.value;
|
|
1314
|
+
if (sourceTranslations[_key]) {
|
|
1315
|
+
newNamespaceTranslations[_key] = sourceTranslations[_key];
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
// Write the new namespace file
|
|
1319
|
+
_context.next = 35;
|
|
1320
|
+
return writeLocalesFile(savePath, locale, newNamespace, newNamespaceTranslations);
|
|
1321
|
+
case 35:
|
|
1322
|
+
keyCount = Object.keys(newNamespaceTranslations).length;
|
|
1323
|
+
console.log("Created pruned namespace '" + newNamespace + "' for locale '" + locale + "' with " + keyCount + " keys");
|
|
1324
|
+
results.push({
|
|
1325
|
+
locale: locale,
|
|
1326
|
+
keyCount: keyCount,
|
|
1327
|
+
success: true
|
|
1328
|
+
});
|
|
1329
|
+
_context.next = 44;
|
|
1330
|
+
break;
|
|
1331
|
+
case 40:
|
|
1332
|
+
_context.prev = 40;
|
|
1333
|
+
_context.t0 = _context["catch"](27);
|
|
1334
|
+
console.error("Error creating pruned namespace for locale '" + locale + "':", _context.t0);
|
|
1335
|
+
results.push({
|
|
1336
|
+
locale: locale,
|
|
1337
|
+
keyCount: 0,
|
|
1338
|
+
success: false,
|
|
1339
|
+
error: _context.t0 instanceof Error ? _context.t0.message : String(_context.t0)
|
|
1340
|
+
});
|
|
1341
|
+
case 44:
|
|
1342
|
+
_context.next = 25;
|
|
1343
|
+
break;
|
|
1344
|
+
case 46:
|
|
1345
|
+
console.log("\u2705 Successfully created pruned namespace '" + newNamespace + "'");
|
|
1346
|
+
return _context.abrupt("return", {
|
|
1347
|
+
success: true,
|
|
1348
|
+
message: "Created pruned namespace '" + newNamespace + "' with " + relevantKeys.length + " keys",
|
|
1349
|
+
keysCount: relevantKeys.length,
|
|
1350
|
+
results: results
|
|
1351
|
+
});
|
|
1352
|
+
case 48:
|
|
1353
|
+
case "end":
|
|
1354
|
+
return _context.stop();
|
|
1355
|
+
}
|
|
1356
|
+
}, _callee, null, [[27, 40]]);
|
|
1357
|
+
}));
|
|
1358
|
+
return function createPrunedNamespaceAutomated(_x, _x2) {
|
|
1359
|
+
return _ref.apply(this, arguments);
|
|
1360
|
+
};
|
|
1361
|
+
}();
|
|
1362
|
+
|
|
1078
1363
|
var getKeyToReplace = /*#__PURE__*/function () {
|
|
1079
1364
|
var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(keys) {
|
|
1080
1365
|
var keyToReplace;
|
|
@@ -1202,69 +1487,79 @@ var replaceTranslation = /*#__PURE__*/function () {
|
|
|
1202
1487
|
|
|
1203
1488
|
var translateMissing = /*#__PURE__*/function () {
|
|
1204
1489
|
var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config) {
|
|
1205
|
-
var loadPath, savePath, defaultLocale, namespaces, locales, context, openai, disableTranslation, newKeys, newKeysWithDefaultLocale, _iterator, _step, newKey, answer, newKeysObject, allLocales, _iterator2, _step2, locale, translatedValues, _loop, _iterator3, _step3;
|
|
1490
|
+
var loadPath, savePath, defaultLocale, namespaces, locales, context, openai, disableTranslation, autoClear, newKeys, newKeysWithDefaultLocale, _iterator, _step, newKey, answer, newKeysObject, allLocales, _iterator2, _step2, locale, translatedValues, _loop, _iterator3, _step3;
|
|
1206
1491
|
return _regeneratorRuntime().wrap(function _callee$(_context2) {
|
|
1207
1492
|
while (1) switch (_context2.prev = _context2.next) {
|
|
1208
1493
|
case 0:
|
|
1209
|
-
loadPath = config.loadPath, savePath = config.savePath, defaultLocale = config.defaultLocale, namespaces = config.namespaces, locales = config.locales, context = config.context, openai = config.openai, disableTranslation = config.disableTranslation;
|
|
1210
|
-
|
|
1494
|
+
loadPath = config.loadPath, savePath = config.savePath, defaultLocale = config.defaultLocale, namespaces = config.namespaces, locales = config.locales, context = config.context, openai = config.openai, disableTranslation = config.disableTranslation, autoClear = config.autoClear; // Run clean command first if autoClear is enabled
|
|
1495
|
+
if (!autoClear) {
|
|
1496
|
+
_context2.next = 6;
|
|
1497
|
+
break;
|
|
1498
|
+
}
|
|
1499
|
+
console$1.log("🧹 Auto-clearing unused translations before scanning...");
|
|
1500
|
+
_context2.next = 5;
|
|
1501
|
+
return removeUnusedKeys(config);
|
|
1502
|
+
case 5:
|
|
1503
|
+
console$1.log("✅ Auto-clear completed. Now scanning for missing translations...\n");
|
|
1504
|
+
case 6:
|
|
1505
|
+
_context2.next = 8;
|
|
1211
1506
|
return getMissingKeys(config);
|
|
1212
|
-
case
|
|
1507
|
+
case 8:
|
|
1213
1508
|
newKeys = _context2.sent;
|
|
1214
1509
|
if (!(newKeys.length === 0)) {
|
|
1215
|
-
_context2.next =
|
|
1510
|
+
_context2.next = 14;
|
|
1216
1511
|
break;
|
|
1217
1512
|
}
|
|
1218
|
-
console.log("No new keys found.");
|
|
1219
|
-
_context2.next =
|
|
1513
|
+
console$1.log("No new keys found.");
|
|
1514
|
+
_context2.next = 13;
|
|
1220
1515
|
return checkAllKeysExist(config);
|
|
1221
|
-
case
|
|
1516
|
+
case 13:
|
|
1222
1517
|
return _context2.abrupt("return");
|
|
1223
|
-
case
|
|
1224
|
-
console.log(newKeys.length + " keys are missing. Please provide the values for the following keys in " + defaultLocale + ":");
|
|
1518
|
+
case 14:
|
|
1519
|
+
console$1.log(newKeys.length + " keys are missing. Please provide the values for the following keys in " + defaultLocale + ":");
|
|
1225
1520
|
newKeysWithDefaultLocale = [];
|
|
1226
1521
|
_iterator = _createForOfIteratorHelperLoose(newKeys);
|
|
1227
|
-
case
|
|
1522
|
+
case 17:
|
|
1228
1523
|
if ((_step = _iterator()).done) {
|
|
1229
|
-
_context2.next =
|
|
1524
|
+
_context2.next = 25;
|
|
1230
1525
|
break;
|
|
1231
1526
|
}
|
|
1232
1527
|
newKey = _step.value;
|
|
1233
|
-
_context2.next =
|
|
1528
|
+
_context2.next = 21;
|
|
1234
1529
|
return getTextInput(newKey.key);
|
|
1235
|
-
case
|
|
1530
|
+
case 21:
|
|
1236
1531
|
answer = _context2.sent;
|
|
1237
1532
|
newKeysWithDefaultLocale.push({
|
|
1238
1533
|
key: newKey.key,
|
|
1239
1534
|
namespace: newKey.namespace,
|
|
1240
1535
|
value: answer
|
|
1241
1536
|
});
|
|
1242
|
-
case
|
|
1243
|
-
_context2.next =
|
|
1537
|
+
case 23:
|
|
1538
|
+
_context2.next = 17;
|
|
1244
1539
|
break;
|
|
1245
|
-
case
|
|
1540
|
+
case 25:
|
|
1246
1541
|
newKeysObject = newKeysWithDefaultLocale.reduce(function (prev, next) {
|
|
1247
1542
|
prev[next.key] = next.value;
|
|
1248
1543
|
return prev;
|
|
1249
1544
|
}, {});
|
|
1250
1545
|
allLocales = disableTranslation ? [defaultLocale] : locales;
|
|
1251
1546
|
_iterator2 = _createForOfIteratorHelperLoose(allLocales);
|
|
1252
|
-
case
|
|
1547
|
+
case 28:
|
|
1253
1548
|
if ((_step2 = _iterator2()).done) {
|
|
1254
|
-
_context2.next =
|
|
1549
|
+
_context2.next = 48;
|
|
1255
1550
|
break;
|
|
1256
1551
|
}
|
|
1257
1552
|
locale = _step2.value;
|
|
1258
1553
|
translatedValues = {};
|
|
1259
1554
|
if (!(locale === defaultLocale)) {
|
|
1260
|
-
_context2.next =
|
|
1555
|
+
_context2.next = 35;
|
|
1261
1556
|
break;
|
|
1262
1557
|
}
|
|
1263
1558
|
translatedValues = newKeysObject;
|
|
1264
|
-
_context2.next =
|
|
1559
|
+
_context2.next = 38;
|
|
1265
1560
|
break;
|
|
1266
|
-
case
|
|
1267
|
-
_context2.next =
|
|
1561
|
+
case 35:
|
|
1562
|
+
_context2.next = 37;
|
|
1268
1563
|
return translateKey({
|
|
1269
1564
|
inputLanguage: defaultLocale,
|
|
1270
1565
|
outputLanguage: locale,
|
|
@@ -1273,9 +1568,9 @@ var translateMissing = /*#__PURE__*/function () {
|
|
|
1273
1568
|
openai: openai,
|
|
1274
1569
|
model: config.model
|
|
1275
1570
|
});
|
|
1276
|
-
case
|
|
1571
|
+
case 37:
|
|
1277
1572
|
translatedValues = _context2.sent;
|
|
1278
|
-
case
|
|
1573
|
+
case 38:
|
|
1279
1574
|
_loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
|
|
1280
1575
|
var namespace, existingKeys, relevantKeys, _iterator4, _step4, key;
|
|
1281
1576
|
return _regeneratorRuntime().wrap(function _loop$(_context) {
|
|
@@ -1307,30 +1602,30 @@ var translateMissing = /*#__PURE__*/function () {
|
|
|
1307
1602
|
}, _loop);
|
|
1308
1603
|
});
|
|
1309
1604
|
_iterator3 = _createForOfIteratorHelperLoose(namespaces);
|
|
1310
|
-
case
|
|
1605
|
+
case 40:
|
|
1311
1606
|
if ((_step3 = _iterator3()).done) {
|
|
1312
|
-
_context2.next =
|
|
1607
|
+
_context2.next = 46;
|
|
1313
1608
|
break;
|
|
1314
1609
|
}
|
|
1315
|
-
return _context2.delegateYield(_loop(), "t0",
|
|
1316
|
-
case
|
|
1610
|
+
return _context2.delegateYield(_loop(), "t0", 42);
|
|
1611
|
+
case 42:
|
|
1317
1612
|
if (!_context2.t0) {
|
|
1318
|
-
_context2.next =
|
|
1613
|
+
_context2.next = 44;
|
|
1319
1614
|
break;
|
|
1320
1615
|
}
|
|
1321
|
-
return _context2.abrupt("continue",
|
|
1322
|
-
case
|
|
1323
|
-
_context2.next =
|
|
1616
|
+
return _context2.abrupt("continue", 44);
|
|
1617
|
+
case 44:
|
|
1618
|
+
_context2.next = 40;
|
|
1324
1619
|
break;
|
|
1325
|
-
case
|
|
1326
|
-
_context2.next =
|
|
1620
|
+
case 46:
|
|
1621
|
+
_context2.next = 28;
|
|
1327
1622
|
break;
|
|
1328
|
-
case
|
|
1329
|
-
_context2.next =
|
|
1623
|
+
case 48:
|
|
1624
|
+
_context2.next = 50;
|
|
1330
1625
|
return checkAllKeysExist(config);
|
|
1331
|
-
case
|
|
1332
|
-
console.log("Successfully translated " + newKeys.length + " keys.");
|
|
1333
|
-
case
|
|
1626
|
+
case 50:
|
|
1627
|
+
console$1.log("Successfully translated " + newKeys.length + " keys.");
|
|
1628
|
+
case 51:
|
|
1334
1629
|
case "end":
|
|
1335
1630
|
return _context2.stop();
|
|
1336
1631
|
}
|
|
@@ -1477,91 +1772,5 @@ var syncLocales = /*#__PURE__*/function () {
|
|
|
1477
1772
|
};
|
|
1478
1773
|
}();
|
|
1479
1774
|
|
|
1480
|
-
|
|
1481
|
-
var program = /*#__PURE__*/new Command();
|
|
1482
|
-
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");
|
|
1483
|
-
var commands = [{
|
|
1484
|
-
name: "scan",
|
|
1485
|
-
description: "Scan for missing translations, get prompted for each, translate it to the other locales and save it to the JSON file.",
|
|
1486
|
-
action: translateMissing
|
|
1487
|
-
}, {
|
|
1488
|
-
name: "replace",
|
|
1489
|
-
description: "Replace a translation based on the key, and translate it to the other locales and save it to the JSON file.",
|
|
1490
|
-
action: replaceTranslation
|
|
1491
|
-
}, {
|
|
1492
|
-
name: "check-missing",
|
|
1493
|
-
description: "Check if there are any missing translations. Useful for a CI/CD pipeline or husky hook.",
|
|
1494
|
-
action: checkMissing
|
|
1495
|
-
}, {
|
|
1496
|
-
name: "sync",
|
|
1497
|
-
description: "Sync the translations from the default locale to the other locales. Useful for a CI/CD pipeline or husky hook.",
|
|
1498
|
-
action: syncLocales
|
|
1499
|
-
}, {
|
|
1500
|
-
name: "clean",
|
|
1501
|
-
description: "Remove unused translations from all locales. Useful for a CI/CD pipeline or husky hook.",
|
|
1502
|
-
action: removeUnusedKeys
|
|
1503
|
-
}];
|
|
1504
|
-
var _loop = function _loop() {
|
|
1505
|
-
var command = _commands[_i];
|
|
1506
|
-
var cmd = program.command(command.name).description(command.description);
|
|
1507
|
-
// Add key option to replace command
|
|
1508
|
-
if (command.name === "replace") {
|
|
1509
|
-
cmd.option("-k, --key <key>", "translation key to replace").allowExcessArguments(true).argument("[key]", "translation key to replace");
|
|
1510
|
-
}
|
|
1511
|
-
cmd.action( /*#__PURE__*/function () {
|
|
1512
|
-
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(arg, options) {
|
|
1513
|
-
var _config$model;
|
|
1514
|
-
var res, config, isGemini, openaiKey, geminiKey, key, keyType, openai, keyToUse;
|
|
1515
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
1516
|
-
while (1) switch (_context.prev = _context.next) {
|
|
1517
|
-
case 0:
|
|
1518
|
-
res = dotenv.config({
|
|
1519
|
-
path: program.opts().env || ".env"
|
|
1520
|
-
});
|
|
1521
|
-
_context.next = 3;
|
|
1522
|
-
return loadConfig({
|
|
1523
|
-
configPath: program.opts().config
|
|
1524
|
-
});
|
|
1525
|
-
case 3:
|
|
1526
|
-
config = _context.sent;
|
|
1527
|
-
isGemini = (_config$model = config.model) == null ? void 0 : _config$model.includes("gemini"); // Get API key from environment or config
|
|
1528
|
-
openaiKey = res.parsed.OPENAI_API_KEY || config.OPENAI_API_KEY;
|
|
1529
|
-
geminiKey = res.parsed.GEMINI_API_KEY || config.GEMINI_API_KEY; // Select appropriate key based on model type
|
|
1530
|
-
key = isGemini ? geminiKey : openaiKey;
|
|
1531
|
-
if (!key) {
|
|
1532
|
-
keyType = isGemini ? "GEMINI_API_KEY" : "OPENAI_API_KEY";
|
|
1533
|
-
console.error("Please provide a" + (isGemini ? " Gemini" : "n OpenAI") + " API key in your .env file or config, called " + keyType + ".");
|
|
1534
|
-
process.exit(1);
|
|
1535
|
-
}
|
|
1536
|
-
openai = new OpenAI(_extends({
|
|
1537
|
-
apiKey: key
|
|
1538
|
-
}, isGemini && {
|
|
1539
|
-
baseURL: "https://generativelanguage.googleapis.com/v1beta/openai/"
|
|
1540
|
-
})); // For replace command, check for key in argument or option
|
|
1541
|
-
if (command.name === "replace") {
|
|
1542
|
-
// If key is provided as positional argument, use that first
|
|
1543
|
-
keyToUse = typeof arg === "string" ? arg : options.key;
|
|
1544
|
-
command.action(_extends({}, config, {
|
|
1545
|
-
openai: openai
|
|
1546
|
-
}), keyToUse);
|
|
1547
|
-
} else {
|
|
1548
|
-
command.action(_extends({}, config, {
|
|
1549
|
-
openai: openai
|
|
1550
|
-
}));
|
|
1551
|
-
}
|
|
1552
|
-
case 11:
|
|
1553
|
-
case "end":
|
|
1554
|
-
return _context.stop();
|
|
1555
|
-
}
|
|
1556
|
-
}, _callee);
|
|
1557
|
-
}));
|
|
1558
|
-
return function (_x, _x2) {
|
|
1559
|
-
return _ref.apply(this, arguments);
|
|
1560
|
-
};
|
|
1561
|
-
}());
|
|
1562
|
-
};
|
|
1563
|
-
for (var _i = 0, _commands = commands; _i < _commands.length; _i++) {
|
|
1564
|
-
_loop();
|
|
1565
|
-
}
|
|
1566
|
-
program.parse(process.argv);
|
|
1775
|
+
export { checkMissing, createPrunedNamespace, createPrunedNamespaceAutomated, loadConfig, removeUnusedKeys, replaceTranslation, syncLocales, translateMissing };
|
|
1567
1776
|
//# sourceMappingURL=i18n-magic.esm.js.map
|