import fs from 'node:fs'; import path, { basename } from 'node:path'; import process from 'node:process'; import yaml from 'yaml'; const toolsDir = 'src/tools'; const localesFile = 'locales/en.yml'; // Load existing locales file or initialize an empty object let localesData = {}; if (fs.existsSync(localesFile)) { localesData = yaml.parse(fs.readFileSync(localesFile, 'utf-8')) || {}; } // Function to process Vue components in a directory function processVueComponents(toolDir, toolName) { fs.readdirSync(toolDir) .filter(file => file.endsWith('.vue')) .forEach(file => processVueComponent(path.join(toolDir, file), toolName)); } // Function to process a Vue component file function processVueComponent(filePath, toolName) { console.log(`Processing ${filePath} in ${toolName}`); let content = fs.readFileSync(filePath, 'utf8'); const origContent = content; // Update locales file with extracted name and description if (!localesData.tools) { localesData.tools = {}; } if (!localesData.tools[toolName]) { localesData.tools[toolName] = {}; } // Regex to find label or placeholder attributes let regex = /\b(label|text|message):\s*'((?:[^']|\\')+)'/g; content = content.replace(regex, (match, attr, text) => { if (!text?.trim()) { return match; } const key = (`${attr}-${text.replace(/[\p{P}\p{S}\s]+/gu, '-').replace(/^-+|-+$/g, '').replace(/-+/g, '-')}`).toLowerCase(); if (!localesData.tools[toolName].texts) { localesData.tools[toolName].texts = {}; } localesData.tools[toolName].texts[key] = text; return `${attr}: t('tools.${toolName}.texts.${key}')`; }); if (content.includes('/* NO EXTRACT SCRIPT */')) { console.log(`Marked to not extract: ${filePath}`); return; } const hasAlreayI18n = filePath.endsWith('.vue') ? /const\s+\{.*?\bt\b.*?\}\s+=\s+useI18n\(.*?\)/s.test(content) : /import\s+\{.*?\btranslate\b.*?\}\s+from\s+(['"])@\/plugins\/i18n\.plugin\1/s.test(content); if (hasAlreayI18n) { console.log(`Already extracted: ${filePath}`); return; } if (filePath.endsWith('.vue')) { // Regex to find label or placeholder attributes regex = /(? { if (!text?.trim()) { return match; } const key = (`${attr}-${text.replace(/[\p{P}\p{S}\s]+/gu, '-').replace(/^-+|-+$/g, '').replace(/-+/g, '-')}`).toLowerCase(); if (!localesData.tools[toolName].texts) { localesData.tools[toolName].texts = {}; } localesData.tools[toolName].texts[key] = text; return `:${attr}="t('tools.${toolName}.texts.${key}')"`; }); // Regex to find text inside HTML tags const textInsideTagsRegex = /(?(?:\n\s*)?([^\<\>\n]+)(?:\n\s*)?\ { if (!text?.trim()) { return match; } if (text.match(/[\}\{\|\[\<\>\]]/)) { return match; } const key = (`tag-${text.trim().replace(/[\p{P}\p{S}\s]+/gu, '-').replace(/^-+|-+$/g, '').replace(/-+/g, '-')}`).toLowerCase(); if (!localesData.tools[toolName].texts) { localesData.tools[toolName].texts = {}; } localesData.tools[toolName].texts[key] = text.trim(); return `>{{ t('tools.${toolName}.texts.${key}') }}<`; }); } if (origContent === content) { console.log(`Nothing to extract in ${filePath}`); return; } if (filePath.endsWith('.vue')) { // Ensure the useI18n import exists if (!hasAlreayI18n) { content = content.replace(/\