UNPKG

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

189 lines (187 loc) 5.01 kB
"use strict"; 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.AzureTranslate = void 0; const util_1 = require("./util"); const promises_1 = require("timers/promises"); const axios = require("axios").default; const { v4: uuidv4 } = require("uuid"); const supportedLanguages = [ "af", "sq", "am", "ar", "hy", "as", "az", "bn", "ba", "bs", "bg", "yue", "ca", "lzh", "zh-Hans", "zh-Hant", "hr", "cs", "da", "prs", "dv", "nl", "en", "et", "fj", "fil", "fi", "fr", "fr-ca", "ka", "de", "el", "gu", "ht", "he", "hi", "mww", "hu", "is", "id", "iu", "ga", "it", "ja", "kn", "kk", "km", "tlh-Latn", "tlh-Piqd", "ko", "ku", "kmr", "ky", "lo", "lv", "lt", "mk", "mg", "ms", "ml", "mt", "mi", "mr", "mn-Cyrl", "mn-Mong", "my", "ne", "nb", "or", "ps", "fa", "pl", "pt", "pt-pt", "pa", "otq", "ro", "ru", "sm", "sr-Cyrl", "sr-Latn", "sk", "sl", "es", "sw", "sv", "ty", "ta", "tt", "te", "th", "bo", "ti", "to", "tr", "tk", "uk", "ur", "ug", "uz", "vi", "cy", "yua", ]; class AzureTranslate { constructor(subscriptionKey, subscriptionRegion) { this.subscriptionKey = subscriptionKey; this.subscriptionRegion = subscriptionRegion; this.endpoint = "https://api.cognitive.microsofttranslator.com"; } isValidLocale(targetLocale) { return supportedLanguages.includes(targetLocale); } translateText(text, sourceLocale, targetLocale) { return __awaiter(this, void 0, void 0, function* () { var _a; let args; ({ args, text } = util_1.Util.replaceContextVariables(text)); let result = ""; const maxRetries = 5; let retryCount = 0; while (retryCount <= maxRetries) { try { const response = yield axios({ baseURL: this.endpoint, url: "/translate", method: "post", headers: { "Ocp-Apim-Subscription-Key": this.subscriptionKey, "Ocp-Apim-Subscription-Region": this.subscriptionRegion, "Content-type": "application/json", "X-ClientTraceId": uuidv4().toString(), }, params: { "api-version": "3.0", from: sourceLocale, to: [targetLocale], }, data: [ { text: text, }, ], responseType: "json", }); result = response.data[0].translations[0].text; break; // Success, exit the retry loop } catch (error) { if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 429 && retryCount < maxRetries) { retryCount++; console.log(`Rate limit exceeded. Retrying in 10 seconds... (Attempt ${retryCount}/${maxRetries})`); yield (0, promises_1.setTimeout)(10000); // Wait 10 seconds using timers/promises } else { throw error; // Re-throw if it's not a 429 error or we've exceeded max retries } } } result = util_1.Util.replaceArgumentsWithNumbers(args, result); return result; }); } } exports.AzureTranslate = AzureTranslate; // https://docs.microsoft.com/en-us/azure/cognitive-services/translator/quickstart-translator?tabs=nodejs#translate-text // Add your location, also known as region. The default is global. // This is required if using a Cognitive Services resource. /* */