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.

113 lines 3.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const crypto_1 = require("crypto"); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); class TranslationCache { static cache = new Map(); static loaded = false; static enabled = true; static cacheDirectory = path_1.default.resolve(process.cwd(), ".cache"); static cacheFilename = "translations.json"; static configure({ enabled, cacheDirectory, }) { this.enabled = enabled; if (cacheDirectory) { const resolved = path_1.default.resolve(cacheDirectory); if (resolved !== this.cacheDirectory) { this.cacheDirectory = resolved; this.cache.clear(); this.loaded = false; } } if (!this.enabled) { this.loaded = false; this.cache.clear(); return; } this.ensureLoaded(); } static isEnabled() { return this.enabled; } static get(params) { if (!this.enabled) return undefined; this.ensureLoaded(); return this.cache.get(this.buildKey(params)); } static set(params, translatedValue) { if (!this.enabled) return; this.ensureLoaded(); this.cache.set(this.buildKey(params), translatedValue); this.persist(); } static clear() { this.cache.clear(); this.loaded = this.enabled; try { if (fs_1.default.existsSync(this.cacheFilePath())) { fs_1.default.unlinkSync(this.cacheFilePath()); } if (fs_1.default.existsSync(this.cacheDirectory) && fs_1.default.readdirSync(this.cacheDirectory).length === 0) { fs_1.default.rmdirSync(this.cacheDirectory); } } catch (error) { console.warn(`Failed to clear translation cache: ${error}`); } } static ensureLoaded() { if (this.loaded) return; this.loaded = true; try { if (!fs_1.default.existsSync(this.cacheFilePath())) { return; } const contents = fs_1.default.readFileSync(this.cacheFilePath(), "utf-8"); if (!contents) { return; } const parsed = JSON.parse(contents); this.cache = new Map(Object.entries(parsed)); } catch (error) { console.warn(`Failed to load translation cache: ${error}`); this.cache.clear(); } } static persist() { if (!this.enabled) return; try { fs_1.default.mkdirSync(this.cacheDirectory, { recursive: true }); const serialized = JSON.stringify(Object.fromEntries(this.cache.entries()), null, 2); fs_1.default.writeFileSync(this.cacheFilePath(), serialized, "utf-8"); } catch (error) { console.warn(`Failed to persist translation cache: ${error}`); } } static cacheFilePath() { return path_1.default.join(this.cacheDirectory, this.cacheFilename); } static buildKey({ model, inputLanguageCode, outputLanguageCode, keyPath, originalValue, }) { const hashedOriginal = (0, crypto_1.createHash)("sha256") .update(originalValue) .digest("hex"); return [ String(model), inputLanguageCode, outputLanguageCode, keyPath, hashedOriginal, ].join("::"); } } exports.default = TranslationCache; //# sourceMappingURL=translation_cache.js.map