const fs = require('fs'); const path = require('path'); const { execSync } = require('child_process'); const BASE = path.join(__dirname, '..', 'src', 'views', 'medicationmanagement'); const CWD = path.join(__dirname, '..', '..'); function walkDir(dir) { const files = []; const entries = fs.readdirSync(dir, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(dir, entry.name); if (entry.isDirectory()) files.push(...walkDir(fullPath)); else if (entry.name.endsWith('.vue')) files.push(fullPath); } return files; } function getOriginalContent(filePath) { try { const relPath = path.relative(CWD, filePath).replace(/\\/g, '/'); return execSync(`git show HEAD:"${relPath}"`, { encoding: 'utf8', cwd: CWD }); } catch (e) { return null; } } const vueFiles = walkDir(BASE); const translations = {}; for (const file of vueFiles) { const content = fs.readFileSync(file, 'utf8'); const original = getOriginalContent(file); if (!original) continue; const currentLines = content.split('\n'); const originalLines = original.split('\n'); for (let i = 0; i < currentLines.length; i++) { const line = currentLines[i]; // Find t('key') calls in this line const tMatch = line.match(/t\('(medication\.[^']+)'\)/); if (!tMatch) continue; const key = tMatch[1]; if (translations[key]) continue; // Already mapped // Look at the same line in the original file if (i < originalLines.length) { const origLine = originalLines[i]; // Find Chinese text in the original line const chineseMatch = origLine.match(/"([^"]*[\u4e00-\u9fff][^"]*)"/); if (chineseMatch) { translations[key] = chineseMatch[1]; } } } } console.log(`Extracted ${Object.keys(translations).length} translation keys`); // Write flat keys const flatPath = path.join(__dirname, '..', 'src', 'i18n', 'locales', 'medication_keys_flat.json'); fs.writeFileSync(flatPath, JSON.stringify(translations, null, 2), 'utf8'); // Write structured keys const outputPath = path.join(__dirname, '..', 'src', 'i18n', 'locales', 'medication_keys.json'); const output = {}; for (const [key, value] of Object.entries(translations)) { const parts = key.split('.'); let current = output; for (let i = 0; i < parts.length - 1; i++) { if (!current[parts[i]]) current[parts[i]] = {}; current = current[parts[i]]; } current[parts[parts.length - 1]] = value; } fs.writeFileSync(outputPath, JSON.stringify(output, null, 2), 'utf8'); console.log(`Wrote ${flatPath}`); console.log(`Wrote ${outputPath}`);