|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Validates locale key parity, placeholder consistency, and source key coverage. |
| 4 | + * Usage: node scripts/i18n-check.mjs |
| 5 | + */ |
| 6 | +import path from 'node:path'; |
| 7 | +import { |
| 8 | + LOCALES_DIR, |
| 9 | + listLocaleFiles, |
| 10 | + readJson, |
| 11 | + flattenMessages, |
| 12 | + collectPlaceholders, |
| 13 | + loadGuardrailsConfig, |
| 14 | + extractKeysFromSource, |
| 15 | + keyIsIgnored |
| 16 | +} from './i18n-common.mjs'; |
| 17 | + |
| 18 | +function formatList(values, max = 12) { |
| 19 | + if (values.length === 0) return ''; |
| 20 | + const shown = values.slice(0, max); |
| 21 | + const lines = shown.map((value) => ` - ${value}`); |
| 22 | + if (values.length > max) { |
| 23 | + lines.push(` - ... and ${values.length - max} more`); |
| 24 | + } |
| 25 | + return lines.join('\n'); |
| 26 | +} |
| 27 | + |
| 28 | +function sameStringArray(left, right) { |
| 29 | + if (left.length !== right.length) return false; |
| 30 | + return left.every((value, index) => value === right[index]); |
| 31 | +} |
| 32 | + |
| 33 | +async function main() { |
| 34 | + const config = await loadGuardrailsConfig(); |
| 35 | + const sourceLocaleFile = `${config.sourceLocale}.json`; |
| 36 | + const sourceLocalePath = path.join(LOCALES_DIR, sourceLocaleFile); |
| 37 | + const localeFiles = await listLocaleFiles(); |
| 38 | + |
| 39 | + if (!localeFiles.includes(sourceLocaleFile)) { |
| 40 | + console.error(`Source locale file not found: ${sourceLocalePath}`); |
| 41 | + process.exit(1); |
| 42 | + } |
| 43 | + |
| 44 | + const sourceLocale = await readJson(sourceLocalePath); |
| 45 | + const sourceFlat = flattenMessages(sourceLocale); |
| 46 | + const sourceKeys = Object.keys(sourceFlat).sort(); |
| 47 | + |
| 48 | + const extracted = await extractKeysFromSource(sourceKeys); |
| 49 | + const usedKeySet = new Set([ |
| 50 | + ...extracted.staticKeys, |
| 51 | + ...extracted.templateResolvedKeys, |
| 52 | + ...extracted.literalReferencedKeys |
| 53 | + ]); |
| 54 | + |
| 55 | + const errors = []; |
| 56 | + const warnings = []; |
| 57 | + |
| 58 | + const usedButMissingInSource = extracted.staticKeys.filter((key) => !sourceKeys.includes(key)).sort(); |
| 59 | + if (usedButMissingInSource.length > 0) { |
| 60 | + errors.push( |
| 61 | + [ |
| 62 | + 'Keys used in source code but missing in source locale:', |
| 63 | + formatList(usedButMissingInSource, 50) |
| 64 | + ].join('\n') |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + for (const localeFile of localeFiles) { |
| 69 | + if (localeFile === sourceLocaleFile) continue; |
| 70 | + const localeCode = localeFile.replace(/\.json$/, ''); |
| 71 | + const localePath = path.join(LOCALES_DIR, localeFile); |
| 72 | + const localeObject = await readJson(localePath); |
| 73 | + const localeFlat = flattenMessages(localeObject); |
| 74 | + const localeKeys = Object.keys(localeFlat).sort(); |
| 75 | + |
| 76 | + const missingKeys = sourceKeys.filter((key) => !(key in localeFlat)); |
| 77 | + const extraKeys = localeKeys.filter((key) => !(key in sourceFlat)); |
| 78 | + |
| 79 | + if (missingKeys.length > 0) { |
| 80 | + errors.push( |
| 81 | + [`[${localeCode}] Missing keys (${missingKeys.length}):`, formatList(missingKeys, 25)].join('\n') |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + if (extraKeys.length > 0) { |
| 86 | + errors.push( |
| 87 | + [`[${localeCode}] Extra keys (${extraKeys.length}):`, formatList(extraKeys, 25)].join('\n') |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + const placeholderMismatches = []; |
| 92 | + for (const key of sourceKeys) { |
| 93 | + const sourceValue = sourceFlat[key]; |
| 94 | + const localeValue = localeFlat[key]; |
| 95 | + |
| 96 | + if (typeof sourceValue !== 'string' || typeof localeValue !== 'string') continue; |
| 97 | + |
| 98 | + const sourcePlaceholders = collectPlaceholders(sourceValue); |
| 99 | + const localePlaceholders = collectPlaceholders(localeValue); |
| 100 | + |
| 101 | + if (!sameStringArray(sourcePlaceholders, localePlaceholders)) { |
| 102 | + placeholderMismatches.push( |
| 103 | + `${key} (source: ${JSON.stringify(sourcePlaceholders)}, locale: ${JSON.stringify(localePlaceholders)})` |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (placeholderMismatches.length > 0) { |
| 109 | + errors.push( |
| 110 | + [ |
| 111 | + `[${localeCode}] Placeholder mismatches (${placeholderMismatches.length}):`, |
| 112 | + formatList(placeholderMismatches, 15) |
| 113 | + ].join('\n') |
| 114 | + ); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + const staleSourceKeys = sourceKeys.filter( |
| 119 | + (key) => !usedKeySet.has(key) && !keyIsIgnored(key, config) |
| 120 | + ); |
| 121 | + |
| 122 | + if (staleSourceKeys.length > 0) { |
| 123 | + warnings.push( |
| 124 | + [ |
| 125 | + `Potentially stale source locale keys (${staleSourceKeys.length}):`, |
| 126 | + formatList(staleSourceKeys, 20), |
| 127 | + ' (These are warnings only due to dynamic key access patterns.)' |
| 128 | + ].join('\n') |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + if (extracted.variableExpressions.length > 0) { |
| 133 | + warnings.push( |
| 134 | + [ |
| 135 | + `Dynamic key expressions detected (${extracted.variableExpressions.length}):`, |
| 136 | + formatList(extracted.variableExpressions, 20), |
| 137 | + ' (Resolved via literal scans when possible.)' |
| 138 | + ].join('\n') |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + console.log(`Checked locales: ${localeFiles.length}`); |
| 143 | + console.log(`Source locale: ${config.sourceLocale}`); |
| 144 | + console.log(`Source files scanned: ${extracted.sourceFilesScanned}`); |
| 145 | + console.log(`Source locale keys: ${sourceKeys.length}`); |
| 146 | + console.log(`Used keys detected: ${usedKeySet.size}`); |
| 147 | + |
| 148 | + if (warnings.length > 0) { |
| 149 | + console.log('\nWarnings:'); |
| 150 | + for (const warning of warnings) { |
| 151 | + console.log(`\n${warning}`); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (errors.length > 0) { |
| 156 | + console.error('\nErrors:'); |
| 157 | + for (const error of errors) { |
| 158 | + console.error(`\n${error}`); |
| 159 | + } |
| 160 | + process.exit(1); |
| 161 | + } |
| 162 | + |
| 163 | + console.log('\nAll i18n guardrails passed.'); |
| 164 | +} |
| 165 | + |
| 166 | +await main(); |
0 commit comments