117 lines
3.4 KiB
JavaScript
117 lines
3.4 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 process a single file
|
|
function processFile(filePath) {
|
|
try {
|
|
let content = fs.readFileSync(filePath, 'utf8');
|
|
let modified = false;
|
|
|
|
// Only process files that contain useTranslations
|
|
if (!content.includes('useTranslations')) {
|
|
return false;
|
|
}
|
|
|
|
// Pattern to match t("key") or t('key') but not t("key", { defaultValue: "..." })
|
|
// This pattern is more specific to avoid false positives
|
|
const pattern = /(\bt\s*\(\s*["'])([^"']+)(["']\s*\))/g;
|
|
|
|
content = content.replace(pattern, (match, prefix, key, suffix) => {
|
|
// Skip if already has a second parameter (default value)
|
|
if (match.includes(',')) {
|
|
return match;
|
|
}
|
|
|
|
const defaultValue = createDefaultValue(key);
|
|
modified = true;
|
|
return `${prefix}${key}${suffix.replace(')', `, { defaultValue: "${defaultValue}" })`)}`;
|
|
});
|
|
|
|
if (modified) {
|
|
fs.writeFileSync(filePath, content, '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 translation default value addition with object format...\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✨ Translation default values addition completed!');
|
|
}
|
|
|
|
// Run the script
|
|
if (require.main === module) {
|
|
main();
|
|
}
|
|
|
|
module.exports = { processFile, createDefaultValue, findFiles };
|