This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
fetchDescriptions.ts
113 lines (100 loc) · 2.35 KB
/
fetchDescriptions.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
import { ensureDirSync, removeSync, writeFileSync } from "fs-extra";
import graphqlRequest from "./src/util/functions/graphql";
let langList = [];
graphqlRequest(`
query {
langFiles(project: "extension") {
lang
}
}
`).then(res => res.data.langFiles.forEach(lang => {
langList.push(lang.lang);
})).finally(() => {
removeSync("src/_locales");
Promise.all(
langList.map(async langCode => {
return [
langCode,
await translationsInLanguage(langCode, "extension.description.short")
];
})
).then(data => {
data.map(description => {
switch (description[0]) {
case "pt":
description[0] = "pt_PT";
break;
case "ar_SA":
description[0] = "ar";
break;
case "cs_CZ":
description[0] = "cs";
break;
case "da_DK":
description[0] = "da";
break;
case "he_IL":
description[0] = "he";
break;
case "ja_JP":
description[0] = "ja";
break;
case "ko_KR":
description[0] = "ko";
break;
case "sl_SI":
description[0] = "sl";
break;
case "sv_SE":
description[0] = "sv";
break;
case "uk_UA":
description[0] = "uk";
break;
case "bs_BA":
description[0] = "bs";
break;
}
if (!description[1]) return;
ensureDirSync(`src/_locales/${description[0]}`);
writeFileSync(
`src/_locales/${description[0]}/messages.json`,
JSON.stringify({
description: {
message: description[1]
}
})
);
});
});
});
// Obtain extension strings, whole project or specific string (if given) fallbacks to english
async function translationsInLanguage(langCode: string, string?: string): Promise<object|string> {
const FALLBACK_LOCALE = "en";
const langFiles = (await graphqlRequest(`
query {
langFiles(project: "extension", lang: "${langCode}") {
translations
}
}
`)).data.langFiles;
if (langFiles.length === 0) {
if (langCode !== FALLBACK_LOCALE) {
return !string
? await translationsInLanguage(FALLBACK_LOCALE)
: await translationsInLanguage(FALLBACK_LOCALE, string);
} else {
return string ?? {};
}
}
const translations = langFiles[0].translations;
if (!string) {
return translations;
}
if (translations[string]) {
return translations[string];
} else if (langCode !== FALLBACK_LOCALE) {
return await translationsInLanguage(FALLBACK_LOCALE, string);
}
return string;
}