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/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -741,7 +741,7 @@ var init_types = __esm({
|
|
|
741
741
|
languages: ["en"],
|
|
742
742
|
sourceLanguage: "en",
|
|
743
743
|
typesOutputPath: "src/types/i18nTypes.ts",
|
|
744
|
-
provider: "
|
|
744
|
+
provider: "deepl",
|
|
745
745
|
useFallbackLanguages: true
|
|
746
746
|
};
|
|
747
747
|
DEFAULT_LANGUAGES = ["en", "fr", "it", "pl", "es", "pt", "de", "nl", "sv", "hu", "cs", "ja"];
|
|
@@ -883,7 +883,7 @@ var init_init = __esm({
|
|
|
883
883
|
// src/translations/cli/add-key.ts
|
|
884
884
|
import * as path3 from "path";
|
|
885
885
|
|
|
886
|
-
// src/translations/utils/
|
|
886
|
+
// src/translations/utils/deepl-translate-provider.ts
|
|
887
887
|
init_language_fallback();
|
|
888
888
|
function preserveVariables(text) {
|
|
889
889
|
const variableMap = /* @__PURE__ */ new Map();
|
|
@@ -903,6 +903,99 @@ function restoreVariables(text, variableMap) {
|
|
|
903
903
|
}
|
|
904
904
|
return result;
|
|
905
905
|
}
|
|
906
|
+
function normalizeLanguageCode(langCode) {
|
|
907
|
+
return langCode.replace("_", "-").toUpperCase();
|
|
908
|
+
}
|
|
909
|
+
var DeepLTranslateProvider = class {
|
|
910
|
+
isFreeApi;
|
|
911
|
+
constructor(isFreeApi = false) {
|
|
912
|
+
this.isFreeApi = isFreeApi;
|
|
913
|
+
}
|
|
914
|
+
getApiEndpoint() {
|
|
915
|
+
return this.isFreeApi ? "https://api-free.deepl.com/v2/translate" : "https://api.deepl.com/v2/translate";
|
|
916
|
+
}
|
|
917
|
+
async translate(options) {
|
|
918
|
+
const { text, sourceLang, targetLang, apiKey, useFallbackLanguages = true } = options;
|
|
919
|
+
if (!apiKey) {
|
|
920
|
+
throw new Error(
|
|
921
|
+
"DeepL API key is required. Set DEEPL_API_KEY environment variable or provide apiKey in options."
|
|
922
|
+
);
|
|
923
|
+
}
|
|
924
|
+
const targetLangResult = resolveLanguageWithFallback(targetLang, "deepl", useFallbackLanguages);
|
|
925
|
+
logLanguageFallback(targetLangResult, "deepl");
|
|
926
|
+
let resolvedSourceLang;
|
|
927
|
+
if (sourceLang) {
|
|
928
|
+
const sourceLangResult = resolveLanguageWithFallback(sourceLang, "deepl", useFallbackLanguages);
|
|
929
|
+
logLanguageFallback(sourceLangResult, "deepl");
|
|
930
|
+
resolvedSourceLang = sourceLangResult.resolvedLanguage;
|
|
931
|
+
}
|
|
932
|
+
const { textWithPlaceholders, variableMap } = preserveVariables(text);
|
|
933
|
+
const body = {
|
|
934
|
+
text: [textWithPlaceholders],
|
|
935
|
+
target_lang: normalizeLanguageCode(targetLangResult.resolvedLanguage),
|
|
936
|
+
...resolvedSourceLang && { source_lang: normalizeLanguageCode(resolvedSourceLang) }
|
|
937
|
+
};
|
|
938
|
+
const response = await fetch(this.getApiEndpoint(), {
|
|
939
|
+
method: "POST",
|
|
940
|
+
headers: {
|
|
941
|
+
Authorization: `DeepL-Auth-Key ${apiKey}`,
|
|
942
|
+
"Content-Type": "application/json"
|
|
943
|
+
},
|
|
944
|
+
body: JSON.stringify(body)
|
|
945
|
+
});
|
|
946
|
+
if (!response.ok) {
|
|
947
|
+
const errorData = await response.json().catch(() => ({}));
|
|
948
|
+
throw new Error(`DeepL API error: ${errorData.message || response.statusText} (${response.status})`);
|
|
949
|
+
}
|
|
950
|
+
const data = await response.json();
|
|
951
|
+
if (!data.translations || data.translations.length === 0) {
|
|
952
|
+
throw new Error("DeepL API returned no translations");
|
|
953
|
+
}
|
|
954
|
+
const translatedText = data.translations[0].text;
|
|
955
|
+
return restoreVariables(translatedText, variableMap);
|
|
956
|
+
}
|
|
957
|
+
async translateBatch(texts, sourceLang, targetLang, apiKey, delayMs = 100) {
|
|
958
|
+
const results = [];
|
|
959
|
+
for (const text of texts) {
|
|
960
|
+
const translated = await this.translate({
|
|
961
|
+
text,
|
|
962
|
+
sourceLang,
|
|
963
|
+
targetLang,
|
|
964
|
+
apiKey
|
|
965
|
+
});
|
|
966
|
+
results.push(translated);
|
|
967
|
+
if (delayMs > 0) {
|
|
968
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
return results;
|
|
972
|
+
}
|
|
973
|
+
async validateConfig() {
|
|
974
|
+
const apiKey = process.env.DEEPL_API_KEY;
|
|
975
|
+
return !!apiKey;
|
|
976
|
+
}
|
|
977
|
+
};
|
|
978
|
+
|
|
979
|
+
// src/translations/utils/google-translate-provider.ts
|
|
980
|
+
init_language_fallback();
|
|
981
|
+
function preserveVariables2(text) {
|
|
982
|
+
const variableMap = /* @__PURE__ */ new Map();
|
|
983
|
+
let placeholderIndex = 0;
|
|
984
|
+
const textWithPlaceholders = text.replace(/\{\{([^}]+)\}\}/g, (match) => {
|
|
985
|
+
const placeholder = `XXX_${placeholderIndex}_XXX`;
|
|
986
|
+
variableMap.set(placeholder, match);
|
|
987
|
+
placeholderIndex++;
|
|
988
|
+
return placeholder;
|
|
989
|
+
});
|
|
990
|
+
return { textWithPlaceholders, variableMap };
|
|
991
|
+
}
|
|
992
|
+
function restoreVariables2(text, variableMap) {
|
|
993
|
+
let result = text;
|
|
994
|
+
for (const [placeholder, original] of variableMap) {
|
|
995
|
+
result = result.replace(new RegExp(placeholder, "g"), original);
|
|
996
|
+
}
|
|
997
|
+
return result;
|
|
998
|
+
}
|
|
906
999
|
var GoogleTranslateProvider = class {
|
|
907
1000
|
async translate(options) {
|
|
908
1001
|
const { text, sourceLang, targetLang, apiKey, useFallbackLanguages = true } = options;
|
|
@@ -919,7 +1012,7 @@ var GoogleTranslateProvider = class {
|
|
|
919
1012
|
logLanguageFallback(sourceLangResult, "google");
|
|
920
1013
|
resolvedSourceLang = sourceLangResult.resolvedLanguage;
|
|
921
1014
|
}
|
|
922
|
-
const { textWithPlaceholders, variableMap } =
|
|
1015
|
+
const { textWithPlaceholders, variableMap } = preserveVariables2(text);
|
|
923
1016
|
const url = `https://translation.googleapis.com/language/translate/v2?key=${apiKey}`;
|
|
924
1017
|
const sourceForGoogle = resolvedSourceLang?.includes("_") ? resolvedSourceLang.split("_")[0] : resolvedSourceLang;
|
|
925
1018
|
const targetForGoogle = targetLangResult.resolvedLanguage.includes("_") ? targetLangResult.resolvedLanguage.split("_")[0] : targetLangResult.resolvedLanguage;
|
|
@@ -940,7 +1033,7 @@ var GoogleTranslateProvider = class {
|
|
|
940
1033
|
throw new Error(`Google Translate API error: ${data.error.message}`);
|
|
941
1034
|
}
|
|
942
1035
|
const translatedText = data.data.translations[0].translatedText;
|
|
943
|
-
return
|
|
1036
|
+
return restoreVariables2(translatedText, variableMap);
|
|
944
1037
|
}
|
|
945
1038
|
async translateBatch(texts, sourceLang, targetLang, apiKey, delayMs = 100) {
|
|
946
1039
|
const results = [];
|
|
@@ -998,6 +1091,16 @@ async function addTranslationKey(projectRoot, options) {
|
|
|
998
1091
|
const config = loadConfig(projectRoot);
|
|
999
1092
|
const translationsPath = path3.join(projectRoot, config.translationsPath);
|
|
1000
1093
|
const { namespace, key, value, autoTranslate = false, apiKey } = options;
|
|
1094
|
+
const currentProvider = getTranslationProvider();
|
|
1095
|
+
const isDefaultGoogleProvider = currentProvider.constructor.name === "GoogleTranslateProvider";
|
|
1096
|
+
if (isDefaultGoogleProvider) {
|
|
1097
|
+
const provider = config.provider || "deepl";
|
|
1098
|
+
if (provider === "deepl") {
|
|
1099
|
+
setTranslationProvider(new DeepLTranslateProvider());
|
|
1100
|
+
} else {
|
|
1101
|
+
setTranslationProvider(new GoogleTranslateProvider());
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1001
1104
|
console.log("=====");
|
|
1002
1105
|
console.log("Adding translation key");
|
|
1003
1106
|
console.log("=====");
|
|
@@ -1027,7 +1130,7 @@ async function addTranslationKey(projectRoot, options) {
|
|
|
1027
1130
|
targetTranslations[namespace] = {};
|
|
1028
1131
|
}
|
|
1029
1132
|
if (!targetTranslations[namespace][key] || targetTranslations[namespace][key].trim() === "") {
|
|
1030
|
-
const translated = await translateText(value, lang, sourceLang, apiKey);
|
|
1133
|
+
const translated = await translateText(value, lang, sourceLang, apiKey, config.useFallbackLanguages ?? true);
|
|
1031
1134
|
targetTranslations[namespace][key] = translated;
|
|
1032
1135
|
const sorted = sortKeys(targetTranslations[namespace]);
|
|
1033
1136
|
writeTranslation(translationsPath, lang, namespace, sorted);
|
|
@@ -1079,101 +1182,6 @@ async function addTranslationKeys(projectRoot, entries, autoTranslate = false, a
|
|
|
1079
1182
|
|
|
1080
1183
|
// src/translations/cli/auto-fill.ts
|
|
1081
1184
|
import * as path5 from "path";
|
|
1082
|
-
|
|
1083
|
-
// src/translations/utils/deepl-translate-provider.ts
|
|
1084
|
-
init_language_fallback();
|
|
1085
|
-
function preserveVariables2(text) {
|
|
1086
|
-
const variableMap = /* @__PURE__ */ new Map();
|
|
1087
|
-
let placeholderIndex = 0;
|
|
1088
|
-
const textWithPlaceholders = text.replace(/\{\{([^}]+)\}\}/g, (match) => {
|
|
1089
|
-
const placeholder = `XXX_${placeholderIndex}_XXX`;
|
|
1090
|
-
variableMap.set(placeholder, match);
|
|
1091
|
-
placeholderIndex++;
|
|
1092
|
-
return placeholder;
|
|
1093
|
-
});
|
|
1094
|
-
return { textWithPlaceholders, variableMap };
|
|
1095
|
-
}
|
|
1096
|
-
function restoreVariables2(text, variableMap) {
|
|
1097
|
-
let result = text;
|
|
1098
|
-
for (const [placeholder, original] of variableMap) {
|
|
1099
|
-
result = result.replace(new RegExp(placeholder, "g"), original);
|
|
1100
|
-
}
|
|
1101
|
-
return result;
|
|
1102
|
-
}
|
|
1103
|
-
function normalizeLanguageCode(langCode) {
|
|
1104
|
-
return langCode.replace("_", "-").toUpperCase();
|
|
1105
|
-
}
|
|
1106
|
-
var DeepLTranslateProvider = class {
|
|
1107
|
-
isFreeApi;
|
|
1108
|
-
constructor(isFreeApi = false) {
|
|
1109
|
-
this.isFreeApi = isFreeApi;
|
|
1110
|
-
}
|
|
1111
|
-
getApiEndpoint() {
|
|
1112
|
-
return this.isFreeApi ? "https://api-free.deepl.com/v2/translate" : "https://api.deepl.com/v2/translate";
|
|
1113
|
-
}
|
|
1114
|
-
async translate(options) {
|
|
1115
|
-
const { text, sourceLang, targetLang, apiKey, useFallbackLanguages = true } = options;
|
|
1116
|
-
if (!apiKey) {
|
|
1117
|
-
throw new Error(
|
|
1118
|
-
"DeepL API key is required. Set DEEPL_API_KEY environment variable or provide apiKey in options."
|
|
1119
|
-
);
|
|
1120
|
-
}
|
|
1121
|
-
const targetLangResult = resolveLanguageWithFallback(targetLang, "deepl", useFallbackLanguages);
|
|
1122
|
-
logLanguageFallback(targetLangResult, "deepl");
|
|
1123
|
-
let resolvedSourceLang;
|
|
1124
|
-
if (sourceLang) {
|
|
1125
|
-
const sourceLangResult = resolveLanguageWithFallback(sourceLang, "deepl", useFallbackLanguages);
|
|
1126
|
-
logLanguageFallback(sourceLangResult, "deepl");
|
|
1127
|
-
resolvedSourceLang = sourceLangResult.resolvedLanguage;
|
|
1128
|
-
}
|
|
1129
|
-
const { textWithPlaceholders, variableMap } = preserveVariables2(text);
|
|
1130
|
-
const body = {
|
|
1131
|
-
text: [textWithPlaceholders],
|
|
1132
|
-
target_lang: normalizeLanguageCode(targetLangResult.resolvedLanguage),
|
|
1133
|
-
...resolvedSourceLang && { source_lang: normalizeLanguageCode(resolvedSourceLang) }
|
|
1134
|
-
};
|
|
1135
|
-
const response = await fetch(this.getApiEndpoint(), {
|
|
1136
|
-
method: "POST",
|
|
1137
|
-
headers: {
|
|
1138
|
-
Authorization: `DeepL-Auth-Key ${apiKey}`,
|
|
1139
|
-
"Content-Type": "application/json"
|
|
1140
|
-
},
|
|
1141
|
-
body: JSON.stringify(body)
|
|
1142
|
-
});
|
|
1143
|
-
if (!response.ok) {
|
|
1144
|
-
const errorData = await response.json().catch(() => ({}));
|
|
1145
|
-
throw new Error(`DeepL API error: ${errorData.message || response.statusText} (${response.status})`);
|
|
1146
|
-
}
|
|
1147
|
-
const data = await response.json();
|
|
1148
|
-
if (!data.translations || data.translations.length === 0) {
|
|
1149
|
-
throw new Error("DeepL API returned no translations");
|
|
1150
|
-
}
|
|
1151
|
-
const translatedText = data.translations[0].text;
|
|
1152
|
-
return restoreVariables2(translatedText, variableMap);
|
|
1153
|
-
}
|
|
1154
|
-
async translateBatch(texts, sourceLang, targetLang, apiKey, delayMs = 100) {
|
|
1155
|
-
const results = [];
|
|
1156
|
-
for (const text of texts) {
|
|
1157
|
-
const translated = await this.translate({
|
|
1158
|
-
text,
|
|
1159
|
-
sourceLang,
|
|
1160
|
-
targetLang,
|
|
1161
|
-
apiKey
|
|
1162
|
-
});
|
|
1163
|
-
results.push(translated);
|
|
1164
|
-
if (delayMs > 0) {
|
|
1165
|
-
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
1166
|
-
}
|
|
1167
|
-
}
|
|
1168
|
-
return results;
|
|
1169
|
-
}
|
|
1170
|
-
async validateConfig() {
|
|
1171
|
-
const apiKey = process.env.DEEPL_API_KEY;
|
|
1172
|
-
return !!apiKey;
|
|
1173
|
-
}
|
|
1174
|
-
};
|
|
1175
|
-
|
|
1176
|
-
// src/translations/cli/auto-fill.ts
|
|
1177
1185
|
init_utils();
|
|
1178
1186
|
init_init();
|
|
1179
1187
|
|