-
Notifications
You must be signed in to change notification settings - Fork 14
/
test-locales.js
125 lines (104 loc) · 3.89 KB
/
test-locales.js
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
import { chromium } from 'playwright';
import fs from 'fs/promises';
import path from 'path';
const REFERENCE_LOCALE = 'en';
const SOCIAL_SOURCE_LANGS = ['de', 'en', 'es', 'fr', 'he', 'it', 'nl', 'pl', 'pt', 'ru', 'tr', 'uk', 'zh-TW', 'zh'];
const rootPath = path.resolve('./');
const localeGroups = await fs.readdir(path.join(rootPath, './locales'));
const localesPerGroup = {};
for (const group of localeGroups) {
const locales = (await fs.readdir(path.join(rootPath, './locales', group))).filter((filename) =>
filename.endsWith('.js'),
);
localesPerGroup[group] = {};
for (const locale of locales) {
const module = await import(path.join(rootPath, './locales', group, locale));
localesPerGroup[group][locale.replace('.js', '')] = module.default;
}
}
const getMissingKeys = (reference, definition) => {
const missingKeys = [];
for (const key in reference) {
if (!definition[key]) {
missingKeys.push(key);
}
}
return missingKeys;
};
(async () => {
let anyError = false;
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
for (const group of Object.keys(localesPerGroup)) {
console.log(`Checking group "${group}"`);
const referenceDefinition = localesPerGroup[group][REFERENCE_LOCALE];
if (!referenceDefinition) {
console.error(`Reference locale "${REFERENCE_LOCALE}" not found for group "${group}"`);
anyError = true;
continue;
}
for (const localeName of Object.keys(localesPerGroup[group])) {
console.log(`Checking locale "${localeName}" in group "${group}"`);
const definition = localesPerGroup[group][localeName];
const localeId = definition['locale-id'];
const socialSourceLang = definition['social-source-lang'];
if (!localeId) {
console.error(`Missing locale-id for locale "${localeName}" in group "${group}"`);
anyError = true;
}
if (!socialSourceLang || !SOCIAL_SOURCE_LANGS.includes(socialSourceLang)) {
console.error(
`Invalid social-source-lang "${socialSourceLang}" for locale "${localeName}" in group "${group}"`,
);
anyError = true;
}
const isValidLocaleId = await page.evaluate((id) => {
try {
new Intl.PluralRules(id);
return true;
} catch (err) {
return false;
}
}, localeId);
if (!isValidLocaleId) {
console.error(`Invalid locale-id "${localeId}" for locale "${localeName}" in group "${group}"`);
anyError = true;
}
const pluralCategories = await page.evaluate((id) => {
const pluralRules = new Intl.PluralRules(id);
const options = pluralRules.resolvedOptions();
return options.pluralCategories;
}, localeId);
const missingKeys = getMissingKeys(referenceDefinition, definition);
if (missingKeys.length > 0) {
console.error(`Missing keys for locale ${localeName} in group ${group}: ${missingKeys.join(', ')}`);
anyError = true;
}
const pluralKeys = Object.keys(definition)
.filter((key) => key.includes('__'))
.reduce((acc, key) => {
const [baseKey, pluralCategory] = key.split('__');
if (!acc[baseKey]) {
acc[baseKey] = [];
}
acc[baseKey].push(pluralCategory);
return acc;
}, {});
for (const key of Object.keys(pluralKeys)) {
const missingPluralCategories = pluralCategories.filter((category) => !pluralKeys[key].includes(category));
if (missingPluralCategories.length > 0) {
console.error(
`Missing plural categories for key"${key}" in locale ${localeName} in group ${group}: ${missingPluralCategories.join(', ')}`,
);
anyError = true;
}
}
}
}
await context.close();
await browser.close();
if (anyError) {
process.exit(1);
}
})();