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

174 lines (173 loc) 7.6 kB
#!/usr/bin/env node "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _a, _b, _c, _d, _e, _f, _g, _h; Object.defineProperty(exports, "__esModule", { value: true }); const node_path_1 = __importDefault(require("node:path")); const minimist_1 = __importDefault(require("minimist")); const picocolors_1 = __importDefault(require("picocolors")); const package_json_1 = require("../../package.json"); const lib_1 = require("../lib"); require("dotenv").config(); // Define a function to display the help message function displayHelp() { console.log(`🔨 Auto translate json cli v${package_json_1.version}`); console.log("Usage: atj [options] <inputPath>"); console.log(""); console.log("Options:"); console.log(" --help, -h Display this help message"); console.log(" --mode, -m <mode> Specify the translation mode:file or folder"); console.log(" --engine, -e <engine> Specify the translation engine:aws,azure,google,deepLPro,deepLFree or openai"); console.log(" --sourceLocale, -s <locale> Specify the source locale"); console.log(" --keepTranslations, --no-keepTranslations Keep or retranslate existing translations"); console.log(" --keepExtraTranslations, --no-keepExtraTranslations Keep or remove extra translations"); console.log(""); console.log("Default values"); console.log(" --mode, -m <mode> file"); console.log(" --engine, -e <engine> aws"); console.log(" --sourceLocale, -s <locale> en"); console.log(" --keepTranslations, --no-keepTranslations --keepTranslations"); console.log(" --keepExtraTranslations, --no-keepExtraTranslations --no-keepExtraTranslations"); } const arguments_ = process.argv.slice(2); if (arguments_.length === 0) { displayHelp(); process.exit(0); } const flags = (0, minimist_1.default)(arguments_, { alias: { mode: ["m"], engine: ["e"], keepTranslations: ["kt"], keepExtraTranslations: ["ket"], sourceLocale: ["s"], help: ["h"], }, string: ["mode", "engine", "sourceLocale"], boolean: ["keepTranslations", "keepExtraTranslations", "help"], default: { engine: "aws", sourceLocale: "en", keepTranslations: true, keepExtraTranslations: false, mode: "file", }, }); console.log(picocolors_1.default.green(`🔨 Auto translate json cli v${package_json_1.version}`)); if (flags.help) { displayHelp(); process.exit(0); } const inputPath = flags._[0]; const { mode, engine, sourceLocale, keepTranslations, keepExtraTranslations } = flags; const config = {}; // set translate engine config switch (engine) { case "google": { const googleApiKey = process.env.ATJ_GOOGLE_API_KEY; if (!googleApiKey) { console.error(picocolors_1.default.red("❌ Google api key not found in environment variable ATJ_GOOGLE_API_KEY")); process.exit(1); } config.translationKeyInfo = { kind: "google", apiKey: googleApiKey, }; break; } case "aws": { const awsAccessKeyId = process.env.ATJ_AWS_ACCESS_KEY_ID; const awsSecretAccessKey = process.env.ATJ_AWS_SECRET_ACCESS_KEY; const awsRegion = process.env.ATJ_AWS_REGION; if (!awsAccessKeyId || !awsSecretAccessKey || !awsRegion) { console.error(picocolors_1.default.red("❌ AWS credentials not found in environment variables ATJ_AWS_ACCESS_KEY_ID, ATJ_AWS_SECRET_ACCESS_KEY, ATJ_AWS_REGION")); process.exit(1); } config.translationKeyInfo = { kind: "aws", accessKeyId: awsAccessKeyId, secretAccessKey: awsSecretAccessKey, region: awsRegion, }; break; } case "azure": { const azureSecretKey = process.env.ATJ_AZURE_SECRET_KEY; const azureRegion = process.env.ATJ_AZURE_REGION; if (!azureSecretKey || !azureRegion) { console.error(picocolors_1.default.red("❌ Azure credentials not found in environment variables ATJ_AZURE_SECRET_KEY, ATJ_AZURE_REGION")); process.exit(1); } config.translationKeyInfo = { kind: "azure", secretKey: azureSecretKey, region: azureRegion, }; break; } case "openai": { const openaiApiKey = process.env.ATJ_OPEN_AI_SECRET_KEY; if (!openaiApiKey) { console.error(picocolors_1.default.red("❌ Openai api key not found in environment variable ATJ_OPEN_AI_SECRET_KEY")); process.exit(1); } config.translationKeyInfo = { kind: "openai", apiKey: openaiApiKey, baseUrl: (_a = process.env.ATJ_OPEN_AI_BASE_URL) !== null && _a !== void 0 ? _a : "https://api.openai.com/v1", model: (_b = process.env.ATJ_OPEN_AI_MODEL) !== null && _b !== void 0 ? _b : "gpt-3.5-turbo", maxTokens: Number((_c = process.env.ATJ_OPEN_AI_MAX_TOKENS) !== null && _c !== void 0 ? _c : "256"), temperature: Number((_d = process.env.ATJ_OPEN_AI_TEMPERATURE) !== null && _d !== void 0 ? _d : "0.7"), topP: Number((_e = process.env.ATJ_OPEN_AI_TOP_P) !== null && _e !== void 0 ? _e : "0.9"), n: Number((_f = process.env.ATJ_OPEN_AI_N) !== null && _f !== void 0 ? _f : "1"), frequencyPenalty: Number((_g = process.env.ATJ_OPEN_AI_FREQUENCY_PENALTY) !== null && _g !== void 0 ? _g : "0"), presencePenalty: Number((_h = process.env.ATJ_OPEN_AI_PRESENCE_PENALTY) !== null && _h !== void 0 ? _h : "0"), }; break; } case "deepLPro": { const deepLProSecretKey = process.env.ATJ_DEEPL_PRO_SECRET_KEY; if (!deepLProSecretKey) { console.log(picocolors_1.default.red("❌ DeepL pro api key not found in environment variable ATJ_DEEPL_PRO_SECRET_KEY")); process.exit(1); } config.translationKeyInfo = { kind: "deepLPro", secretKey: deepLProSecretKey, }; break; } case "deepLFree": { const deepLFreeSecretKey = process.env.ATJ_DEEPL_FREE_SECRET_KEY; if (!deepLFreeSecretKey) { console.error(picocolors_1.default.red("❌ DeepL free api key not found in environment variable ATJ_DEEPL_FREE_SECRET_KEY")); process.exit(1); } config.translationKeyInfo = { kind: "deepLFree", secretKey: deepLFreeSecretKey, }; break; } default: console.error(picocolors_1.default.red(`❌ Engine ${engine} not supported`)); process.exit(1); } if (process.env.ATJ_START_DELIMITER) { config.startDelimiter = process.env.ATJ_START_DELIMITER; } if (process.env.ATJ_END_DELIMITER) { config.endDelimiter = process.env.ATJ_END_DELIMITER; } config.sourceLocale = sourceLocale; config.keepTranslations = keepTranslations ? "keep" : "retranslate"; config.keepExtraTranslations = keepExtraTranslations ? "keep" : "remove"; config.mode = mode; const sourcePath = node_path_1.default.join(process.cwd(), inputPath); console.log(picocolors_1.default.green(`🌐 Translating ${sourcePath}`)); (0, lib_1.translate)(sourcePath, config).catch((error) => { console.error(picocolors_1.default.red("❌ Translate error:\n"), error); process.exit(1); });