poly-lexis 0.4.0 → 0.4.2
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 +109 -103
- package/dist/cli/translations.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +108 -100
- package/dist/index.js.map +1 -1
- package/dist/scripts/verify-translations.js +1 -1
- package/dist/scripts/verify-translations.js.map +1 -1
- package/package.json +2 -2
package/dist/cli/translations.js
CHANGED
|
@@ -649,7 +649,7 @@ var init_types = __esm({
|
|
|
649
649
|
languages: ["en"],
|
|
650
650
|
sourceLanguage: "en",
|
|
651
651
|
typesOutputPath: "src/types/i18nTypes.ts",
|
|
652
|
-
provider: "
|
|
652
|
+
provider: "deepl",
|
|
653
653
|
useFallbackLanguages: true
|
|
654
654
|
};
|
|
655
655
|
DEFAULT_LANGUAGES = ["en", "fr", "it", "pl", "es", "pt", "de", "nl", "sv", "hu", "cs", "ja"];
|
|
@@ -868,13 +868,104 @@ import { confirm as confirm2, input as input2, select as select2 } from "@inquir
|
|
|
868
868
|
init_esm_shims();
|
|
869
869
|
import * as path4 from "path";
|
|
870
870
|
|
|
871
|
-
// src/translations/utils/
|
|
871
|
+
// src/translations/utils/deepl-translate-provider.ts
|
|
872
872
|
init_esm_shims();
|
|
873
|
+
init_language_fallback();
|
|
874
|
+
function preserveVariables(text) {
|
|
875
|
+
const variableMap = /* @__PURE__ */ new Map();
|
|
876
|
+
let placeholderIndex = 0;
|
|
877
|
+
const textWithPlaceholders = text.replace(/\{\{([^}]+)\}\}/g, (match) => {
|
|
878
|
+
const placeholder = `XXX_${placeholderIndex}_XXX`;
|
|
879
|
+
variableMap.set(placeholder, match);
|
|
880
|
+
placeholderIndex++;
|
|
881
|
+
return placeholder;
|
|
882
|
+
});
|
|
883
|
+
return { textWithPlaceholders, variableMap };
|
|
884
|
+
}
|
|
885
|
+
function restoreVariables(text, variableMap) {
|
|
886
|
+
let result = text;
|
|
887
|
+
for (const [placeholder, original] of variableMap) {
|
|
888
|
+
result = result.replace(new RegExp(placeholder, "g"), original);
|
|
889
|
+
}
|
|
890
|
+
return result;
|
|
891
|
+
}
|
|
892
|
+
function normalizeLanguageCode(langCode) {
|
|
893
|
+
return langCode.replace("_", "-").toUpperCase();
|
|
894
|
+
}
|
|
895
|
+
var DeepLTranslateProvider = class {
|
|
896
|
+
isFreeApi;
|
|
897
|
+
constructor(isFreeApi = false) {
|
|
898
|
+
this.isFreeApi = isFreeApi;
|
|
899
|
+
}
|
|
900
|
+
getApiEndpoint() {
|
|
901
|
+
return this.isFreeApi ? "https://api-free.deepl.com/v2/translate" : "https://api.deepl.com/v2/translate";
|
|
902
|
+
}
|
|
903
|
+
async translate(options) {
|
|
904
|
+
const { text, sourceLang, targetLang, apiKey, useFallbackLanguages = true } = options;
|
|
905
|
+
if (!apiKey) {
|
|
906
|
+
throw new Error(
|
|
907
|
+
"DeepL API key is required. Set DEEPL_API_KEY environment variable or provide apiKey in options."
|
|
908
|
+
);
|
|
909
|
+
}
|
|
910
|
+
const targetLangResult = resolveLanguageWithFallback(targetLang, "deepl", useFallbackLanguages);
|
|
911
|
+
logLanguageFallback(targetLangResult, "deepl");
|
|
912
|
+
let resolvedSourceLang;
|
|
913
|
+
if (sourceLang) {
|
|
914
|
+
const sourceLangResult = resolveLanguageWithFallback(sourceLang, "deepl", useFallbackLanguages);
|
|
915
|
+
logLanguageFallback(sourceLangResult, "deepl");
|
|
916
|
+
resolvedSourceLang = sourceLangResult.resolvedLanguage;
|
|
917
|
+
}
|
|
918
|
+
const { textWithPlaceholders, variableMap } = preserveVariables(text);
|
|
919
|
+
const body = {
|
|
920
|
+
text: [textWithPlaceholders],
|
|
921
|
+
target_lang: normalizeLanguageCode(targetLangResult.resolvedLanguage),
|
|
922
|
+
...resolvedSourceLang && { source_lang: normalizeLanguageCode(resolvedSourceLang) }
|
|
923
|
+
};
|
|
924
|
+
const response = await fetch(this.getApiEndpoint(), {
|
|
925
|
+
method: "POST",
|
|
926
|
+
headers: {
|
|
927
|
+
Authorization: `DeepL-Auth-Key ${apiKey}`,
|
|
928
|
+
"Content-Type": "application/json"
|
|
929
|
+
},
|
|
930
|
+
body: JSON.stringify(body)
|
|
931
|
+
});
|
|
932
|
+
if (!response.ok) {
|
|
933
|
+
const errorData = await response.json().catch(() => ({}));
|
|
934
|
+
throw new Error(`DeepL API error: ${errorData.message || response.statusText} (${response.status})`);
|
|
935
|
+
}
|
|
936
|
+
const data = await response.json();
|
|
937
|
+
if (!data.translations || data.translations.length === 0) {
|
|
938
|
+
throw new Error("DeepL API returned no translations");
|
|
939
|
+
}
|
|
940
|
+
const translatedText = data.translations[0].text;
|
|
941
|
+
return restoreVariables(translatedText, variableMap);
|
|
942
|
+
}
|
|
943
|
+
async translateBatch(texts, sourceLang, targetLang, apiKey, delayMs = 100) {
|
|
944
|
+
const results = [];
|
|
945
|
+
for (const text of texts) {
|
|
946
|
+
const translated = await this.translate({
|
|
947
|
+
text,
|
|
948
|
+
sourceLang,
|
|
949
|
+
targetLang,
|
|
950
|
+
apiKey
|
|
951
|
+
});
|
|
952
|
+
results.push(translated);
|
|
953
|
+
if (delayMs > 0) {
|
|
954
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
return results;
|
|
958
|
+
}
|
|
959
|
+
async validateConfig() {
|
|
960
|
+
const apiKey = process.env.DEEPL_API_KEY;
|
|
961
|
+
return !!apiKey;
|
|
962
|
+
}
|
|
963
|
+
};
|
|
873
964
|
|
|
874
965
|
// src/translations/utils/google-translate-provider.ts
|
|
875
966
|
init_esm_shims();
|
|
876
967
|
init_language_fallback();
|
|
877
|
-
function
|
|
968
|
+
function preserveVariables2(text) {
|
|
878
969
|
const variableMap = /* @__PURE__ */ new Map();
|
|
879
970
|
let placeholderIndex = 0;
|
|
880
971
|
const textWithPlaceholders = text.replace(/\{\{([^}]+)\}\}/g, (match) => {
|
|
@@ -885,7 +976,7 @@ function preserveVariables(text) {
|
|
|
885
976
|
});
|
|
886
977
|
return { textWithPlaceholders, variableMap };
|
|
887
978
|
}
|
|
888
|
-
function
|
|
979
|
+
function restoreVariables2(text, variableMap) {
|
|
889
980
|
let result = text;
|
|
890
981
|
for (const [placeholder, original] of variableMap) {
|
|
891
982
|
result = result.replace(new RegExp(placeholder, "g"), original);
|
|
@@ -908,7 +999,7 @@ var GoogleTranslateProvider = class {
|
|
|
908
999
|
logLanguageFallback(sourceLangResult, "google");
|
|
909
1000
|
resolvedSourceLang = sourceLangResult.resolvedLanguage;
|
|
910
1001
|
}
|
|
911
|
-
const { textWithPlaceholders, variableMap } =
|
|
1002
|
+
const { textWithPlaceholders, variableMap } = preserveVariables2(text);
|
|
912
1003
|
const url = `https://translation.googleapis.com/language/translate/v2?key=${apiKey}`;
|
|
913
1004
|
const sourceForGoogle = resolvedSourceLang?.includes("_") ? resolvedSourceLang.split("_")[0] : resolvedSourceLang;
|
|
914
1005
|
const targetForGoogle = targetLangResult.resolvedLanguage.includes("_") ? targetLangResult.resolvedLanguage.split("_")[0] : targetLangResult.resolvedLanguage;
|
|
@@ -929,7 +1020,7 @@ var GoogleTranslateProvider = class {
|
|
|
929
1020
|
throw new Error(`Google Translate API error: ${data.error.message}`);
|
|
930
1021
|
}
|
|
931
1022
|
const translatedText = data.data.translations[0].translatedText;
|
|
932
|
-
return
|
|
1023
|
+
return restoreVariables2(translatedText, variableMap);
|
|
933
1024
|
}
|
|
934
1025
|
async translateBatch(texts, sourceLang, targetLang, apiKey, delayMs = 100) {
|
|
935
1026
|
const results = [];
|
|
@@ -954,6 +1045,7 @@ var GoogleTranslateProvider = class {
|
|
|
954
1045
|
};
|
|
955
1046
|
|
|
956
1047
|
// src/translations/utils/translator.ts
|
|
1048
|
+
init_esm_shims();
|
|
957
1049
|
var defaultProvider = new GoogleTranslateProvider();
|
|
958
1050
|
var customProvider = null;
|
|
959
1051
|
function setTranslationProvider(provider) {
|
|
@@ -980,6 +1072,16 @@ async function addTranslationKey(projectRoot, options) {
|
|
|
980
1072
|
const config = loadConfig(projectRoot);
|
|
981
1073
|
const translationsPath = path4.join(projectRoot, config.translationsPath);
|
|
982
1074
|
const { namespace, key, value, autoTranslate = false, apiKey } = options;
|
|
1075
|
+
const currentProvider = getTranslationProvider();
|
|
1076
|
+
const isDefaultGoogleProvider = currentProvider.constructor.name === "GoogleTranslateProvider";
|
|
1077
|
+
if (isDefaultGoogleProvider) {
|
|
1078
|
+
const provider = config.provider || "deepl";
|
|
1079
|
+
if (provider === "deepl") {
|
|
1080
|
+
setTranslationProvider(new DeepLTranslateProvider());
|
|
1081
|
+
} else {
|
|
1082
|
+
setTranslationProvider(new GoogleTranslateProvider());
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
983
1085
|
console.log("=====");
|
|
984
1086
|
console.log("Adding translation key");
|
|
985
1087
|
console.log("=====");
|
|
@@ -1009,7 +1111,7 @@ async function addTranslationKey(projectRoot, options) {
|
|
|
1009
1111
|
targetTranslations[namespace] = {};
|
|
1010
1112
|
}
|
|
1011
1113
|
if (!targetTranslations[namespace][key] || targetTranslations[namespace][key].trim() === "") {
|
|
1012
|
-
const translated = await translateText(value, lang, sourceLang, apiKey);
|
|
1114
|
+
const translated = await translateText(value, lang, sourceLang, apiKey, config.useFallbackLanguages ?? true);
|
|
1013
1115
|
targetTranslations[namespace][key] = translated;
|
|
1014
1116
|
const sorted = sortKeys(targetTranslations[namespace]);
|
|
1015
1117
|
writeTranslation(translationsPath, lang, namespace, sorted);
|
|
@@ -1236,102 +1338,6 @@ import * as path9 from "path";
|
|
|
1236
1338
|
// src/translations/cli/auto-fill.ts
|
|
1237
1339
|
init_esm_shims();
|
|
1238
1340
|
import * as path7 from "path";
|
|
1239
|
-
|
|
1240
|
-
// src/translations/utils/deepl-translate-provider.ts
|
|
1241
|
-
init_esm_shims();
|
|
1242
|
-
init_language_fallback();
|
|
1243
|
-
function preserveVariables2(text) {
|
|
1244
|
-
const variableMap = /* @__PURE__ */ new Map();
|
|
1245
|
-
let placeholderIndex = 0;
|
|
1246
|
-
const textWithPlaceholders = text.replace(/\{\{([^}]+)\}\}/g, (match) => {
|
|
1247
|
-
const placeholder = `XXX_${placeholderIndex}_XXX`;
|
|
1248
|
-
variableMap.set(placeholder, match);
|
|
1249
|
-
placeholderIndex++;
|
|
1250
|
-
return placeholder;
|
|
1251
|
-
});
|
|
1252
|
-
return { textWithPlaceholders, variableMap };
|
|
1253
|
-
}
|
|
1254
|
-
function restoreVariables2(text, variableMap) {
|
|
1255
|
-
let result = text;
|
|
1256
|
-
for (const [placeholder, original] of variableMap) {
|
|
1257
|
-
result = result.replace(new RegExp(placeholder, "g"), original);
|
|
1258
|
-
}
|
|
1259
|
-
return result;
|
|
1260
|
-
}
|
|
1261
|
-
function normalizeLanguageCode(langCode) {
|
|
1262
|
-
return langCode.replace("_", "-").toUpperCase();
|
|
1263
|
-
}
|
|
1264
|
-
var DeepLTranslateProvider = class {
|
|
1265
|
-
isFreeApi;
|
|
1266
|
-
constructor(isFreeApi = false) {
|
|
1267
|
-
this.isFreeApi = isFreeApi;
|
|
1268
|
-
}
|
|
1269
|
-
getApiEndpoint() {
|
|
1270
|
-
return this.isFreeApi ? "https://api-free.deepl.com/v2/translate" : "https://api.deepl.com/v2/translate";
|
|
1271
|
-
}
|
|
1272
|
-
async translate(options) {
|
|
1273
|
-
const { text, sourceLang, targetLang, apiKey, useFallbackLanguages = true } = options;
|
|
1274
|
-
if (!apiKey) {
|
|
1275
|
-
throw new Error(
|
|
1276
|
-
"DeepL API key is required. Set DEEPL_API_KEY environment variable or provide apiKey in options."
|
|
1277
|
-
);
|
|
1278
|
-
}
|
|
1279
|
-
const targetLangResult = resolveLanguageWithFallback(targetLang, "deepl", useFallbackLanguages);
|
|
1280
|
-
logLanguageFallback(targetLangResult, "deepl");
|
|
1281
|
-
let resolvedSourceLang;
|
|
1282
|
-
if (sourceLang) {
|
|
1283
|
-
const sourceLangResult = resolveLanguageWithFallback(sourceLang, "deepl", useFallbackLanguages);
|
|
1284
|
-
logLanguageFallback(sourceLangResult, "deepl");
|
|
1285
|
-
resolvedSourceLang = sourceLangResult.resolvedLanguage;
|
|
1286
|
-
}
|
|
1287
|
-
const { textWithPlaceholders, variableMap } = preserveVariables2(text);
|
|
1288
|
-
const body = {
|
|
1289
|
-
text: [textWithPlaceholders],
|
|
1290
|
-
target_lang: normalizeLanguageCode(targetLangResult.resolvedLanguage),
|
|
1291
|
-
...resolvedSourceLang && { source_lang: normalizeLanguageCode(resolvedSourceLang) }
|
|
1292
|
-
};
|
|
1293
|
-
const response = await fetch(this.getApiEndpoint(), {
|
|
1294
|
-
method: "POST",
|
|
1295
|
-
headers: {
|
|
1296
|
-
Authorization: `DeepL-Auth-Key ${apiKey}`,
|
|
1297
|
-
"Content-Type": "application/json"
|
|
1298
|
-
},
|
|
1299
|
-
body: JSON.stringify(body)
|
|
1300
|
-
});
|
|
1301
|
-
if (!response.ok) {
|
|
1302
|
-
const errorData = await response.json().catch(() => ({}));
|
|
1303
|
-
throw new Error(`DeepL API error: ${errorData.message || response.statusText} (${response.status})`);
|
|
1304
|
-
}
|
|
1305
|
-
const data = await response.json();
|
|
1306
|
-
if (!data.translations || data.translations.length === 0) {
|
|
1307
|
-
throw new Error("DeepL API returned no translations");
|
|
1308
|
-
}
|
|
1309
|
-
const translatedText = data.translations[0].text;
|
|
1310
|
-
return restoreVariables2(translatedText, variableMap);
|
|
1311
|
-
}
|
|
1312
|
-
async translateBatch(texts, sourceLang, targetLang, apiKey, delayMs = 100) {
|
|
1313
|
-
const results = [];
|
|
1314
|
-
for (const text of texts) {
|
|
1315
|
-
const translated = await this.translate({
|
|
1316
|
-
text,
|
|
1317
|
-
sourceLang,
|
|
1318
|
-
targetLang,
|
|
1319
|
-
apiKey
|
|
1320
|
-
});
|
|
1321
|
-
results.push(translated);
|
|
1322
|
-
if (delayMs > 0) {
|
|
1323
|
-
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
1324
|
-
}
|
|
1325
|
-
}
|
|
1326
|
-
return results;
|
|
1327
|
-
}
|
|
1328
|
-
async validateConfig() {
|
|
1329
|
-
const apiKey = process.env.DEEPL_API_KEY;
|
|
1330
|
-
return !!apiKey;
|
|
1331
|
-
}
|
|
1332
|
-
};
|
|
1333
|
-
|
|
1334
|
-
// src/translations/cli/auto-fill.ts
|
|
1335
1341
|
init_utils();
|
|
1336
1342
|
init_init();
|
|
1337
1343
|
|