forked from metabase/metabase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport-po-from-poeditor
executable file
·90 lines (80 loc) · 2.59 KB
/
import-po-from-poeditor
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
#!/usr/bin/env node
// USAGE:
//
// POEDITOR_API_TOKEN=TOKEN ./bin/i18n/import-po-from-poeditor [LANG...]
//
// If no arguments are provided it updates existing locales in ./locales
const fs = require("fs");
const fsp = fs.promises;
const https = require("https");
const fetch = require("isomorphic-fetch");
const url = require("url");
const POEDITOR_API_TOKEN = process.env["POEDITOR_API_TOKEN"];
const POEDITOR_PROJECT_ID = "200535"; // Metabae POEditor project
const aliases = {
"pt-BR": "pt", // default to brazilian portuguese
"zh-Hans": "zh", // default to simplified chinese
};
async function main(args) {
const wanted = new Set(args.length > 0 ? args : await getExistingLanguages());
const other = [];
// list available languages
const { result: { languages } } = await poeditor("languages/list");
for (const language of languages) {
const code = (aliases[language.code] || language.code)
// use "zh-TW" rather than "zh-tw"
.replace(/-.*$/, s => s.toUpperCase());
if (wanted.has(code)) {
wanted.delete(code);
console.log(`Downloading: ${code} (${language.percentage}%)`);
// start an export
const { result: { url } } = await poeditor("projects/export", {
language: language.code,
type: "po",
});
// download the exported file
https.get(url, res =>
res.pipe(fs.createWriteStream(`./locales/${code}.po`)),
);
} else {
other.push(language);
}
}
// log others with large percentage complete
for (const language of other
.filter(l => l.percentage > 50)
.sort((a, b) => b.percentage - a.percentage)) {
console.log(`Other: ${language.code} (${language.percentage}%)`);
}
// log languages that are wanted but missing in POEditor
for (const code of wanted) {
console.log(`Missing: ${code}`);
}
if (wanted.size > 0) {
throw new Error("Some wanted language not found");
}
}
// simple API client for poeditor
function poeditor(command, params = {}) {
const uri = url.format({
protocol: "https",
hostname: "api.poeditor.com",
pathname: `/v2/${command}`,
});
const query = {
api_token: POEDITOR_API_TOKEN,
id: POEDITOR_PROJECT_ID,
...params,
};
return fetch(uri, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: url.format({ query }).replace(/^\?/, ""),
}).then(res => res.json());
}
async function getExistingLanguages() {
return (await fsp.readdir("./locales"))
.filter(f => /\.po$/.test(f))
.map(f => f.replace(/\.po$/, ""));
}
main(process.argv.slice(2)).then(null, console.warn);