auto-translate-json-library
Version:
Automatically translates JSON language files to other languages using Google Translate,AWS,Azure,DeepL,OpenAI or local OpenAI compatible server
182 lines (181 loc) • 5.43 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenAITranslate = void 0;
const openai_1 = require("openai");
const util_1 = require("./util");
const supportedLanguages = {
af: "Afrikaans",
sq: "Albanian",
am: "Amharic",
ar: "Arabic",
hy: "Armenian",
az: "Azerbaijani",
eu: "Basque",
be: "Belarusian",
bn: "Bengali",
bs: "Bosnian",
bg: "Bulgarian",
ca: "Catalan",
ceb: "Cebuano",
"zh-CN": "Chinese (Simplified)",
"zh-TW": "Chinese (Traditional)",
co: "Corsican",
hr: "Croatian",
cs: "Czech",
da: "Danish",
nl: "Dutch",
en: "English",
eo: "Esperanto",
et: "Estonian",
fi: "Finnish",
fr: "French",
fy: "Frisian",
gl: "Galician",
ka: "Georgian",
de: "German",
el: "Greek",
gu: "Gujarati",
ht: "Haitian Creole",
ha: "Hausa",
haw: "Hawaiian",
he: "Hebrew",
hi: "Hindi",
hmn: "Hmong",
hu: "Hungarian",
is: "Icelandic",
ig: "Igbo",
id: "Indonesian",
ga: "Irish",
it: "Italian",
ja: "Japanese",
jv: "Javanese",
kn: "Kannada",
kk: "Kazakh",
km: "Khmer",
ko: "Korean",
ku: "Kurdish",
ky: "Kyrgyz",
lo: "Lao",
la: "Latin",
lv: "Latvian",
lt: "Lithuanian",
lb: "Luxembourgish",
mk: "Macedonian",
mg: "Malagasy",
ms: "Malay",
ml: "Malayalam",
mt: "Maltese",
mi: "Maori",
mr: "Marathi",
mn: "Mongolian",
my: "Myanmar (Burmese)",
ne: "Nepali",
no: "Norwegian",
ny: "Nyanja (Chichewa)",
or: "Odia (Oriya)",
ps: "Pashto",
fa: "Persian",
pl: "Polish",
pt: "Portuguese",
pa: "Punjabi",
ro: "Romanian",
ru: "Russian",
sm: "Samoan",
gd: "Scots Gaelic",
sr: "Serbian",
st: "Sesotho",
sn: "Shona",
sd: "Sindhi",
si: "Sinhala (Sinhalese)",
sk: "Slovak",
sl: "Slovenian",
so: "Somali",
es: "Spanish",
su: "Sundanese",
sw: "Swahili",
sv: "Swedish",
tl: "Tagalog (Filipino)",
tg: "Tajik",
ta: "Tamil",
tt: "Tatar",
te: "Telugu",
th: "Thai",
tr: "Turkish",
tk: "Turkmen",
uk: "Ukrainian",
ur: "Urdu",
ug: "Uyghur",
uz: "Uzbek",
vi: "Vietnamese",
cy: "Welsh",
xh: "Xhosa",
yi: "Yiddish",
yo: "Yoruba",
zu: "Zulu",
};
class OpenAITranslate {
constructor(apiKey, baseUrl, model, maxTokens, temperature, topP, n, frequencyPenalty, presencePenalty) {
this.model = model;
this.maxTokens = maxTokens;
this.temperature = temperature;
this.topP = topP;
this.n = n;
this.frequencyPenalty = frequencyPenalty;
this.presencePenalty = presencePenalty;
const configuration = {
apiKey: apiKey,
baseURL: baseUrl,
};
this.openai = new openai_1.OpenAI(configuration);
}
isValidLocale(targetLocale) {
return targetLocale in supportedLanguages;
}
translateText(text, sourceLocale, targetLocale) {
return __awaiter(this, void 0, void 0, function* () {
if (sourceLocale !== "en") {
throw Error(`${sourceLocale} is not supported.Currently we support just English `);
}
let result = "";
let args;
({ args, text } = util_1.Util.replaceContextVariables(text));
const systemPrompt = `You will be provided with a sentence or words in English, and your task is to translate it into ${supportedLanguages[targetLocale]}`;
const userPrompt = text;
const response = yield this.openai.chat.completions.create({
model: this.model,
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: userPrompt },
],
temperature: this.temperature,
max_tokens: this.maxTokens,
top_p: this.topP,
n: this.n,
frequency_penalty: this.frequencyPenalty,
presence_penalty: this.presencePenalty,
});
if (response.choices[0].message.content !== null) {
result = util_1.Util.replaceArgumentsWithNumbers(args, response.choices[0].message.content);
result = result.replace(/^\n+|\n+$/g, "");
}
else {
console.error(`can not translate text with
system prompt : ${systemPrompt}
and
user prompt : ${userPrompt} `);
result = util_1.Util.replaceArgumentsWithNumbers(args, result);
}
return result;
});
}
}
exports.OpenAITranslate = OpenAITranslate;