-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathfetch-languages.js
80 lines (68 loc) · 2.2 KB
/
fetch-languages.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
/**
* This script fetches languages from Crowdin and Enterprise APIs and saves them to a JSON file.
* @see https://support.crowdin.com/languages/crowdin.json
* @see https://support.crowdin.com/languages/enterprise.json
*/
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const OUTPUT_DIR = path.join(__dirname, 'public/languages');
const CROWDIN_API = 'https://api.crowdin.com/api/v2/languages?limit=500';
const ENTERPRISE_API = 'https://enterprise.api.crowdin.com/api/v2/languages?limit=500';
if (!fs.existsSync(OUTPUT_DIR)) {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
}
async function fetchLanguages(apiUrl) {
try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data.data;
} catch (error) {
console.error(`Error fetching languages from ${apiUrl}:`, error.message);
return [];
}
}
function processLanguages(languages) {
const requiredFields = [
'id',
'name',
'editorCode',
'twoLettersCode',
'threeLettersCode',
'locale',
'androidCode',
'osxCode',
'osxLocale',
];
return languages.reduce((acc, lang) => {
const { id, ...data } = lang.data;
acc[id] = requiredFields.reduce((obj, field) => {
obj[field] = data[field];
return obj;
}, {});
return acc;
}, {});
}
async function main() {
console.log('Fetching languages from Crowdin API...');
const crowdinLanguages = await fetchLanguages(CROWDIN_API);
const crowdinProcessed = processLanguages(crowdinLanguages);
console.log('Fetching languages from Enterprise API...');
const enterpriseLanguages = await fetchLanguages(ENTERPRISE_API);
const enterpriseProcessed = processLanguages(enterpriseLanguages);
fs.writeFileSync(
path.join(OUTPUT_DIR, 'crowdin.json'),
JSON.stringify(crowdinProcessed)
);
fs.writeFileSync(
path.join(OUTPUT_DIR, 'enterprise.json'),
JSON.stringify(enterpriseProcessed)
);
console.log('Language files have been generated successfully!');
}
main().catch(console.error);