-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPoEditorWidgetTerms.js
More file actions
139 lines (100 loc) · 3.25 KB
/
getPoEditorWidgetTerms.js
File metadata and controls
139 lines (100 loc) · 3.25 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var request = require("request");
var program = require('commander');
var fs = require('fs');
var querystring = require('querystring');
program.version('1.0.0')
.option('-p, --projectId [type]', 'projectId', "97547")
.option('-t, --token [type]', 'token')
.parse(process.argv);
downloadData(program.projectId, program.language, program.token);
function downloadData(projectId, language, token) {
var data_tokenId = {
api_token: token,
id: projectId,
}
var postData_languages = querystring.stringify(data_tokenId);
console.log("get languages...");
getProjectLanguages(postData_languages, (languages) => {
for (id in languages) {
var lang_code = languages[id].code;
var data_tokenIdLanguage = {
api_token: token,
id: projectId,
language: lang_code
}
var postData_terms = querystring.stringify(data_tokenIdLanguage);
getTerms(postData_terms, lang_code);
}
});
//getTerms(postData_terms);
}
function getProjectLanguages(token_data, callback) {
var options = {
method: 'POST',
url: 'https://api.poeditor.com/v2/languages/list',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'cache-control': 'no-cache'
},
body: token_data,
json: true
};
request(options, function(error, response, body) {
if (error) throw new Error(error);
request(options, function(error, response, body) {
if (error) throw new Error(error);
var languages = [];
for (id in body.result.languages) {
var code = body.result.languages[id].code;
var name = body.result.languages[id].name;
var lang_obj = {
name: name,
code: code
};
languages.push(lang_obj);
}
console.log("Languages: " + languages.length);
callback(languages);
});
});
}
function getTerms(token_data, languageCode) {
var options = {
method: 'POST',
url: 'https://api.poeditor.com/v2/terms/list',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'cache-control': 'no-cache'
},
body: token_data,
json: true
};
request(options, function(error, response, body) {
if (error) throw new Error(error);
var terms = body.result.terms;
//var export_data = [];
var term_obj = {};
for (id in body.result.terms) {
var result_term = body.result.terms[id];
var term = result_term.term;
var translation = result_term.translation.content;
translation = translation.replace('%d', '{number}');
translation = translation.replace('%s', '{string}');
term_obj[term] = translation;
//console.log(term_obj['term'] + " -- " + translation);
//console.log(term + " --- " + translation);
}
//console.log("Terms count: " + terms.length);
//console.log(export_data);
var exportString = "/* TrashOut Widget localization file - language: " + program.language + " */ \n";
exportString += `var i18nLoadLanguages = function (lng) { \n \t if (lng === '${languageCode}' ) { \n \t \t i18n.translator.add({ \n \t \t \t values: `;
exportString += JSON.stringify(term_obj, null, '\t \t \t \t');
exportString += " \n }); } \n else { console.log(lng); \n }};";
fs.writeFile("messages_widgets/" + languageCode + ".js", exportString, function(err) {
if (err) {
return console.log(err);
}
console.log(`The file ${languageCode} was saved!`);
});
});
}