#!/usr/bin/env node /** * Create Spanish (es) localizations for all English (en) content in Strapi */ const STRAPI_URL = process.env.STRAPI_URL || "http://localhost:1337"; async function apiRequest(endpoint, method = "GET", body = null) { const url = `${STRAPI_URL}/api${endpoint}`; const options = { method, headers: { "Content-Type": "application/json" }, ...(body ? { body: JSON.stringify(body) } : {}), }; const response = await fetch(url, options); const data = await response.json().catch(() => null); if (!response.ok) { throw new Error(`${response.status}: ${JSON.stringify(data)}`); } return data; } async function getAllEnDocuments(path) { const res = await apiRequest(`${path}?pagination[pageSize]=200&locale=en`); const items = res.data || []; // Deduplicate by documentId const seen = new Set(); return items.filter((item) => { if (seen.has(item.documentId)) return false; seen.add(item.documentId); return true; }); } async function createEsLocalization(path, documentId, data) { return apiRequest(`${path}/${documentId}?locale=es`, "PUT", { data }); } async function main() { console.log("🌐 Creating Spanish localizations...\n"); // 1. Documentaries console.log("šŸ“½ļø Documentaries:"); const docs = await getAllEnDocuments("/documentaries"); for (const doc of docs) { try { await createEsLocalization("/documentaries", doc.documentId, { title: doc.title, description: doc.description, }); console.log(` āœ… ${doc.title}`); } catch (err) { if (err.message.includes("already exists")) { console.log(` āš ļø Already has es: ${doc.title}`); } else { console.log(` āŒ ${doc.title}: ${err.message}`); } } } // 2. Chapters console.log("\nšŸ“– Chapters:"); const chapters = await getAllEnDocuments("/chapters"); for (const ch of chapters) { try { await createEsLocalization("/chapters", ch.documentId, { title: ch.title, content: ch.content, order: ch.order, }); console.log(` āœ… ${ch.title}`); } catch (err) { if (err.message.includes("already exists")) { console.log(` āš ļø Already has es: ${ch.title}`); } else { console.log(` āŒ ${ch.title}: ${err.message}`); } } } console.log("\n✨ Done!"); } main().catch((err) => { console.error("šŸ’„ Fatal error:", err.message); process.exit(1); });