This repository was archived by the owner on Oct 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.js
87 lines (77 loc) · 2.1 KB
/
convert.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
const fs = require('fs-extra');
const config = require("config");
const request = require("request");
const async = require("async");
const localesFolder = "./locales/";
fs.removeSync(localesFolder);
async.waterfall([
(next) => {
fs.mkdirSync(localesFolder);
next();
},
(next) => {
request.post(`https://api.poeditor.com/v2/languages/list`, {form: {api_token: config.get("poeditor.api_token"), id: config.get("poeditor.project_id")}}, next);
},
(res, body, next) => {
body = JSON.parse(body);
if (body.response.status !== "success") return next(body.response.message);
let languages = body.result.languages.map((language) => {
return language.code;
});
next(null, languages);
},
(languages, next) => {
async.each(
languages,
(language, next) => {
async.waterfall([
(next) => {
fs.mkdirSync(`${localesFolder}${language}`);
next();
},
(next) => {
request.post(`https://api.poeditor.com/v2/projects/export`, {form: {api_token: config.get("poeditor.api_token"), id: config.get("poeditor.project_id"), language, type: "key_value_json", filters: "translated"}}, next);
},
(res, body, next) => {
body = JSON.parse(body);
if (body.response.status !== "success") return next(body.response.message);
request(body.result.url, next);
},
(res, body, next) => {
if (!body) body = "{}";
body = JSON.parse(body);
const files = [];
Object.keys(body).forEach((namespace) => {
files.push({
namespace,
content: body[namespace]
});
});
next(null, files);
},
(files, next) => {
async.each(
files,
(file, next) => {
fs.writeFile(`${localesFolder}${language}/${file.namespace}.json`, JSON.stringify(file.content), function(err) {
if(err) console.err(`Failed to write namespace ${file.namespace} for language ${language}.`);
next();
});
},
() => {
next();
}
);
}
], () => {
next();
});
},
() => {
next();
}
);
}
], (err, res) => {
console.log(err, res);
});