sync translations for 2FA and misc messages/labels
This commit is contained in:
1 parent
65e810ec79
commit
0dcba7d388
52 files changed
+3795
-106
No files matched your search
@@ -1,12 +1,22 @@
|
||||
import { readdirSync, readFileSync } from 'node:fs';
|
||||
import { readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
const messagesDir = join(__dirname, '..', 'public', 'intl', 'messages');
|
||||
const sourcePath = join(messagesDir, 'en-US.json');
|
||||
const removeExtraKeys = process.argv.includes('--remove-extra-keys');
|
||||
const help = process.argv.includes('--help') || process.argv.includes('-h');
|
||||
|
||||
const en = JSON.parse(readFileSync(join(messagesDir, 'en-US.json'), 'utf8'));
|
||||
if (help) {
|
||||
console.log('Usage: node scripts/check-missing-messages.js [--remove-extra-keys]');
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const enRaw = readFileSync(sourcePath, 'utf8');
|
||||
const newline = enRaw.includes('\r\n') ? '\r\n' : '\n';
|
||||
const en = JSON.parse(enRaw);
|
||||
|
||||
// Flatten nested structure: { label: { foo: 'bar' } } -> { 'label.foo': 'bar' }
|
||||
function flatten(obj, prefix = '') {
|
||||
@@ -22,8 +32,33 @@ function flatten(obj, prefix = '') {
|
||||
return result;
|
||||
}
|
||||
|
||||
function setNestedValue(target, flatKey, value) {
|
||||
const parts = flatKey.split('.');
|
||||
let current = target;
|
||||
|
||||
for (let index = 0; index < parts.length - 1; index++) {
|
||||
current[parts[index]] ??= {};
|
||||
current = current[parts[index]];
|
||||
}
|
||||
|
||||
current[parts.at(-1)] = value;
|
||||
}
|
||||
|
||||
function buildNormalizedObject(flat) {
|
||||
const ordered = {};
|
||||
|
||||
for (const key of enKeys) {
|
||||
if (key in flat) {
|
||||
setNestedValue(ordered, key, flat[key]);
|
||||
}
|
||||
}
|
||||
|
||||
return ordered;
|
||||
}
|
||||
|
||||
const enFlat = flatten(en);
|
||||
const enKeys = Object.keys(enFlat);
|
||||
const enKeySet = new Set(enKeys);
|
||||
console.log(`en-US.json has ${enKeys.length} keys`);
|
||||
|
||||
const files = readdirSync(messagesDir)
|
||||
@@ -31,19 +66,47 @@ const files = readdirSync(messagesDir)
|
||||
.sort();
|
||||
|
||||
const allMissing = {};
|
||||
let total = 0;
|
||||
const allExtra = {};
|
||||
let totalMissing = 0;
|
||||
let totalExtra = 0;
|
||||
let updatedFiles = 0;
|
||||
|
||||
for (const fname of files) {
|
||||
const data = JSON.parse(readFileSync(join(messagesDir, fname), 'utf8'));
|
||||
const filePath = join(messagesDir, fname);
|
||||
const raw = readFileSync(filePath, 'utf8');
|
||||
const data = JSON.parse(raw);
|
||||
const flat = flatten(data);
|
||||
const missing = enKeys.filter(k => !(k in flat));
|
||||
const extra = Object.keys(flat).filter(k => !enKeySet.has(k)).sort();
|
||||
|
||||
if (missing.length) {
|
||||
allMissing[fname] = missing;
|
||||
console.log(`${fname}: ${missing.length} missing`);
|
||||
total += missing.length;
|
||||
totalMissing += missing.length;
|
||||
}
|
||||
|
||||
if (extra.length) {
|
||||
allExtra[fname] = extra;
|
||||
console.log(`${fname}: ${extra.length} extra`);
|
||||
totalExtra += extra.length;
|
||||
}
|
||||
|
||||
if (removeExtraKeys && extra.length) {
|
||||
const normalized = buildNormalizedObject(flat);
|
||||
const nextRaw = `${JSON.stringify(normalized, null, 2).replace(/\n/g, newline)}${newline}`;
|
||||
|
||||
if (nextRaw !== raw) {
|
||||
writeFileSync(filePath, nextRaw, 'utf8');
|
||||
updatedFiles++;
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(`\nTotal missing across all locales: ${total}`);
|
||||
console.log(`\nTotal missing across all locales: ${totalMissing}`);
|
||||
console.log(`Total extra across all locales: ${totalExtra}`);
|
||||
|
||||
if (removeExtraKeys) {
|
||||
console.log(`Files updated to remove extra keys: ${updatedFiles}`);
|
||||
}
|
||||
|
||||
const keyCounts = {};
|
||||
for (const missing of Object.values(allMissing)) {
|
||||
@@ -58,3 +121,17 @@ if (sorted.length) {
|
||||
console.log(` "${k}": missing from ${count} files (en value: "${enFlat[k]}")`);
|
||||
}
|
||||
}
|
||||
|
||||
const extraKeyCounts = {};
|
||||
for (const extra of Object.values(allExtra)) {
|
||||
for (const k of extra) {
|
||||
extraKeyCounts[k] = (extraKeyCounts[k] || 0) + 1;
|
||||
}
|
||||
}
|
||||
const sortedExtra = Object.entries(extraKeyCounts).sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]));
|
||||
if (sortedExtra.length) {
|
||||
console.log('\nMost commonly extra keys:');
|
||||
for (const [k, count] of sortedExtra) {
|
||||
console.log(` "${k}": extra in ${count} files`);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user