|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Script to analyze duplicate keys and values in translation files |
| 4 | + * Scans all JSON files in translations/en/ directory and reports: |
| 5 | + * - Duplicate keys (same key path in multiple files) |
| 6 | + * - Duplicate values (same value used for different keys) |
| 7 | + */ |
| 8 | + |
| 9 | +import fs from 'node:fs' |
| 10 | +import path from 'node:path' |
| 11 | +import { fileURLToPath } from 'node:url' |
| 12 | + |
| 13 | +const __filename = fileURLToPath(import.meta.url) |
| 14 | +const __dirname = path.dirname(__filename) |
| 15 | +const ROOT_DIR = path.resolve(__dirname, '../..') |
| 16 | +const TRANSLATIONS_DIR = path.join(ROOT_DIR, 'translations', 'en') |
| 17 | + |
| 18 | +/** |
| 19 | + * Flatten a nested object into dot-notation keys |
| 20 | + * @param {object} obj - The object to flatten |
| 21 | + * @param {string} prefix - The prefix for keys |
| 22 | + * @returns {object} - Flattened object with dot-notation keys |
| 23 | + */ |
| 24 | +function flattenObject(obj, prefix = '') { |
| 25 | + const flattened = {} |
| 26 | + |
| 27 | + for (const [key, value] of Object.entries(obj)) { |
| 28 | + const newKey = prefix ? `${prefix}.${key}` : key |
| 29 | + |
| 30 | + if (value !== null && typeof value === 'object' && !Array.isArray(value)) { |
| 31 | + // Recursively flatten nested objects |
| 32 | + Object.assign(flattened, flattenObject(value, newKey)) |
| 33 | + } else { |
| 34 | + // Store the value |
| 35 | + flattened[newKey] = value |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return flattened |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Read and parse a JSON file |
| 44 | + * @param {string} filePath - Path to the JSON file |
| 45 | + * @returns {object|null} - Parsed JSON object or null if error |
| 46 | + */ |
| 47 | +function readJsonFile(filePath) { |
| 48 | + try { |
| 49 | + const content = fs.readFileSync(filePath, 'utf8') |
| 50 | + return JSON.parse(content) |
| 51 | + } catch (error) { |
| 52 | + console.warn(`Warning: Failed to read ${filePath}: ${error.message}`) |
| 53 | + return null |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Get all JSON files recursively from a directory |
| 59 | + * @param {string} dir - Directory to search |
| 60 | + * @param {string} baseDir - Base directory for relative paths |
| 61 | + * @returns {Array<{filePath: string, relativePath: string}>} - Array of file info |
| 62 | + */ |
| 63 | +function getAllJsonFiles(dir, baseDir = dir) { |
| 64 | + const files = [] |
| 65 | + |
| 66 | + if (!fs.existsSync(dir)) { |
| 67 | + return files |
| 68 | + } |
| 69 | + |
| 70 | + const entries = fs.readdirSync(dir, { withFileTypes: true }) |
| 71 | + |
| 72 | + for (const entry of entries) { |
| 73 | + const fullPath = path.join(dir, entry.name) |
| 74 | + const relativePath = path.relative(baseDir, fullPath) |
| 75 | + |
| 76 | + if (entry.isDirectory()) { |
| 77 | + // Recursively search subdirectories |
| 78 | + files.push(...getAllJsonFiles(fullPath, baseDir)) |
| 79 | + } else if (entry.isFile() && entry.name.endsWith('.json')) { |
| 80 | + files.push({ |
| 81 | + filePath: fullPath, |
| 82 | + relativePath: relativePath, |
| 83 | + }) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return files |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Analyze all translation files |
| 92 | + * @returns {object} - Analysis results |
| 93 | + */ |
| 94 | +function analyzeTranslations() { |
| 95 | + const files = getAllJsonFiles(TRANSLATIONS_DIR) |
| 96 | + const keyMap = new Map() // key -> Array of {file, fullKey} |
| 97 | + const valueMap = new Map() // value -> Array of {file, fullKey} |
| 98 | + |
| 99 | + console.log(`Scanning ${files.length} translation files...\n`) |
| 100 | + |
| 101 | + // Process each file |
| 102 | + for (const { filePath, relativePath } of files) { |
| 103 | + const data = readJsonFile(filePath) |
| 104 | + if (!data) continue |
| 105 | + |
| 106 | + const flattened = flattenObject(data) |
| 107 | + |
| 108 | + // Track keys |
| 109 | + for (const [fullKey, value] of Object.entries(flattened)) { |
| 110 | + // Track duplicate keys |
| 111 | + if (!keyMap.has(fullKey)) { |
| 112 | + keyMap.set(fullKey, []) |
| 113 | + } |
| 114 | + keyMap.get(fullKey).push({ file: relativePath, fullKey }) |
| 115 | + |
| 116 | + // Track duplicate values (only for string values) |
| 117 | + if (typeof value === 'string') { |
| 118 | + if (!valueMap.has(value)) { |
| 119 | + valueMap.set(value, []) |
| 120 | + } |
| 121 | + valueMap.get(value).push({ file: relativePath, fullKey }) |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Find duplicate keys (keys that appear in multiple files) |
| 127 | + const duplicateKeys = [] |
| 128 | + for (const [key, locations] of keyMap.entries()) { |
| 129 | + if (locations.length > 1) { |
| 130 | + duplicateKeys.push({ key, locations }) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Find duplicate values (values used by multiple keys) |
| 135 | + const duplicateValues = [] |
| 136 | + for (const [value, locations] of valueMap.entries()) { |
| 137 | + if (locations.length > 1) { |
| 138 | + duplicateValues.push({ value, locations }) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return { |
| 143 | + totalFiles: files.length, |
| 144 | + totalKeys: keyMap.size, |
| 145 | + duplicateKeys, |
| 146 | + duplicateValues, |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * Generate and print report |
| 152 | + */ |
| 153 | +function printReport(results) { |
| 154 | + const { totalFiles, totalKeys, duplicateKeys, duplicateValues } = results |
| 155 | + |
| 156 | + console.log('='.repeat(80)) |
| 157 | + console.log('TRANSLATION DUPLICATE ANALYSIS REPORT') |
| 158 | + console.log('='.repeat(80)) |
| 159 | + console.log() |
| 160 | + |
| 161 | + // Summary |
| 162 | + console.log('SUMMARY') |
| 163 | + console.log('-'.repeat(80)) |
| 164 | + console.log(`Total files scanned: ${totalFiles}`) |
| 165 | + console.log(`Total unique keys: ${totalKeys}`) |
| 166 | + console.log(`Duplicate keys (same key in multiple files): ${duplicateKeys.length}`) |
| 167 | + console.log(`Duplicate values (same value for different keys): ${duplicateValues.length}`) |
| 168 | + console.log() |
| 169 | + |
| 170 | + // Duplicate keys report |
| 171 | + if (duplicateKeys.length > 0) { |
| 172 | + console.log('='.repeat(80)) |
| 173 | + console.log('DUPLICATE KEYS') |
| 174 | + console.log('='.repeat(80)) |
| 175 | + console.log('The following keys appear in multiple files:') |
| 176 | + console.log() |
| 177 | + |
| 178 | + // Sort by key name for better readability |
| 179 | + duplicateKeys.sort((a, b) => a.key.localeCompare(b.key)) |
| 180 | + |
| 181 | + for (const { key, locations } of duplicateKeys) { |
| 182 | + console.log(`Key: "${key}"`) |
| 183 | + console.log(` Found in ${locations.length} file(s):`) |
| 184 | + for (const { file } of locations) { |
| 185 | + console.log(` - ${file}`) |
| 186 | + } |
| 187 | + console.log() |
| 188 | + } |
| 189 | + } else { |
| 190 | + console.log('='.repeat(80)) |
| 191 | + console.log('DUPLICATE KEYS') |
| 192 | + console.log('='.repeat(80)) |
| 193 | + console.log('✓ No duplicate keys found (each key appears in only one file)') |
| 194 | + console.log() |
| 195 | + } |
| 196 | + |
| 197 | + // Duplicate values report |
| 198 | + if (duplicateValues.length > 0) { |
| 199 | + console.log('='.repeat(80)) |
| 200 | + console.log('DUPLICATE VALUES') |
| 201 | + console.log('='.repeat(80)) |
| 202 | + console.log('The following values are used by multiple keys:') |
| 203 | + console.log() |
| 204 | + |
| 205 | + // Sort by number of occurrences (descending) for better readability |
| 206 | + duplicateValues.sort((a, b) => b.locations.length - a.locations.length) |
| 207 | + |
| 208 | + for (const { value, locations } of duplicateValues) { |
| 209 | + // Truncate long values for display |
| 210 | + const displayValue = value.length > 60 ? `${value.substring(0, 60)}...` : value |
| 211 | + console.log(`Value: "${displayValue}"`) |
| 212 | + console.log(` Used by ${locations.length} key(s):`) |
| 213 | + for (const { file, fullKey } of locations) { |
| 214 | + console.log(` - ${file} -> "${fullKey}"`) |
| 215 | + } |
| 216 | + console.log() |
| 217 | + } |
| 218 | + } else { |
| 219 | + console.log('='.repeat(80)) |
| 220 | + console.log('DUPLICATE VALUES') |
| 221 | + console.log('='.repeat(80)) |
| 222 | + console.log('✓ No duplicate values found (each value is unique)') |
| 223 | + console.log() |
| 224 | + } |
| 225 | + |
| 226 | + console.log('='.repeat(80)) |
| 227 | + console.log('END OF REPORT') |
| 228 | + console.log('='.repeat(80)) |
| 229 | +} |
| 230 | + |
| 231 | +/** |
| 232 | + * Main function |
| 233 | + */ |
| 234 | +function main() { |
| 235 | + if (!fs.existsSync(TRANSLATIONS_DIR)) { |
| 236 | + console.error(`Error: Translations directory not found: ${TRANSLATIONS_DIR}`) |
| 237 | + process.exit(1) |
| 238 | + } |
| 239 | + |
| 240 | + const results = analyzeTranslations() |
| 241 | + printReport(results) |
| 242 | + |
| 243 | + // Exit with non-zero code if duplicates found |
| 244 | + if (results.duplicateKeys.length > 0 || results.duplicateValues.length > 0) { |
| 245 | + process.exit(1) |
| 246 | + } |
| 247 | +} |
| 248 | + |
| 249 | +// Run the script |
| 250 | +main() |
0 commit comments