UNPKG

@scoutello/i18n-magic

Version:

Intelligent CLI toolkit that automates internationalization workflows with AI-powered translations for JavaScript/TypeScript projects

105 lines 4.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.replaceTranslation = void 0; const utils_1 = require("../lib/utils"); const getKeyToReplace = async (allAvailableKeys) => { const keyToReplace = await (0, utils_1.getTextInput)("Enter the key to replace the translation for: "); if (!allAvailableKeys[keyToReplace]) { console.log(`The key "${keyToReplace}" does not exist.`); return await getKeyToReplace(allAvailableKeys); } const namespaces = allAvailableKeys[keyToReplace].map((k) => k.namespace); console.log(`The key "${keyToReplace}" exists in namespaces: ${namespaces.join(", ")}.`); return { key: keyToReplace, namespaces }; }; const replaceTranslation = async (config, key) => { const { loadPath, savePath, defaultLocale, defaultNamespace, namespaces, locales, globPatterns, context, openai, } = config; // Find all keys with their namespaces from the codebase const keysWithNamespaces = await (0, utils_1.getKeysWithNamespaces)({ globPatterns, defaultNamespace, }); // Build a map of all available keys across all namespaces const allAvailableKeys = {}; for (const namespace of namespaces) { const keys = await (0, utils_1.loadLocalesFile)(loadPath, defaultLocale, namespace); for (const [keyName, value] of Object.entries(keys)) { if (!allAvailableKeys[keyName]) { allAvailableKeys[keyName] = []; } allAvailableKeys[keyName].push({ namespace, value }); } } let keyToReplace; let targetNamespaces = []; if (key) { if (allAvailableKeys[key]) { keyToReplace = key; // Determine which namespaces this key should be updated in based on usage const keyUsage = keysWithNamespaces.filter((k) => { const pureKey = (0, utils_1.getPureKey)(k.key, defaultNamespace, true); return pureKey === key || k.key === key; }); if (keyUsage.length > 0) { // Use namespaces from actual usage const allNamespaces = []; for (const k of keyUsage) { allNamespaces.push(...k.namespaces); } targetNamespaces = [...new Set(allNamespaces)]; } else { // Fallback to all namespaces where the key exists targetNamespaces = allAvailableKeys[key].map((k) => k.namespace); } console.log(`The key "${keyToReplace}" exists in namespaces: ${targetNamespaces.join(", ")}.`); } else { console.log(`The key "${key}" does not exist.`); const result = await getKeyToReplace(allAvailableKeys); keyToReplace = result.key; targetNamespaces = result.namespaces; } } else { const result = await getKeyToReplace(allAvailableKeys); keyToReplace = result.key; targetNamespaces = result.namespaces; } // Show current translations across namespaces for (const namespace of targetNamespaces) { const keys = await (0, utils_1.loadLocalesFile)(loadPath, defaultLocale, namespace); if (keys[keyToReplace]) { console.log(`Current translation in ${defaultLocale} (${namespace}): "${keys[keyToReplace]}"`); } } const newTranslation = await (0, utils_1.getTextInput)("Enter the new translation: "); // Update the key in all relevant namespaces and locales for (const namespace of targetNamespaces) { for (const locale of locales) { let newValue = ""; if (locale === defaultLocale) { newValue = newTranslation; } else { const translation = await (0, utils_1.translateKey)({ context, inputLanguage: defaultLocale, outputLanguage: locale, object: { [keyToReplace]: newTranslation, }, openai, model: config.model, }); newValue = translation[keyToReplace]; } const existingKeys = await (0, utils_1.loadLocalesFile)(loadPath, locale, namespace); existingKeys[keyToReplace] = newValue; await (0, utils_1.writeLocalesFile)(savePath, locale, namespace, existingKeys); console.log(`Updated "${keyToReplace}" in ${locale} (${namespace}): "${newValue}"`); } } }; exports.replaceTranslation = replaceTranslation; //# sourceMappingURL=replace.js.map