|
| 1 | +/** |
| 2 | + * Script to find missing translations in locale files |
| 3 | + * |
| 4 | + * Usage: |
| 5 | + * node scripts/find-missing-translations.js [options] |
| 6 | + * |
| 7 | + * Options: |
| 8 | + * --locale=<locale> Only check a specific locale (e.g. --locale=fr) |
| 9 | + * --file=<file> Only check a specific file (e.g. --file=chat.json) |
| 10 | + * --help Show this help message |
| 11 | + */ |
| 12 | + |
| 13 | +const fs = require("fs") |
| 14 | +const path = require("path") |
| 15 | + |
| 16 | +// Process command line arguments |
| 17 | +const args = process.argv.slice(2).reduce((acc, arg) => { |
| 18 | + if (arg === "--help") { |
| 19 | + acc.help = true |
| 20 | + } else if (arg.startsWith("--locale=")) { |
| 21 | + acc.locale = arg.split("=")[1] |
| 22 | + } else if (arg.startsWith("--file=")) { |
| 23 | + acc.file = arg.split("=")[1] |
| 24 | + } |
| 25 | + return acc |
| 26 | +}, {}) |
| 27 | + |
| 28 | +// Show help if requested |
| 29 | +if (args.help) { |
| 30 | + console.log(` |
| 31 | +Find Missing Translations |
| 32 | +
|
| 33 | +A utility script to identify missing translations across locale files. |
| 34 | +Compares non-English locale files to the English ones to find any missing keys. |
| 35 | +
|
| 36 | +Usage: |
| 37 | + node scripts/find-missing-translations.js [options] |
| 38 | +
|
| 39 | +Options: |
| 40 | + --locale=<locale> Only check a specific locale (e.g. --locale=fr) |
| 41 | + --file=<file> Only check a specific file (e.g. --file=chat.json) |
| 42 | + --help Show this help message |
| 43 | +
|
| 44 | +Output: |
| 45 | + - Generates a report of missing translations |
| 46 | + `) |
| 47 | + process.exit(0) |
| 48 | +} |
| 49 | + |
| 50 | +// Path to the locales directory |
| 51 | +const LOCALES_DIR = path.join(__dirname, "../webview-ui/src/i18n/locales") |
| 52 | + |
| 53 | +// Recursively find all keys in an object |
| 54 | +function findKeys(obj, parentKey = "") { |
| 55 | + let keys = [] |
| 56 | + |
| 57 | + for (const [key, value] of Object.entries(obj)) { |
| 58 | + const currentKey = parentKey ? `${parentKey}.${key}` : key |
| 59 | + |
| 60 | + if (typeof value === "object" && value !== null) { |
| 61 | + // If value is an object, recurse |
| 62 | + keys = [...keys, ...findKeys(value, currentKey)] |
| 63 | + } else { |
| 64 | + // If value is a primitive, add the key |
| 65 | + keys.push(currentKey) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return keys |
| 70 | +} |
| 71 | + |
| 72 | +// Get value at a dotted path in an object |
| 73 | +function getValueAtPath(obj, path) { |
| 74 | + const parts = path.split(".") |
| 75 | + let current = obj |
| 76 | + |
| 77 | + for (const part of parts) { |
| 78 | + if (current === undefined || current === null) { |
| 79 | + return undefined |
| 80 | + } |
| 81 | + current = current[part] |
| 82 | + } |
| 83 | + |
| 84 | + return current |
| 85 | +} |
| 86 | + |
| 87 | +// Main function to find missing translations |
| 88 | +function findMissingTranslations() { |
| 89 | + try { |
| 90 | + // Get all locale directories (or filter to the specified locale) |
| 91 | + const allLocales = fs.readdirSync(LOCALES_DIR).filter((item) => { |
| 92 | + const stats = fs.statSync(path.join(LOCALES_DIR, item)) |
| 93 | + return stats.isDirectory() && item !== "en" // Exclude English as it's our source |
| 94 | + }) |
| 95 | + |
| 96 | + // Filter to the specified locale if provided |
| 97 | + const locales = args.locale ? allLocales.filter((locale) => locale === args.locale) : allLocales |
| 98 | + |
| 99 | + if (args.locale && locales.length === 0) { |
| 100 | + console.error(`Error: Locale '${args.locale}' not found in ${LOCALES_DIR}`) |
| 101 | + process.exit(1) |
| 102 | + } |
| 103 | + |
| 104 | + console.log(`Checking ${locales.length} non-English locale(s): ${locales.join(", ")}`) |
| 105 | + |
| 106 | + // Get all English JSON files |
| 107 | + const englishDir = path.join(LOCALES_DIR, "en") |
| 108 | + let englishFiles = fs.readdirSync(englishDir).filter((file) => file.endsWith(".json") && !file.startsWith(".")) |
| 109 | + |
| 110 | + // Filter to the specified file if provided |
| 111 | + if (args.file) { |
| 112 | + if (!englishFiles.includes(args.file)) { |
| 113 | + console.error(`Error: File '${args.file}' not found in ${englishDir}`) |
| 114 | + process.exit(1) |
| 115 | + } |
| 116 | + englishFiles = englishFiles.filter((file) => file === args.file) |
| 117 | + } |
| 118 | + |
| 119 | + // Load file contents |
| 120 | + const englishFileContents = englishFiles.map((file) => ({ |
| 121 | + name: file, |
| 122 | + content: JSON.parse(fs.readFileSync(path.join(englishDir, file), "utf8")), |
| 123 | + })) |
| 124 | + |
| 125 | + console.log( |
| 126 | + `Checking ${englishFileContents.length} translation file(s): ${englishFileContents.map((f) => f.name).join(", ")}`, |
| 127 | + ) |
| 128 | + |
| 129 | + // Results object to store missing translations |
| 130 | + const missingTranslations = {} |
| 131 | + |
| 132 | + // For each locale, check for missing translations |
| 133 | + for (const locale of locales) { |
| 134 | + missingTranslations[locale] = {} |
| 135 | + |
| 136 | + for (const { name, content: englishContent } of englishFileContents) { |
| 137 | + const localeFilePath = path.join(LOCALES_DIR, locale, name) |
| 138 | + |
| 139 | + // Check if the file exists in the locale |
| 140 | + if (!fs.existsSync(localeFilePath)) { |
| 141 | + missingTranslations[locale][name] = { file: "File is missing entirely" } |
| 142 | + continue |
| 143 | + } |
| 144 | + |
| 145 | + // Load the locale file |
| 146 | + const localeContent = JSON.parse(fs.readFileSync(localeFilePath, "utf8")) |
| 147 | + |
| 148 | + // Find all keys in the English file |
| 149 | + const englishKeys = findKeys(englishContent) |
| 150 | + |
| 151 | + // Check for missing keys in the locale file |
| 152 | + const missingKeys = [] |
| 153 | + |
| 154 | + for (const key of englishKeys) { |
| 155 | + const englishValue = getValueAtPath(englishContent, key) |
| 156 | + const localeValue = getValueAtPath(localeContent, key) |
| 157 | + |
| 158 | + if (localeValue === undefined) { |
| 159 | + missingKeys.push({ |
| 160 | + key, |
| 161 | + englishValue, |
| 162 | + }) |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if (missingKeys.length > 0) { |
| 167 | + missingTranslations[locale][name] = missingKeys |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + // Output results |
| 173 | + let hasMissingTranslations = false |
| 174 | + |
| 175 | + console.log("\nMissing Translations Report:\n") |
| 176 | + |
| 177 | + for (const [locale, files] of Object.entries(missingTranslations)) { |
| 178 | + if (Object.keys(files).length === 0) { |
| 179 | + console.log(`✅ ${locale}: No missing translations`) |
| 180 | + continue |
| 181 | + } |
| 182 | + |
| 183 | + hasMissingTranslations = true |
| 184 | + console.log(`📝 ${locale}:`) |
| 185 | + |
| 186 | + for (const [fileName, missingItems] of Object.entries(files)) { |
| 187 | + if (missingItems.file) { |
| 188 | + console.log(` - ${fileName}: ${missingItems.file}`) |
| 189 | + continue |
| 190 | + } |
| 191 | + |
| 192 | + console.log(` - ${fileName}: ${missingItems.length} missing translations`) |
| 193 | + |
| 194 | + for (const { key, englishValue } of missingItems) { |
| 195 | + console.log(` ${key}: "${englishValue}"`) |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + console.log("") |
| 200 | + } |
| 201 | + |
| 202 | + if (!hasMissingTranslations) { |
| 203 | + console.log("\n✅ All translations are complete!") |
| 204 | + } else { |
| 205 | + console.log("✏️ To add missing translations:") |
| 206 | + console.log("1. Add the missing keys to the corresponding locale files") |
| 207 | + console.log("2. Translate the English values to the appropriate language") |
| 208 | + console.log("3. Run this script again to verify all translations are complete") |
| 209 | + } |
| 210 | + } catch (error) { |
| 211 | + console.error("Error:", error.message) |
| 212 | + console.error(error.stack) |
| 213 | + process.exit(1) |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +// Run the main function |
| 218 | +findMissingTranslations() |
0 commit comments