This repository has been archived by the owner on Oct 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
getMessages.js
67 lines (57 loc) · 1.63 KB
/
getMessages.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
const jsonfile = require("jsonfile");
const preq = require("preq");
const DEFAULT_LANGS = ["en"];
const API_BASE_URL = `https://www.mediawiki.org/w/api.php`;
const MESSAGE_KEYS = [
"login",
"readinglists-browser-add-entry-success",
"readinglists-browser-enable-sync-prompt",
"readinglists-browser-error-intro",
"readinglists-browser-extension-info-link-text",
"readinglists-browser-list-entry-limit-exceeded",
"readinglists-browser-login-prompt"
];
const ALLMESSAGES_QUERY = {
action: "query",
format: "json",
formatversion: "2",
meta: "allmessages",
ammessages: MESSAGE_KEYS.join("|"),
amenableparser: ""
};
function objToQueryString(obj) {
return Object.keys(obj)
.map(key => `${key}=${obj[key]}`)
.join("&");
}
function allMessagesRequestUriForLang(lang) {
return `${API_BASE_URL}?${objToQueryString(
Object.assign(ALLMESSAGES_QUERY, { amlang: lang })
)}`;
}
async function getMessages(langs) {
const messages = {};
await Promise.all(
langs.map(async lang => {
await preq.get({ uri: allMessagesRequestUriForLang(lang) }).then(res => {
if (!(res.body && res.body.query && res.body.query.allmessages)) return;
messages[lang] = {};
res.body.query.allmessages.forEach(
messageObj => (messages[lang][messageObj.name] = messageObj.content)
);
});
})
);
return messages;
}
getMessages(DEFAULT_LANGS).then(messages => {
Object.keys(messages).forEach(lang =>
jsonfile.writeFile(
`extension/i18n/${lang}.json`,
messages[lang],
{ spaces: 2 },
err => {}
)
);
console.log("Wrote messages to i18n/[lang].json");
});