@scoutello/i18n-magic
Version:
Intelligent CLI toolkit that automates internationalization workflows with AI-powered translations for JavaScript/TypeScript projects
66 lines • 2.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.translateMissing = void 0;
const utils_1 = require("../lib/utils");
const clean_1 = require("./clean");
const translateMissing = async (config) => {
const { loadPath, savePath, defaultLocale, namespaces, locales, context, openai, disableTranslation, autoClear, } = config;
// Run clean command first if autoClear is enabled
if (autoClear) {
console.log("🧹 Auto-clearing unused translations before scanning...");
await (0, clean_1.removeUnusedKeys)(config);
console.log("✅ Auto-clear completed. Now scanning for missing translations...\n");
}
const newKeys = await (0, utils_1.getMissingKeys)(config);
if (newKeys.length === 0) {
console.log("No new keys found.");
await (0, utils_1.checkAllKeysExist)(config);
return;
}
console.log(`${newKeys.length} keys are missing. Please provide the values for the following keys in ${defaultLocale}:`);
const newKeysWithDefaultLocale = [];
for (const newKey of newKeys) {
const answer = await (0, utils_1.getTextInput)(newKey.key);
newKeysWithDefaultLocale.push({
key: newKey.key,
namespace: newKey.namespace,
value: answer,
});
}
const newKeysObject = newKeysWithDefaultLocale.reduce((prev, next) => {
prev[next.key] = next.value;
return prev;
}, {});
const allLocales = disableTranslation ? [defaultLocale] : locales;
for (const locale of allLocales) {
let translatedValues = {};
if (locale === defaultLocale) {
translatedValues = newKeysObject;
}
else {
translatedValues = await (0, utils_1.translateKey)({
inputLanguage: defaultLocale,
outputLanguage: locale,
context,
object: newKeysObject,
openai,
model: config.model,
});
}
for (const namespace of namespaces) {
const existingKeys = await (0, utils_1.loadLocalesFile)(loadPath, locale, namespace);
const relevantKeys = newKeysWithDefaultLocale.filter((key) => key.namespace === namespace);
if (relevantKeys.length === 0) {
continue;
}
for (const key of relevantKeys) {
existingKeys[key.key] = translatedValues[key.key];
}
(0, utils_1.writeLocalesFile)(savePath, locale, namespace, existingKeys);
}
}
await (0, utils_1.checkAllKeysExist)(config);
console.log(`Successfully translated ${newKeys.length} keys.`);
};
exports.translateMissing = translateMissing;
//# sourceMappingURL=scan.js.map