148 lines
4.3 KiB
JavaScript
148 lines
4.3 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// Function to extract key from t("key") and create a default value
|
|
function createDefaultValue(key) {
|
|
// Remove quotes and convert to readable text
|
|
const cleanKey = key.replace(/['"]/g, '');
|
|
|
|
// Convert camelCase or kebab-case to readable text
|
|
const readable = cleanKey
|
|
.replace(/([A-Z])/g, ' $1') // Add space before capital letters
|
|
.replace(/-/g, ' ') // Replace hyphens with spaces
|
|
.replace(/_/g, ' ') // Replace underscores with spaces
|
|
.toLowerCase()
|
|
.trim();
|
|
|
|
// Capitalize first letter of each word
|
|
return readable
|
|
.split(' ')
|
|
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
|
|
.join(' ');
|
|
}
|
|
|
|
// Function to check if this is likely a translation function call
|
|
function isTranslationCall(line, match) {
|
|
// Look for patterns that indicate this is a translation function
|
|
const beforeMatch = line.substring(0, line.indexOf(match));
|
|
const afterMatch = line.substring(line.indexOf(match) + match.length);
|
|
|
|
// Check if it's preceded by common translation patterns
|
|
const translationPatterns = [
|
|
/const\s+t\s*=\s*useTranslations/,
|
|
/t\s*\(\s*["']/,
|
|
/{t\s*\(\s*["']/,
|
|
/t\s*\(\s*["']/,
|
|
/\bt\s*\(\s*["']/
|
|
];
|
|
|
|
return translationPatterns.some(pattern => pattern.test(beforeMatch + match));
|
|
}
|
|
|
|
// Function to process a single file
|
|
function processFile(filePath) {
|
|
try {
|
|
let content = fs.readFileSync(filePath, 'utf8');
|
|
let modified = false;
|
|
|
|
// Split content into lines to analyze context
|
|
const lines = content.split('\n');
|
|
const newLines = [];
|
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
let line = lines[i];
|
|
|
|
// Pattern to match t("key") or t('key') but not t("key", "default")
|
|
const pattern = /t\s*\(\s*["']([^"']+)["']\s*\)/g;
|
|
|
|
// Check if this line contains translation calls
|
|
if (line.includes('t(') && line.includes(')')) {
|
|
line = line.replace(pattern, (match, key) => {
|
|
// Skip if already has a second parameter (default value)
|
|
if (match.includes(',')) {
|
|
return match;
|
|
}
|
|
|
|
// Check if this is likely a translation call
|
|
if (isTranslationCall(line, match)) {
|
|
const defaultValue = createDefaultValue(key);
|
|
modified = true;
|
|
return `t("${key}", "${defaultValue}")`;
|
|
}
|
|
|
|
return match;
|
|
});
|
|
}
|
|
|
|
newLines.push(line);
|
|
}
|
|
|
|
if (modified) {
|
|
const newContent = newLines.join('\n');
|
|
fs.writeFileSync(filePath, newContent, 'utf8');
|
|
console.log(`✅ Updated: ${filePath}`);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
} catch (error) {
|
|
console.error(`❌ Error processing ${filePath}:`, error.message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Function to recursively find all TypeScript and TSX files
|
|
function findFiles(dir, extensions = ['.ts', '.tsx']) {
|
|
const files = [];
|
|
|
|
function traverse(currentDir) {
|
|
const items = fs.readdirSync(currentDir);
|
|
|
|
for (const item of items) {
|
|
const fullPath = path.join(currentDir, item);
|
|
const stat = fs.statSync(fullPath);
|
|
|
|
if (stat.isDirectory()) {
|
|
// Skip certain directories
|
|
if (!['node_modules', '.next', 'dist', 'build', 'scripts'].includes(item)) {
|
|
traverse(fullPath);
|
|
}
|
|
} else if (extensions.includes(path.extname(item)) && !item.endsWith('.d.ts')) {
|
|
files.push(fullPath);
|
|
}
|
|
}
|
|
}
|
|
|
|
traverse(dir);
|
|
return files;
|
|
}
|
|
|
|
// Main function to process all files
|
|
function main() {
|
|
console.log('🚀 Starting precise translation default value addition...\n');
|
|
|
|
const files = findFiles('.');
|
|
let processedCount = 0;
|
|
let totalFiles = files.length;
|
|
|
|
console.log(`Found ${totalFiles} TypeScript/TSX files to process...\n`);
|
|
|
|
files.forEach(file => {
|
|
if (processFile(file)) {
|
|
processedCount++;
|
|
}
|
|
});
|
|
|
|
console.log(`\n📊 Summary:`);
|
|
console.log(`Total files scanned: ${totalFiles}`);
|
|
console.log(`Files updated: ${processedCount}`);
|
|
console.log(`Files unchanged: ${totalFiles - processedCount}`);
|
|
console.log('\n✨ Precise translation default values addition completed!');
|
|
}
|
|
|
|
// Run the script
|
|
if (require.main === module) {
|
|
main();
|
|
}
|
|
|
|
module.exports = { processFile, createDefaultValue, findFiles };
|