|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +if (process.argv[2]) { |
| 6 | + var no_build = process.argv[2].toLowerCase() == '--nobuild'; |
| 7 | + if (no_build == false) { |
| 8 | + console.log( |
| 9 | + 'Incorrect arg. Please use --nobuild if you would like to download without api key.' |
| 10 | + ); |
| 11 | + process.exit(1); |
| 12 | + } |
| 13 | +} else { |
| 14 | + var no_build = false; |
| 15 | + console.log( |
| 16 | + '\n' + |
| 17 | + 'Please note: If you do not have the crowdin API key and would like to download the ' + |
| 18 | + 'translations without building anyways, please make sure your English files are the same ' + |
| 19 | + 'version as crowdin, and then run this script with --nobuild\n\n' + |
| 20 | + 'eg. "node crowdin_download.js --nobuild"\n\n' |
| 21 | + ); |
| 22 | +} |
| 23 | + |
| 24 | +var fs = require('fs'); |
| 25 | +var path = require('path'); |
| 26 | +var https = require('https'); |
| 27 | +var AdmZip = require('adm-zip'); |
| 28 | + |
| 29 | +var crowdin_identifier = 'copay'; |
| 30 | + |
| 31 | +var local_file_name2 = path.join(__dirname, 'docs/appstore_en.txt'); |
| 32 | +var local_file_name3 = path.join(__dirname, 'docs/updateinfo_en.txt'); |
| 33 | + |
| 34 | +try { |
| 35 | + fs.statSync(local_file_name2); |
| 36 | + fs.statSync(local_file_name3); |
| 37 | +} catch (e) { |
| 38 | + console.log( |
| 39 | + '\n### ABORTING ### One of the following files does not exist:\n' + |
| 40 | + local_file_name2 + |
| 41 | + '\n' + |
| 42 | + local_file_name3 |
| 43 | + ); |
| 44 | + process.exit(1); |
| 45 | +} |
| 46 | + |
| 47 | +try { |
| 48 | + // obtain the crowdin api key |
| 49 | + var crowdin_api_key = fs.readFileSync( |
| 50 | + path.join(__dirname, 'crowdin_api_key.txt'), |
| 51 | + 'utf8' |
| 52 | + ); |
| 53 | +} catch (e) { |
| 54 | + console.log( |
| 55 | + '### ERROR ### You do not have the crowdin api key in ./crowdin_api_key.txt' |
| 56 | + ); |
| 57 | + process.exit(1); |
| 58 | +} |
| 59 | + |
| 60 | +if (no_build == false) { |
| 61 | + // Reminder: Any changes to the script below must also be made to the else clause and vice versa. |
| 62 | + |
| 63 | + // This call will tell the server to generate a new zip file for you based on most recent translations. |
| 64 | + https |
| 65 | + .get( |
| 66 | + 'https://api.crowdin.com/api/project/' + |
| 67 | + crowdin_identifier + |
| 68 | + '/export?key=' + |
| 69 | + crowdin_api_key, |
| 70 | + function(res) { |
| 71 | + console.log('Export Got response: ' + res.statusCode); |
| 72 | + |
| 73 | + res.on('data', function(chunk) { |
| 74 | + var resxml = chunk.toString('utf8'); |
| 75 | + console.log(resxml); |
| 76 | + |
| 77 | + if (resxml.indexOf('status="skipped"') >= 0) { |
| 78 | + console.log( |
| 79 | + 'Translation build was skipped due to either:\n' + |
| 80 | + '1. No changes since last translation build, or\n' + |
| 81 | + '2. API limit of once per 30 minutes has not been waited.\n\n' + |
| 82 | + 'Since we can not guarantee that translations have been built properly, this script will end here.\n' + |
| 83 | + 'Log in to Copay\'s Crowdin Settings and click the "Build Project" button to assure it is built recently, and then run this ' + |
| 84 | + 'script again with the --nobuild arg to download translations without checking if built.' |
| 85 | + ); |
| 86 | + process.exit(1); |
| 87 | + } |
| 88 | + |
| 89 | + // Download most recent translations for all languages. |
| 90 | + https.get( |
| 91 | + 'https://crowdin.com/download/project/' + |
| 92 | + crowdin_identifier + |
| 93 | + '.zip', |
| 94 | + function(res) { |
| 95 | + var data = [], |
| 96 | + dataLen = 0; |
| 97 | + |
| 98 | + res |
| 99 | + .on('data', function(chunk) { |
| 100 | + data.push(chunk); |
| 101 | + dataLen += chunk.length; |
| 102 | + }) |
| 103 | + .on('end', function() { |
| 104 | + var buf = new Buffer(dataLen); |
| 105 | + for (var i = 0, len = data.length, pos = 0; i < len; i++) { |
| 106 | + data[i].copy(buf, pos); |
| 107 | + pos += data[i].length; |
| 108 | + } |
| 109 | + var zip = new AdmZip(buf); |
| 110 | + zip.extractAllTo('./', true); |
| 111 | + console.log('Done extracting ZIP file.'); |
| 112 | + |
| 113 | + var files = fs.readdirSync('./docs'); |
| 114 | + |
| 115 | + for (var i in files) { |
| 116 | + debugger; |
| 117 | + if ( |
| 118 | + files[i].slice(0, 9) == 'appstore_' && |
| 119 | + files[i].slice(-4) == '.txt' && |
| 120 | + files[i] != 'appstore_en.txt' |
| 121 | + ) { |
| 122 | + var english_file = fs.readFileSync( |
| 123 | + local_file_name2, |
| 124 | + 'utf8' |
| 125 | + ); |
| 126 | + var compare_file = fs.readFileSync( |
| 127 | + path.join(__dirname, 'docs/' + files[i]), |
| 128 | + 'utf8' |
| 129 | + ); |
| 130 | + english_file = english_file.replace(/\r\n/g, '\n'); |
| 131 | + compare_file = compare_file.replace(/\r\n/g, '\n'); |
| 132 | + if (compare_file == english_file) { |
| 133 | + fs.unlinkSync(path.join(__dirname, 'docs/' + files[i])); |
| 134 | + } |
| 135 | + } |
| 136 | + if ( |
| 137 | + files[i].slice(0, 11) == 'updateinfo_' && |
| 138 | + files[i].slice(-4) == '.txt' && |
| 139 | + files[i] != 'updateinfo_en.txt' |
| 140 | + ) { |
| 141 | + var english_file = fs.readFileSync( |
| 142 | + local_file_name3, |
| 143 | + 'utf8' |
| 144 | + ); |
| 145 | + var compare_file = fs.readFileSync( |
| 146 | + path.join(__dirname, 'docs/' + files[i]), |
| 147 | + 'utf8' |
| 148 | + ); |
| 149 | + english_file = english_file.replace(/\r\n/g, '\n'); |
| 150 | + compare_file = compare_file.replace(/\r\n/g, '\n'); |
| 151 | + if (compare_file == english_file) { |
| 152 | + fs.unlinkSync(path.join(__dirname, 'docs/' + files[i])); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + console.log( |
| 158 | + 'Cleaned out completely untranslated appstore docs.' |
| 159 | + ); |
| 160 | + |
| 161 | + var files = fs.readdirSync('./po'); |
| 162 | + |
| 163 | + for (var i in files) { |
| 164 | + if (files[i] != 'template.pot') { |
| 165 | + var po_file = fs.readFileSync( |
| 166 | + path.join(__dirname, 'po/' + files[i]), |
| 167 | + 'utf8' |
| 168 | + ); |
| 169 | + var po_array = po_file.split('\n'); |
| 170 | + for (var j in po_array) { |
| 171 | + if (po_array[j].slice(0, 5) == 'msgid') { |
| 172 | + var source_text = po_array[j].slice(5); |
| 173 | + } else if (po_array[j].slice(0, 6) == 'msgstr') { |
| 174 | + var translate_text = po_array[j].slice(6); |
| 175 | + // if a line is not == English, it means there is translation. Keep this file. |
| 176 | + if (source_text != translate_text) { |
| 177 | + // erase email addresses of last translator for privacy |
| 178 | + po_file = po_file.replace(/ <.+@.+\..+>/, ''); |
| 179 | + fs.writeFileSync( |
| 180 | + path.join(__dirname, 'po/' + files[i]), |
| 181 | + po_file |
| 182 | + ); |
| 183 | + |
| 184 | + // split the file into 3 parts, before locale, locale, and after locale. |
| 185 | + var lang_pos = po_file.search('"Language: ') + 11; |
| 186 | + var po_start = po_file.slice(0, lang_pos); |
| 187 | + var po_locale = po_file.slice( |
| 188 | + lang_pos, |
| 189 | + lang_pos + 5 |
| 190 | + ); |
| 191 | + var po_end = po_file.slice(lang_pos + 5); |
| 192 | + |
| 193 | + // check for underscore, if it's there, only take the first 2 letters and reconstruct the po file. |
| 194 | + if (po_locale.search('_') > 0) { |
| 195 | + fs.writeFileSync( |
| 196 | + path.join(__dirname, 'po/' + files[i]), |
| 197 | + po_start + po_locale.slice(0, 2) + po_end |
| 198 | + ); |
| 199 | + po_start = ''; |
| 200 | + po_locale = ''; |
| 201 | + po_end = ''; |
| 202 | + } |
| 203 | + break; |
| 204 | + } |
| 205 | + } |
| 206 | + if (j == po_array.length - 1) { |
| 207 | + // All strings are exactly identical to English. Delete po file. |
| 208 | + fs.unlinkSync(path.join(__dirname, 'po/' + files[i])); |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + console.log('Cleaned out completely untranslated po files.'); |
| 215 | + }); |
| 216 | + } |
| 217 | + ); |
| 218 | + }); |
| 219 | + } |
| 220 | + ) |
| 221 | + .on('error', function(e) { |
| 222 | + console.log('Export Got error: ' + e.message); |
| 223 | + }); |
| 224 | +} else { |
| 225 | + // Reminder: Any changes to the script below must also be made to the above and vice versa. |
| 226 | + |
| 227 | + // Download most recent translations for all languages. |
| 228 | + https.get( |
| 229 | + 'https://api.crowdin.com/api/project/' + |
| 230 | + crowdin_identifier + |
| 231 | + '/download/all.zip?key=' + |
| 232 | + crowdin_api_key, |
| 233 | + function(res) { |
| 234 | + var data = [], |
| 235 | + dataLen = 0; |
| 236 | + |
| 237 | + res |
| 238 | + .on('data', function(chunk) { |
| 239 | + data.push(chunk); |
| 240 | + dataLen += chunk.length; |
| 241 | + }) |
| 242 | + .on('end', function() { |
| 243 | + var buf = new Buffer(dataLen); |
| 244 | + for (var i = 0, len = data.length, pos = 0; i < len; i++) { |
| 245 | + data[i].copy(buf, pos); |
| 246 | + pos += data[i].length; |
| 247 | + } |
| 248 | + var zip = new AdmZip(buf); |
| 249 | + zip.extractAllTo('./', true); |
| 250 | + console.log('Done extracting ZIP file.'); |
| 251 | + |
| 252 | + var files = fs.readdirSync('./docs'); |
| 253 | + |
| 254 | + for (var i in files) { |
| 255 | + if ( |
| 256 | + files[i].slice(0, 9) == 'appstore_' && |
| 257 | + files[i].slice(-4) == '.txt' && |
| 258 | + files[i] != 'appstore_en.txt' |
| 259 | + ) { |
| 260 | + var english_file = fs.readFileSync(local_file_name2, 'utf8'); |
| 261 | + var compare_file = fs.readFileSync( |
| 262 | + path.join(__dirname, 'docs/' + files[i]), |
| 263 | + 'utf8' |
| 264 | + ); |
| 265 | + english_file = english_file.replace(/\r\n/g, '\n'); |
| 266 | + compare_file = compare_file.replace(/\r\n/g, '\n'); |
| 267 | + if (compare_file == english_file) { |
| 268 | + fs.unlinkSync(path.join(__dirname, 'docs/' + files[i])); |
| 269 | + } |
| 270 | + } |
| 271 | + if ( |
| 272 | + files[i].slice(0, 11) == 'updateinfo_' && |
| 273 | + files[i].slice(-4) == '.txt' && |
| 274 | + files[i] != 'updateinfo_en.txt' |
| 275 | + ) { |
| 276 | + var english_file = fs.readFileSync(local_file_name3, 'utf8'); |
| 277 | + var compare_file = fs.readFileSync( |
| 278 | + path.join(__dirname, 'docs/' + files[i]), |
| 279 | + 'utf8' |
| 280 | + ); |
| 281 | + english_file = english_file.replace(/\r\n/g, '\n'); |
| 282 | + compare_file = compare_file.replace(/\r\n/g, '\n'); |
| 283 | + if (compare_file == english_file) { |
| 284 | + fs.unlinkSync(path.join(__dirname, 'docs/' + files[i])); |
| 285 | + } |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | + console.log('Cleaned out completely untranslated appstore docs.'); |
| 290 | + |
| 291 | + var files = fs.readdirSync('./po'); |
| 292 | + |
| 293 | + for (var i in files) { |
| 294 | + if (files[i] != 'template.pot') { |
| 295 | + var po_file = fs.readFileSync( |
| 296 | + path.join(__dirname, 'po/' + files[i]), |
| 297 | + 'utf8' |
| 298 | + ); |
| 299 | + var po_array = po_file.split('\n'); |
| 300 | + for (var j in po_array) { |
| 301 | + if (po_array[j].slice(0, 5) == 'msgid') { |
| 302 | + var source_text = po_array[j].slice(5); |
| 303 | + } else if (po_array[j].slice(0, 6) == 'msgstr') { |
| 304 | + var translate_text = po_array[j].slice(6); |
| 305 | + // if a line is not == English, it means there is translation. Keep this file. |
| 306 | + if (source_text != translate_text) { |
| 307 | + // erase email addresses of last translator for privacy |
| 308 | + po_file = po_file.replace(/ <.+@.+\..+>/, ''); |
| 309 | + fs.writeFileSync( |
| 310 | + path.join(__dirname, 'po/' + files[i]), |
| 311 | + po_file |
| 312 | + ); |
| 313 | + |
| 314 | + // split the file into 3 parts, before locale, locale, and after locale. |
| 315 | + var lang_pos = po_file.search('"Language: ') + 11; |
| 316 | + var po_start = po_file.slice(0, lang_pos); |
| 317 | + var po_locale = po_file.slice(lang_pos, lang_pos + 5); |
| 318 | + var po_end = po_file.slice(lang_pos + 5); |
| 319 | + |
| 320 | + // check for underscore, if it's there, only take the first 2 letters and reconstruct the po file. |
| 321 | + if (po_locale.search('_') > 0) { |
| 322 | + fs.writeFileSync( |
| 323 | + path.join(__dirname, 'po/' + files[i]), |
| 324 | + po_start + po_locale.slice(0, 2) + po_end |
| 325 | + ); |
| 326 | + po_start = ''; |
| 327 | + po_locale = ''; |
| 328 | + po_end = ''; |
| 329 | + } |
| 330 | + break; |
| 331 | + } |
| 332 | + } |
| 333 | + if (j == po_array.length - 1) { |
| 334 | + // All strings are exactly identical to English. Delete po file. |
| 335 | + fs.unlinkSync(path.join(__dirname, 'po/' + files[i])); |
| 336 | + } |
| 337 | + } |
| 338 | + } |
| 339 | + } |
| 340 | + |
| 341 | + console.log('Cleaned out completely untranslated po files.'); |
| 342 | + }); |
| 343 | + } |
| 344 | + ); |
| 345 | +} |
0 commit comments