poly-lexis 0.4.2 → 0.4.3
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/dist/cli/translations.js +27 -20
- package/dist/cli/translations.js.map +1 -1
- package/dist/index.js +71 -63
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -881,7 +881,7 @@ var init_init = __esm({
|
|
|
881
881
|
});
|
|
882
882
|
|
|
883
883
|
// src/translations/cli/add-key.ts
|
|
884
|
-
import * as
|
|
884
|
+
import * as path4 from "path";
|
|
885
885
|
|
|
886
886
|
// src/translations/utils/deepl-translate-provider.ts
|
|
887
887
|
init_language_fallback();
|
|
@@ -1086,10 +1086,68 @@ async function translateBatch(texts, targetLang, sourceLang = "en", apiKey, dela
|
|
|
1086
1086
|
|
|
1087
1087
|
// src/translations/cli/add-key.ts
|
|
1088
1088
|
init_utils();
|
|
1089
|
+
|
|
1090
|
+
// src/translations/cli/generate-types.ts
|
|
1091
|
+
init_utils();
|
|
1089
1092
|
init_init();
|
|
1090
|
-
|
|
1093
|
+
import { execSync } from "child_process";
|
|
1094
|
+
import * as fs3 from "fs";
|
|
1095
|
+
import * as path3 from "path";
|
|
1096
|
+
var typeTemplate = (translationKeys, namespaceKeys) => `
|
|
1097
|
+
export const translationKeys = [${translationKeys.map((key) => `"${key}"`).join(", ")}] as const;
|
|
1098
|
+
export const namespaceKeys = [${namespaceKeys.map((key) => `"${key}"`).join(", ")}] as const;
|
|
1099
|
+
|
|
1100
|
+
export type TranslationKey = typeof translationKeys[number];
|
|
1101
|
+
export type TranslationNamespace = typeof namespaceKeys[number];
|
|
1102
|
+
`;
|
|
1103
|
+
function generateTranslationTypes(projectRoot = process.cwd()) {
|
|
1104
|
+
console.log("=====");
|
|
1105
|
+
console.time("i18n types generated");
|
|
1106
|
+
console.log("Generating i18n types");
|
|
1107
|
+
console.log("=====");
|
|
1091
1108
|
const config = loadConfig(projectRoot);
|
|
1092
1109
|
const translationsPath = path3.join(projectRoot, config.translationsPath);
|
|
1110
|
+
const sourceLanguage = config.sourceLanguage;
|
|
1111
|
+
const outputFilePath = path3.join(projectRoot, config.typesOutputPath);
|
|
1112
|
+
const dirPath = path3.join(translationsPath, sourceLanguage);
|
|
1113
|
+
if (!fs3.existsSync(dirPath)) {
|
|
1114
|
+
throw new Error(`Source language directory not found: ${dirPath}`);
|
|
1115
|
+
}
|
|
1116
|
+
const namespaces = getNamespaces(translationsPath, sourceLanguage);
|
|
1117
|
+
if (!namespaces.length) {
|
|
1118
|
+
throw new Error(`No translation files found in ${dirPath}`);
|
|
1119
|
+
}
|
|
1120
|
+
const translations = readTranslations(translationsPath, sourceLanguage);
|
|
1121
|
+
let allKeys = [];
|
|
1122
|
+
for (const namespace of namespaces) {
|
|
1123
|
+
const keys = Object.keys(translations[namespace] || {});
|
|
1124
|
+
allKeys = allKeys.concat(keys);
|
|
1125
|
+
}
|
|
1126
|
+
const outputDir = path3.dirname(outputFilePath);
|
|
1127
|
+
if (!fs3.existsSync(outputDir)) {
|
|
1128
|
+
fs3.mkdirSync(outputDir, { recursive: true });
|
|
1129
|
+
}
|
|
1130
|
+
const typeString = typeTemplate(allKeys, namespaces);
|
|
1131
|
+
fs3.writeFileSync(outputFilePath, typeString, "utf8");
|
|
1132
|
+
console.log(`Generated types with ${allKeys.length} keys and ${namespaces.length} namespaces`);
|
|
1133
|
+
console.log(`Output: ${outputFilePath}`);
|
|
1134
|
+
try {
|
|
1135
|
+
execSync(`pnpm biome format --write ${outputFilePath}`, {
|
|
1136
|
+
stdio: "inherit",
|
|
1137
|
+
cwd: projectRoot
|
|
1138
|
+
});
|
|
1139
|
+
} catch {
|
|
1140
|
+
console.warn("Failed to format with Biome, continuing without formatting...");
|
|
1141
|
+
}
|
|
1142
|
+
console.timeEnd("i18n types generated");
|
|
1143
|
+
console.log("=====");
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
// src/translations/cli/add-key.ts
|
|
1147
|
+
init_init();
|
|
1148
|
+
async function addTranslationKey(projectRoot, options) {
|
|
1149
|
+
const config = loadConfig(projectRoot);
|
|
1150
|
+
const translationsPath = path4.join(projectRoot, config.translationsPath);
|
|
1093
1151
|
const { namespace, key, value, autoTranslate = false, apiKey } = options;
|
|
1094
1152
|
const currentProvider = getTranslationProvider();
|
|
1095
1153
|
const isDefaultGoogleProvider = currentProvider.constructor.name === "GoogleTranslateProvider";
|
|
@@ -1166,6 +1224,12 @@ async function addTranslationKey(projectRoot, options) {
|
|
|
1166
1224
|
console.log("=====");
|
|
1167
1225
|
console.log("Translation key added successfully!");
|
|
1168
1226
|
console.log("=====");
|
|
1227
|
+
console.log("\nRegenerating TypeScript types...");
|
|
1228
|
+
try {
|
|
1229
|
+
generateTranslationTypes(projectRoot);
|
|
1230
|
+
} catch (error) {
|
|
1231
|
+
console.error("Failed to generate types:", error instanceof Error ? error.message : "Unknown error");
|
|
1232
|
+
}
|
|
1169
1233
|
}
|
|
1170
1234
|
async function addTranslationKeys(projectRoot, entries, autoTranslate = false, apiKey) {
|
|
1171
1235
|
console.log(`Adding ${entries.length} translation keys...`);
|
|
@@ -1181,17 +1245,17 @@ async function addTranslationKeys(projectRoot, entries, autoTranslate = false, a
|
|
|
1181
1245
|
}
|
|
1182
1246
|
|
|
1183
1247
|
// src/translations/cli/auto-fill.ts
|
|
1184
|
-
import * as
|
|
1248
|
+
import * as path6 from "path";
|
|
1185
1249
|
init_utils();
|
|
1186
1250
|
init_init();
|
|
1187
1251
|
|
|
1188
1252
|
// src/translations/cli/validate.ts
|
|
1189
1253
|
init_utils();
|
|
1190
1254
|
init_init();
|
|
1191
|
-
import * as
|
|
1255
|
+
import * as path5 from "path";
|
|
1192
1256
|
function validateTranslations(projectRoot = process.cwd()) {
|
|
1193
1257
|
const config = loadConfig(projectRoot);
|
|
1194
|
-
const translationsPath =
|
|
1258
|
+
const translationsPath = path5.join(projectRoot, config.translationsPath);
|
|
1195
1259
|
const sourceLanguage = config.sourceLanguage;
|
|
1196
1260
|
const missing = [];
|
|
1197
1261
|
const empty = [];
|
|
@@ -1274,7 +1338,7 @@ function getMissingForLanguage(projectRoot, language) {
|
|
|
1274
1338
|
// src/translations/cli/auto-fill.ts
|
|
1275
1339
|
async function autoFillTranslations(projectRoot = process.cwd(), options = {}) {
|
|
1276
1340
|
const config = loadConfig(projectRoot);
|
|
1277
|
-
const translationsPath =
|
|
1341
|
+
const translationsPath = path6.join(projectRoot, config.translationsPath);
|
|
1278
1342
|
const { apiKey, limit = 1e3, delayMs = 100, dryRun = false } = options;
|
|
1279
1343
|
const currentProvider = getTranslationProvider();
|
|
1280
1344
|
const isDefaultGoogleProvider = currentProvider.constructor.name === "GoogleTranslateProvider";
|
|
@@ -1367,7 +1431,7 @@ Processing language: ${language}`);
|
|
|
1367
1431
|
}
|
|
1368
1432
|
async function fillNamespace(projectRoot, language, namespace, apiKey) {
|
|
1369
1433
|
const config = loadConfig(projectRoot);
|
|
1370
|
-
const translationsPath =
|
|
1434
|
+
const translationsPath = path6.join(projectRoot, config.translationsPath);
|
|
1371
1435
|
const currentProvider = getTranslationProvider();
|
|
1372
1436
|
const isDefaultGoogleProvider = currentProvider.constructor.name === "GoogleTranslateProvider";
|
|
1373
1437
|
if (isDefaultGoogleProvider) {
|
|
@@ -1410,62 +1474,6 @@ async function fillNamespace(projectRoot, language, namespace, apiKey) {
|
|
|
1410
1474
|
}
|
|
1411
1475
|
}
|
|
1412
1476
|
|
|
1413
|
-
// src/translations/cli/generate-types.ts
|
|
1414
|
-
init_utils();
|
|
1415
|
-
init_init();
|
|
1416
|
-
import { execSync } from "child_process";
|
|
1417
|
-
import * as fs3 from "fs";
|
|
1418
|
-
import * as path6 from "path";
|
|
1419
|
-
var typeTemplate = (translationKeys, namespaceKeys) => `
|
|
1420
|
-
export const translationKeys = [${translationKeys.map((key) => `"${key}"`).join(", ")}] as const;
|
|
1421
|
-
export const namespaceKeys = [${namespaceKeys.map((key) => `"${key}"`).join(", ")}] as const;
|
|
1422
|
-
|
|
1423
|
-
export type TranslationKey = typeof translationKeys[number];
|
|
1424
|
-
export type TranslationNamespace = typeof namespaceKeys[number];
|
|
1425
|
-
`;
|
|
1426
|
-
function generateTranslationTypes(projectRoot = process.cwd()) {
|
|
1427
|
-
console.log("=====");
|
|
1428
|
-
console.time("i18n types generated");
|
|
1429
|
-
console.log("Generating i18n types");
|
|
1430
|
-
console.log("=====");
|
|
1431
|
-
const config = loadConfig(projectRoot);
|
|
1432
|
-
const translationsPath = path6.join(projectRoot, config.translationsPath);
|
|
1433
|
-
const sourceLanguage = config.sourceLanguage;
|
|
1434
|
-
const outputFilePath = path6.join(projectRoot, config.typesOutputPath);
|
|
1435
|
-
const dirPath = path6.join(translationsPath, sourceLanguage);
|
|
1436
|
-
if (!fs3.existsSync(dirPath)) {
|
|
1437
|
-
throw new Error(`Source language directory not found: ${dirPath}`);
|
|
1438
|
-
}
|
|
1439
|
-
const namespaces = getNamespaces(translationsPath, sourceLanguage);
|
|
1440
|
-
if (!namespaces.length) {
|
|
1441
|
-
throw new Error(`No translation files found in ${dirPath}`);
|
|
1442
|
-
}
|
|
1443
|
-
const translations = readTranslations(translationsPath, sourceLanguage);
|
|
1444
|
-
let allKeys = [];
|
|
1445
|
-
for (const namespace of namespaces) {
|
|
1446
|
-
const keys = Object.keys(translations[namespace] || {});
|
|
1447
|
-
allKeys = allKeys.concat(keys);
|
|
1448
|
-
}
|
|
1449
|
-
const outputDir = path6.dirname(outputFilePath);
|
|
1450
|
-
if (!fs3.existsSync(outputDir)) {
|
|
1451
|
-
fs3.mkdirSync(outputDir, { recursive: true });
|
|
1452
|
-
}
|
|
1453
|
-
const typeString = typeTemplate(allKeys, namespaces);
|
|
1454
|
-
fs3.writeFileSync(outputFilePath, typeString, "utf8");
|
|
1455
|
-
console.log(`Generated types with ${allKeys.length} keys and ${namespaces.length} namespaces`);
|
|
1456
|
-
console.log(`Output: ${outputFilePath}`);
|
|
1457
|
-
try {
|
|
1458
|
-
execSync(`pnpm biome format --write ${outputFilePath}`, {
|
|
1459
|
-
stdio: "inherit",
|
|
1460
|
-
cwd: projectRoot
|
|
1461
|
-
});
|
|
1462
|
-
} catch {
|
|
1463
|
-
console.warn("Failed to format with Biome, continuing without formatting...");
|
|
1464
|
-
}
|
|
1465
|
-
console.timeEnd("i18n types generated");
|
|
1466
|
-
console.log("=====");
|
|
1467
|
-
}
|
|
1468
|
-
|
|
1469
1477
|
// src/translations/index.ts
|
|
1470
1478
|
init_init();
|
|
1471
1479
|
|