UNPKG

i18n-ai-translate

Version:

AI-powered localization CLI, Node library, and GitHub Action. Translate i18next JSON, Gettext PO, Java .properties, and iOS .strings with ChatGPT, Claude, Gemini, or local Ollama models.

104 lines 4.09 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DEFAULT_CACHE_PATH = exports.CACHE_VERSION = void 0; exports.createCache = createCache; exports.cacheKey = cacheKey; exports.getCachedTranslation = getCachedTranslation; exports.setCachedTranslation = setCachedTranslation; exports.loadCache = loadCache; exports.saveCache = saveCache; const utils_1 = require("./utils"); const crypto_1 = __importDefault(require("crypto")); const fs_1 = __importDefault(require("fs")); /** Bump when the on-disk shape changes; older caches are ignored. */ exports.CACHE_VERSION = 1; /** Default cache file written into the cwd when `--cache` has no path. */ exports.DEFAULT_CACHE_PATH = ".i18n-ai-translate-cache.json"; /** * @returns a fresh, empty cache at the current schema version */ function createCache() { return { entries: {}, version: exports.CACHE_VERSION }; } /** * Compute the content-addressed key for one translatable string. * @param inputLanguageCode - the source language code * @param outputLanguageCode - the target language code * @param context - the `--context` string ("" when unset) * @param source - the source text being translated * @returns a hex sha256 digest uniquely identifying the entry */ function cacheKey(inputLanguageCode, outputLanguageCode, context, source) { return crypto_1.default .createHash("sha256") .update(`${inputLanguageCode}${outputLanguageCode}${context}${source}`) .digest("hex"); } /** * Look up a previously cached translation. * @param cache - the translation memory * @param inputLanguageCode - the source language code * @param outputLanguageCode - the target language code * @param context - the `--context` string ("" when unset) * @param source - the source text being translated * @returns the cached translation, or undefined on a miss */ function getCachedTranslation(cache, inputLanguageCode, outputLanguageCode, context, source) { return cache.entries[cacheKey(inputLanguageCode, outputLanguageCode, context, source)]; } /** * Record a translation for reuse on later runs. * @param cache - the translation memory * @param inputLanguageCode - the source language code * @param outputLanguageCode - the target language code * @param context - the `--context` string ("" when unset) * @param source - the source text being translated * @param translated - the translated text to store */ function setCachedTranslation(cache, inputLanguageCode, outputLanguageCode, context, source, translated) { cache.entries[cacheKey(inputLanguageCode, outputLanguageCode, context, source)] = translated; } /** * Load a cache from disk, tolerating a missing or incompatible file by * returning a fresh cache rather than throwing — a stale cache should * never break a translation run. * @param filePath - path to the cache JSON file * @returns the loaded cache, or a fresh one */ function loadCache(filePath) { let raw; try { raw = fs_1.default.readFileSync(filePath, "utf-8"); } catch { // Missing file on the first run is expected. return createCache(); } try { const parsed = JSON.parse(raw); if (parsed && typeof parsed === "object" && parsed.version === exports.CACHE_VERSION && parsed.entries && typeof parsed.entries === "object") { return parsed; } } catch { // Fall through to the warning below. } (0, utils_1.printWarn)(`Ignoring incompatible cache at ${filePath}; starting fresh.`); return createCache(); } /** * Persist a cache to disk as pretty-printed JSON with a trailing newline. * @param filePath - path to the cache JSON file * @param cache - the translation memory to write */ function saveCache(filePath, cache) { fs_1.default.writeFileSync(filePath, `${JSON.stringify(cache, null, 4)}\n`); } //# sourceMappingURL=cache.js.map