-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
crowdin_download.js
160 lines (143 loc) · 5.2 KB
/
crowdin_download.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env node
'use strict';
console.log(
'\nPlease remember to build translations ' +
'before to download and commit new changes\n'
);
var fs = require('fs');
var path = require('path');
var https = require('https');
var AdmZip = require('adm-zip');
var crowdin_identifier = 'copay';
var local_file_name2 = path.join(__dirname, 'docs/appstore_en.txt');
var local_file_name3 = path.join(__dirname, 'docs/updateinfo_en.txt');
try {
fs.statSync(local_file_name2);
fs.statSync(local_file_name3);
} catch (e) {
console.log(
'\n### ABORTING ### One of the following files does not exist:\n' +
local_file_name2 +
'\n' +
local_file_name3
);
process.exit(1);
}
try {
// obtain the crowdin api key
var crowdin_api_key = fs.readFileSync(
path.join(__dirname, 'crowdin_api_key.txt'),
'utf8'
);
} catch (e) {
console.log(
'### ERROR ### You do not have the crowdin api key in ./crowdin_api_key.txt'
);
process.exit(1);
}
// Download most recent translations for all languages.
https.get(
'https://api.crowdin.com/api/project/' +
crowdin_identifier +
'/download/all.zip?key=' +
crowdin_api_key,
function (res) {
var data = [],
dataLen = 0;
res
.on('data', function (chunk) {
data.push(chunk);
dataLen += chunk.length;
})
.on('end', function () {
var buf = new Buffer.alloc(dataLen);
for (var i = 0, len = data.length, pos = 0; i < len; i++) {
data[i].copy(buf, pos);
pos += data[i].length;
}
var zip = new AdmZip(buf);
zip.extractAllTo('/tmp/copay', true);
console.log('Done extracting ZIP file.');
var files = fs.readdirSync('/tmp/copay/docs');
for (var i in files) {
if (
files[i].slice(0, 9) == 'appstore_' &&
files[i].slice(-4) == '.txt' &&
files[i] != 'appstore_en.txt'
) {
var english_file = fs.readFileSync(local_file_name2, 'utf8');
var compare_file = fs.readFileSync(
path.join(__dirname, 'docs/' + files[i]),
'utf8'
);
english_file = english_file.replace(/\r\n/g, '\n');
compare_file = compare_file.replace(/\r\n/g, '\n');
if (compare_file == english_file) {
fs.unlinkSync(path.join(__dirname, 'docs/' + files[i]));
}
}
if (
files[i].slice(0, 11) == 'updateinfo_' &&
files[i].slice(-4) == '.txt' &&
files[i] != 'updateinfo_en.txt'
) {
var english_file = fs.readFileSync(local_file_name3, 'utf8');
var compare_file = fs.readFileSync(
path.join(__dirname, 'docs/' + files[i]),
'utf8'
);
english_file = english_file.replace(/\r\n/g, '\n');
compare_file = compare_file.replace(/\r\n/g, '\n');
if (compare_file == english_file) {
fs.unlinkSync(path.join(__dirname, 'docs/' + files[i]));
}
}
}
console.log('Cleaned out completely untranslated appstore docs.');
var files = fs.readdirSync('/tmp/copay/po');
for (var i in files) {
var po_file = fs.readFileSync('/tmp/copay/po/' + files[i], 'utf8');
var po_array = po_file.split('\n');
for (var j in po_array) {
if (po_array[j].slice(0, 5) == 'msgid') {
var source_text = po_array[j].slice(5);
} else if (po_array[j].slice(0, 6) == 'msgstr') {
var translate_text = po_array[j].slice(6);
// if a line is not == English, it means there is translation. Keep this file.
if (source_text != translate_text) {
// erase email addresses of last translator for privacy
po_file = po_file.replace(/ <.+@.+\..+>/, '');
fs.writeFileSync(
path.join(__dirname, '../src/assets/i18n/' + files[i]),
po_file
);
// split the file into 3 parts, before locale, locale, and after locale.
var lang_pos = po_file.search('"Language: ') + 11;
var po_start = po_file.slice(0, lang_pos);
var po_locale = po_file.slice(lang_pos, lang_pos + 5);
var po_end = po_file.slice(lang_pos + 5);
// check for underscore, if it's there, only take the first 2 letters and reconstruct the po file.
if (po_locale.search('_') > 0) {
fs.writeFileSync(
path.join(__dirname, '../src/assets/i18n/' + files[i]),
po_start + po_locale.slice(0, 2) + po_end
);
po_start = '';
po_locale = '';
po_end = '';
}
break;
}
}
if (j == po_array.length - 1) {
// All strings are exactly identical to English. Delete po file.
fs.unlinkSync(
path.join(__dirname, '../src/assets/i18n/' + files[i])
);
}
}
}
console.log('Cleaned out completely untranslated po files.');
});
}
);