UNPKG

i18n-ai-translate

Version:

Use LLMs to translate your i18n JSON to any language.

110 lines 3.96 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.delay = delay; exports.retryJob = retryJob; exports.getLanguageCodeFromFilename = getLanguageCodeFromFilename; exports.getAllLanguageCodes = getAllLanguageCodes; exports.getAllFilesInPath = getAllFilesInPath; exports.getTranslationDirectoryKey = getTranslationDirectoryKey; exports.isNAK = isNAK; exports.isACK = isACK; const iso_639_1_1 = __importDefault(require("iso-639-1")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); /** * @param delayDuration - time (in ms) to delay * @returns a promise that resolves after delayDuration */ function delay(delayDuration) { // eslint-disable-next-line no-promise-executor-return return new Promise((resolve) => setTimeout(resolve, delayDuration)); } /** * @param job - the function to retry * @param jobArgs - arguments to pass to job * @param maxRetries - retries of job before throwing * @param firstTry - whether this is the first try * @param delayDuration - time (in ms) before attempting job retry * @param sendError - whether to send a warning or error * @returns the result of job */ async function retryJob(job, jobArgs, maxRetries, firstTry, delayDuration, sendError = true) { if (!firstTry && delayDuration) { await delay(delayDuration); } return job(...jobArgs).catch((err) => { if (sendError) { console.error(`err = ${err}`); } else { console.warn(`err = ${err}`); } if (maxRetries <= 0) { throw err; } return retryJob(job, jobArgs, maxRetries - 1, false, delayDuration); }); } /** * @param filename - the filename to get the language from * @returns the language code from the filename */ function getLanguageCodeFromFilename(filename) { const splitFilename = filename.split("/"); const lastPart = splitFilename[splitFilename.length - 1]; const splitLastPart = lastPart.split("."); return splitLastPart[0]; } /** * @returns all language codes */ function getAllLanguageCodes() { return iso_639_1_1.default.getAllCodes(); } /** * @param directory - the directory to list all files for * @returns all files with their absolute path that exist within the directory, recursively */ function getAllFilesInPath(directory) { const files = []; for (const fileOrDir of fs_1.default.readdirSync(directory)) { const fullPath = path_1.default.join(directory, fileOrDir); if (fs_1.default.lstatSync(fullPath).isDirectory()) { files.push(...getAllFilesInPath(fullPath)); } else { files.push(fullPath); } } return files; } /** * @param sourceFilePath - the source file's path * @param key - the key associated with the translation * @param inputLanguageCode - the language code of the source language * @param outputLanguageCode - the language code of the output language * @returns a key to use when translating a key from a directory; * swaps the input language code with the output language code */ function getTranslationDirectoryKey(sourceFilePath, key, inputLanguageCode, outputLanguageCode) { const outputPath = sourceFilePath.replace(`/${inputLanguageCode}/`, `/${outputLanguageCode ?? inputLanguageCode}/`); return `${outputPath}:${key}`; } /** * @param response - the message from the LLM * @returns whether the response includes NAK */ function isNAK(response) { return response.includes("NAK") && !response.includes("ACK"); } /** * @param response - the message from the LLM * @returns whether the response only contains ACK and not NAK */ function isACK(response) { return response.includes("ACK") && !response.includes("NAK"); } //# sourceMappingURL=utils.js.map