|
| 1 | +/// @ts-check |
| 2 | + |
| 3 | +import { readdir, stat, readFile } from 'fs/promises'; |
| 4 | +import path, { dirname } from 'path'; |
| 5 | +import { fileURLToPath } from 'url'; |
| 6 | + |
| 7 | +const LOCALES_DIR = path.resolve(dirname(fileURLToPath(import.meta.url)), '..', '..', 'public', 'locales'); |
| 8 | + |
| 9 | +const locales = await readdir(LOCALES_DIR); |
| 10 | + |
| 11 | +/** |
| 12 | + * @type {Array<{ language: string, untranslatedCount: number, translatedCount: number }>} |
| 13 | + */ |
| 14 | +const stats = []; |
| 15 | + |
| 16 | +for (const fileName of locales) { |
| 17 | + const filePath = path.join(LOCALES_DIR, fileName, 'grafana.json'); |
| 18 | + if (!(await exists(filePath))) { |
| 19 | + continue; |
| 20 | + } |
| 21 | + |
| 22 | + const messages = await readFile(filePath); |
| 23 | + const parsedMessages = JSON.parse(messages.toString()); |
| 24 | + |
| 25 | + let translatedCount = 0; |
| 26 | + let untranslatedCount = 0; |
| 27 | + |
| 28 | + eachMessage(parsedMessages, (value) => { |
| 29 | + if (value === '') { |
| 30 | + untranslatedCount += 1; |
| 31 | + } else { |
| 32 | + translatedCount += 1; |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + stats.push({ |
| 37 | + language: fileName, |
| 38 | + translatedCount, |
| 39 | + untranslatedCount, |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +for (const stat of stats) { |
| 44 | + logStat(`untranslated.${stat.language}`, stat.untranslatedCount); |
| 45 | + logStat(`translated.${stat.language}`, stat.translatedCount); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * @param {string} filePath |
| 50 | + */ |
| 51 | +async function exists(filePath) { |
| 52 | + try { |
| 53 | + await stat(filePath); |
| 54 | + return true; |
| 55 | + } catch (err) { |
| 56 | + if (err.code === 'ENOENT' || err.code === 'ENOTDIR') { |
| 57 | + return false; |
| 58 | + } |
| 59 | + throw err; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * @param {unknown} value |
| 65 | + * @param {(v: string) => void} callback |
| 66 | + */ |
| 67 | +function eachMessage(value, callback) { |
| 68 | + if (typeof value === 'object') { |
| 69 | + for (const key in value) { |
| 70 | + const element = value[key]; |
| 71 | + eachMessage(element, callback); |
| 72 | + } |
| 73 | + } else if (typeof value === 'string') { |
| 74 | + callback(value); |
| 75 | + } else { |
| 76 | + throw new Error(`Unknown value ${value} in eachMessage`); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * @param {string} name |
| 82 | + * @param {string | number} value |
| 83 | + */ |
| 84 | +function logStat(name, value) { |
| 85 | + // Note that this output format must match the parsing in ci-frontend-metrics.sh |
| 86 | + // which expects the two values to be separated by a space |
| 87 | + console.log(`${name} ${value}`); |
| 88 | +} |
0 commit comments