UNPKG

i18n-ai-translate

Version:

Use LLMs to translate your i18n JSON to any language.

168 lines 8.36 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = buildDiffCommand; const constants_1 = require("./constants"); const commander_1 = require("commander"); const utils_1 = require("./utils"); const cli_helpers_1 = require("./cli_helpers"); const translate_directory_1 = require("./translate_directory"); const translate_file_1 = require("./translate_file"); const fs_1 = __importStar(require("fs")); const path_1 = __importDefault(require("path")); /** * Builds the diff command for comparing i18n files or directories. * @returns the diff command with its options and action. */ function buildDiffCommand() { return new commander_1.Command("diff") .requiredOption("-b, --before <fileOrDirectoryBefore>", "Source i18n file or directory before changes, in the jsons/ directory if a relative path is given") .requiredOption("-a, --after <fileOrDirectoryAfter>", "Source i18n file or directory after changes, in the jsons/ directory if a relative path is given") .requiredOption("-l, --input-language <inputLanguageCode>", "The input language's code, in ISO6391 (e.g. en, fr)") .requiredOption("-e, --engine <engine>", constants_1.CLI_HELP.Engine) .option("-m, --model <model>", constants_1.CLI_HELP.Model) .option("-r, --rate-limit-ms <rateLimitMs>", constants_1.CLI_HELP.RateLimit) .option("-k, --api-key <API key>", "API key") .option("-h, --host <hostIP:port>", constants_1.CLI_HELP.OllamaHost) .option("--ensure-changed-translation", constants_1.CLI_HELP.EnsureChangedTranslation, false) .option("-p, --templated-string-prefix <prefix>", "Prefix for templated strings", constants_1.DEFAULT_TEMPLATED_STRING_PREFIX) .option("-s, --templated-string-suffix <suffix>", "Suffix for templated strings", constants_1.DEFAULT_TEMPLATED_STRING_SUFFIX) .option("-n, --batch-size <batchSize>", constants_1.CLI_HELP.BatchSize) .option("--skip-translation-verification", constants_1.CLI_HELP.SkipTranslationVerification, false) .option("--skip-styling-verification", constants_1.CLI_HELP.SkipStylingVerification, false) .option("--override-prompt <path to JSON file>", constants_1.CLI_HELP.OverridePromptFile) .option("--verbose", constants_1.CLI_HELP.Verbose, false) .option("--prompt-mode <prompt-mode>", constants_1.CLI_HELP.PromptMode) .option("--batch-max-tokens <batch-max-tokens>", constants_1.CLI_HELP.MaxTokens) .option("--dry-run", constants_1.CLI_HELP.DryRun, false) .action(async (options) => { const { model, chatParams, rateLimitMs, apiKey, host, promptMode, batchSize, batchMaxTokens, } = (0, cli_helpers_1.processModelArgs)(options); let overridePrompt; if (options.overridePrompt) { overridePrompt = (0, cli_helpers_1.processOverridePromptFile)(options.overridePrompt); } let dryRun; if (options.dryRun) { dryRun = { basePath: (0, fs_1.mkdtempSync)(`/tmp/i18n-ai-translate-${new Date().toISOString().replace(/[:.]/g, "-")}-`), }; } const jsonFolder = path_1.default.resolve(process.cwd(), "jsons"); let beforeInputPath; if (path_1.default.isAbsolute(options.before)) { beforeInputPath = path_1.default.resolve(options.before); } else { beforeInputPath = path_1.default.resolve(jsonFolder, options.before); if (!fs_1.default.existsSync(beforeInputPath)) { beforeInputPath = path_1.default.resolve(process.cwd(), options.before); } } let afterInputPath; if (path_1.default.isAbsolute(options.after)) { afterInputPath = path_1.default.resolve(options.after); } else { afterInputPath = path_1.default.resolve(jsonFolder, options.after); if (!fs_1.default.existsSync(afterInputPath)) { afterInputPath = path_1.default.resolve(process.cwd(), options.after); } } if (fs_1.default.statSync(beforeInputPath).isFile() !== fs_1.default.statSync(afterInputPath).isFile()) { (0, utils_1.printError)("--before and --after arguments must be both files or both directories"); return; } if (fs_1.default.statSync(beforeInputPath).isFile()) { // Ensure they're in the same path if (path_1.default.dirname(beforeInputPath) !== path_1.default.dirname(afterInputPath)) { (0, utils_1.printError)("Input files are not in the same directory"); return; } await (0, translate_file_1.translateFileDiff)({ apiKey, batchMaxTokens, batchSize, chatParams, dryRun, engine: options.engine, ensureChangedTranslation: options.ensureChangedTranslation, host, inputAfterFileOrPath: afterInputPath, inputBeforeFileOrPath: beforeInputPath, inputLanguageCode: options.inputLanguage, model, overridePrompt, promptMode, rateLimitMs, skipStylingVerification: options.skipStylingVerification, skipTranslationVerification: options.skipTranslationVerification, templatedStringPrefix: options.templatedStringPrefix, templatedStringSuffix: options.templatedStringSuffix, verbose: options.verbose, }); } else { await (0, translate_directory_1.translateDirectoryDiff)({ apiKey, baseDirectory: path_1.default.resolve(beforeInputPath, ".."), batchMaxTokens: options.batchMaxTokens, batchSize: options.batchSize, chatParams, dryRun, engine: options.engine, ensureChangedTranslation: options.ensureChangedTranslation, host, inputFolderNameAfter: afterInputPath, inputFolderNameBefore: beforeInputPath, inputLanguageCode: options.inputLanguage, model, overridePrompt, promptMode, rateLimitMs, skipStylingVerification: options.skipStylingVerification, skipTranslationVerification: options.skipTranslationVerification, templatedStringPrefix: options.templatedStringPrefix, templatedStringSuffix: options.templatedStringSuffix, verbose: options.verbose, }); } }); } //# sourceMappingURL=cli_diff.js.map