@akson/cortex-shopify-translations
Version:
Unified Shopify translations management client with product extraction, translation sync, and CLI tools
100 lines (84 loc) ⢠3 kB
JavaScript
/**
* Reset failed translations back to pending status
* Allows retrying translations that failed due to API errors
*/
import fs from 'fs/promises';
import path from 'path';
async function resetFailed(category = null, language = null) {
console.log('š Resetting failed translations...\n');
const contentDir = path.join(process.cwd(), 'translations', 'content');
// Get files to process
let files;
if (category) {
files = [`${category}.json`];
} else {
const allFiles = await fs.readdir(contentDir);
files = allFiles.filter(f => f.endsWith('.json'));
}
const languages = language ? [language] : ['de', 'it', 'en'];
let totalReset = 0;
for (const fileName of files) {
const filePath = path.join(contentDir, fileName);
try {
const data = JSON.parse(await fs.readFile(filePath, 'utf-8'));
let fileResets = 0;
data.translations.forEach(translation => {
languages.forEach(lang => {
if (translation.status[lang] === 'failed') {
translation.status[lang] = 'pending';
// Clear error message
delete translation[`${lang}_error`];
// Clear validation if exists
if (translation.validation && translation.validation[lang]) {
delete translation.validation[lang];
}
fileResets++;
totalReset++;
}
});
});
if (fileResets > 0) {
// Update metadata
data.metadata.last_updated = new Date().toISOString();
// Recalculate stats
const stats = {
total: data.translations.length,
de: { pending: 0, in_progress: 0, completed: 0, failed: 0, reviewed: 0 },
it: { pending: 0, in_progress: 0, completed: 0, failed: 0, reviewed: 0 },
en: { pending: 0, in_progress: 0, completed: 0, failed: 0, reviewed: 0 }
};
data.translations.forEach(t => {
['de', 'it', 'en'].forEach(l => {
const status = t.status[l] || 'pending';
if (!stats[l][status]) stats[l][status] = 0;
stats[l][status]++;
});
});
data.metadata.stats = stats;
await fs.writeFile(filePath, JSON.stringify(data, null, 2));
console.log(`ā ${fileName}: Reset ${fileResets} failed translations`);
}
} catch (error) {
console.error(`ā Error processing ${fileName}: ${error.message}`);
}
}
if (totalReset > 0) {
console.log(`\nā
Total reset: ${totalReset} translations`);
console.log('Run translate-file.mjs to retry these translations.');
} else {
console.log('No failed translations found.');
}
}
// CLI
const args = process.argv.slice(2);
let category = null;
let language = null;
args.forEach(arg => {
if (arg.startsWith('--category=')) {
category = arg.split('=')[1];
} else if (arg.startsWith('--lang=')) {
language = arg.split('=')[1];
}
});
resetFailed(category, language);