-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathi18n-checker.ts
169 lines (156 loc) · 5.43 KB
/
i18n-checker.ts
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { mkdtemp, readFile } from 'node:fs/promises';
import os from 'node:os';
import { glob } from 'glob';
import * as I18nextParser from 'i18next-parser';
import { jsonKeyPathList } from 'json-key-path-list';
import vfs from 'vinyl-fs';
const IGNORE_MISSING: RegExp[] = [
// key used by a t function in modules/trainschedule/components/ManageTrainSchedule/helpers/checkCurrentConfig.ts
/translation:errorMessages\..*/,
/translation:error/,
/translation:default/,
/translation:error/,
/translation:unspecified/,
// key used by upsertMapWaypointsInOperationalPoints
/translation:requestedPoint/,
// key used by checkStdcmConfigErrors
/translation:arriveAt/,
/translation:departureTime/,
/translation:destinationTime/,
/translation:leaveAt/,
/stdcm:simulation.results/,
/stdcm-help-section:asu/,
/stdcm-help-section:sections/,
/translation:remainingTrackConflicts/,
/translation:remainingWorkConflicts/,
/translation:trackConflict/,
/translation:trackConflictSameDay/,
/translation:workConflict/,
/translation:workConflictSameDay/,
];
const IGNORE_UNUSED: RegExp[] = [
/.*-generated$/,
/.*\.generated\..*$/,
/errors:.*/, // Errors are generated and used dynamicly
/infraEditor:.*/, // Translation of properties object for the form
/infraEditor:__main____.*/, // Found by error by i18n parser in a json-schema
/translation:Editor\.tools\..*/, // Editor tool's label are generated
/translation:Editor.obj-types\..*/, // Type of object are translated dynamicly on the sumpup popin
/translation:Editor.directions\..*/,
/translation:Editor.layers\..*/,
/Editor\.item-statuses\..*/,
/translation:Editor\.infra-errors\.error-type\..*/, // Infra error types are generated
/translation:Editor\.infra-errors\.error-level\..*/, // Infra error level are generated
/translation:Editor\.infra-errors\.corrector-modal\..*/,
/home\/navbar:language\..*/, // Language selector which is generated with the locale
];
/**
* Read a file and returns its content as a JSON
*/
async function readJsonFile<T extends { [key: string]: unknown }>(filePath: string): Promise<T> {
const data = await readFile(filePath, 'utf-8');
return JSON.parse(data);
}
/**
* Given a locales folder, return the list of all i18n keys.
*/
async function getLocalesKeys(localePath: string, locale: string): Promise<Set<string>> {
const pathForLocale = `${localePath}/${locale}/`;
const files = await glob(`${pathForLocale}/**/*.json`);
const allKeys = (
await Promise.all(
files.map(async (file) => {
const data = await readJsonFile(file);
const namespace = file.replace(pathForLocale, '').replace(/\.json$/, '');
return jsonKeyPathList(data).map(
(key) => `${namespace}:${key.replace(/_(zero|one|other|many)$/, '')}`
);
})
)
)
.flat()
.sort();
return new Set(allKeys);
}
/**
* Scan the source code and generate a locale folder structure
* in a temp folder, with all the i18n key found.
*
* @returns The location of the temp folder
*/
async function scanCode(): Promise<string> {
const tempDir = await mkdtemp(`${os.tmpdir()}/osrd-i18n-`);
return new Promise((resolve, reject) => {
const stream = vfs
.src(`${process.cwd()}/src/**/*.{ts,tsx}`)
.pipe(
// eslint-disable-next-line
new (I18nextParser as any).gulp({
locales: ['dev'],
output: '$LOCALE/$NAMESPACE.json',
})
)
.pipe(vfs.dest(tempDir));
stream.on('finish', () => resolve(tempDir));
stream.on('error', (e: Error) => reject(e));
});
}
/**
* The script execution
*/
async function run() {
try {
const keysByLocale = await Promise.all(
['fr', 'en'].map(async (locale) => {
const keys = await getLocalesKeys(`${process.cwd()}/public/locales`, locale);
return { locale, keys };
})
);
const scannedLocalePath = await scanCode();
const scannedNamespacedKeys = await getLocalesKeys(scannedLocalePath, 'dev');
// Search for unused keys
const unusedKeys: Array<{ locale: string; key: string }> = [];
keysByLocale.forEach(({ locale, keys }) => {
keys.forEach((key) => {
if (
!scannedNamespacedKeys.has(key) &&
IGNORE_UNUSED.every((pattern) => !key.match(pattern))
) {
unusedKeys.push({ locale, key });
}
});
});
// Search for missing traduction
const missingKeys: Array<{ locale: string; key: string }> = [];
scannedNamespacedKeys.forEach((key) => {
if (IGNORE_MISSING.every((pattern) => !key.match(pattern))) {
keysByLocale.forEach(({ locale, keys }) => {
if (!keys.has(key)) {
missingKeys.push({ locale, key });
}
});
}
});
/* eslint-disable no-console */
if (unusedKeys.length > 0) {
console.warn(`Unused keys (${unusedKeys.length})`);
console.warn('----------------------------------');
console.warn(unusedKeys.map((e) => `${e.locale}:${e.key}`).join('\n'));
console.warn();
}
if (missingKeys.length > 0) {
console.warn(`Missing keys (${missingKeys.length})`);
console.warn('------------------------------------');
console.warn(missingKeys.map((e) => `${e.locale}:${e.key}`).join('\n'));
console.warn();
console.warn('/!\\ Failed: missing keys are not allowed in fr & en');
process.exit(1);
}
} catch (err) {
console.error(err);
process.exit(1);
} finally {
process.exit();
}
}
run();