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