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.
147 lines • 6.3 kB
JavaScript
;
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 });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const engine_1 = __importDefault(require("../enums/engine"));
const prompt_mode_1 = __importDefault(require("../enums/prompt_mode"));
const TEST_CACHE_DIR = path_1.default.resolve(process.cwd(), ".cache-test");
const TEST_CACHE_FILE = path_1.default.join(TEST_CACHE_DIR, "translations.json");
const params = {
inputLanguageCode: "en",
keyPath: "greeting",
model: "gpt-4o",
originalValue: "Hello, world!",
outputLanguageCode: "fr",
};
function cleanupTestCache() {
if (fs_1.default.existsSync(TEST_CACHE_DIR)) {
fs_1.default.rmSync(TEST_CACHE_DIR, { recursive: true, force: true });
}
}
describe("TranslationCache", () => {
beforeEach(() => {
cleanupTestCache();
jest.resetModules();
});
afterEach(() => {
cleanupTestCache();
});
it("persists entries to disk and reloads them", async () => {
const { default: TranslationCache } = await Promise.resolve().then(() => __importStar(require("../cache/translation_cache")));
TranslationCache.configure({
cacheDirectory: TEST_CACHE_DIR,
enabled: true,
});
TranslationCache.set(params, "Bonjour, le monde!");
expect(fs_1.default.existsSync(TEST_CACHE_FILE)).toBe(true);
jest.resetModules();
const { default: ReloadedCache } = await Promise.resolve().then(() => __importStar(require("../cache/translation_cache")));
ReloadedCache.configure({
cacheDirectory: TEST_CACHE_DIR,
enabled: true,
});
expect(ReloadedCache.get(params)).toBe("Bonjour, le monde!");
});
it("skips persistence when disabled", async () => {
const { default: TranslationCache } = await Promise.resolve().then(() => __importStar(require("../cache/translation_cache")));
TranslationCache.configure({
cacheDirectory: TEST_CACHE_DIR,
enabled: false,
});
TranslationCache.set(params, "Ignored");
expect(fs_1.default.existsSync(TEST_CACHE_DIR)).toBe(false);
expect(TranslationCache.get(params)).toBeUndefined();
});
it("clears both in-memory and on-disk cache", async () => {
const { default: TranslationCache } = await Promise.resolve().then(() => __importStar(require("../cache/translation_cache")));
TranslationCache.configure({
cacheDirectory: TEST_CACHE_DIR,
enabled: true,
});
TranslationCache.set(params, "Salut");
expect(fs_1.default.existsSync(TEST_CACHE_FILE)).toBe(true);
expect(TranslationCache.get(params)).toBe("Salut");
TranslationCache.clear();
expect(fs_1.default.existsSync(TEST_CACHE_FILE)).toBe(false);
expect(TranslationCache.get(params)).toBeUndefined();
});
});
describe("translate cache configuration", () => {
beforeEach(() => {
cleanupTestCache();
jest.resetModules();
});
afterEach(() => {
cleanupTestCache();
});
it("disables the cache when disableCache is true", async () => {
const cacheModule = await Promise.resolve().then(() => __importStar(require("../cache/translation_cache")));
const configureSpy = jest.spyOn(cacheModule.default, "configure");
const { translate } = await Promise.resolve().then(() => __importStar(require("../translate")));
await translate({
disableCache: true,
engine: engine_1.default.ChatGPT,
inputJSON: { hello: "Hello" },
inputLanguageCode: "en",
model: "gpt-4o",
outputLanguageCode: "fr",
promptMode: prompt_mode_1.default.JSON,
rateLimitMs: 0,
});
expect(configureSpy).toHaveBeenCalledWith({ enabled: false });
configureSpy.mockRestore();
});
it("enables the cache by default", async () => {
const cacheModule = await Promise.resolve().then(() => __importStar(require("../cache/translation_cache")));
const configureSpy = jest.spyOn(cacheModule.default, "configure");
const { translate } = await Promise.resolve().then(() => __importStar(require("../translate")));
await translate({
engine: engine_1.default.ChatGPT,
inputJSON: { hello: "Hello" },
inputLanguageCode: "en",
model: "gpt-4o",
outputLanguageCode: "fr",
promptMode: prompt_mode_1.default.JSON,
rateLimitMs: 0,
});
expect(configureSpy).toHaveBeenCalledWith({ enabled: true });
configureSpy.mockRestore();
});
});
//# sourceMappingURL=translation_cache.spec.js.map