2015-07-21 09:07:24 -07:00
#!/usr/bin/env node
'use strict' ;
2015-08-15 09:05:43 -07:00
if ( process . argv [ 2 ] ) {
var no _key = ( process . argv [ 2 ] . toLowerCase ( ) == '--nokey' )
if ( no _key == false ) {
console . log ( 'Incorrect arg. Please use --nokey if you would like to download without api key.' ) ;
process . exit ( 1 ) ;
} ;
} else {
var no _key = false ;
console . log ( '\n' +
'Please note: If you do not have the crowdin API key and would like to download the ' +
'translations without building anyways, please make sure your English files are the same ' +
'version as crowdin, and then run this script with --nokey\n\n' +
'eg. "node crowdin_download.js --nokey"\n\n' ) ;
} ;
2015-07-21 09:07:24 -07:00
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' )
2015-07-24 19:10:46 -07:00
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 ) ;
}
2015-08-15 09:05:43 -07:00
if ( no _key == false ) { // Reminder: Any changes to the script below must also be made to the else clause and vice versa.
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 so the translation build has failed.\nFor download only use --nokey.' ) ;
process . exit ( 1 ) ;
} ;
// This call will tell the server to generate a new zip file for you based on most recent translations.
https . get ( 'https://api.crowdin.com/api/project/' + crowdin _identifier + '/export?key=' + crowdin _api _key , function ( res ) {
console . log ( 'Export Got response: ' + res . statusCode ) ;
res . on ( 'data' , function ( chunk ) {
var resxml = chunk . toString ( 'utf8' ) ;
console . log ( resxml ) ;
2015-07-21 09:07:24 -07:00
2015-08-15 09:05:43 -07:00
if ( resxml . indexOf ( 'status="skipped"' ) >= 0 ) {
console . log ( 'Translation build was skipped due to either:\n' +
'1. No changes since last translation build, or\n' +
'2. API limit of once per 30 minutes has not been waited.\n\n' +
'Since we can not guarantee that translations have been built properly, this script will end here.\n' +
'Log in to Copay\'s Crowdin Settings and click the "Build Project" button to assure it is built recently, and then run this ' +
'script again with the --nokey arg to download translations without checking if built.' ) ;
process . exit ( 1 ) ;
} ;
2015-07-21 09:07:24 -07:00
2015-08-15 09:05:43 -07:00
// Download most recent translations for all languages.
https . get ( 'https://crowdin.com/download/project/' + crowdin _identifier + '.zip' , function ( res ) {
var data = [ ] , dataLen = 0 ;
res . on ( 'data' , function ( chunk ) {
data . push ( chunk ) ;
dataLen += chunk . length ;
} ) . on ( 'end' , function ( ) {
var buf = new Buffer ( 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 ( './' , true ) ;
console . log ( 'Done extracting ZIP file.' ) ;
var files = fs . readdirSync ( './docs' ) ;
for ( var i in files ) {
debugger ;
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 ( './po' ) ;
for ( var i in files ) {
if ( files [ i ] != 'template.pot' ) {
var po _file = fs . readFileSync ( path . join ( _ _dirname , '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 , 'po/' + 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 , 'po/' + 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 , 'po/' + files [ i ] ) ) ;
} ;
} ;
} ;
} ;
console . log ( 'Cleaned out completely untranslated po files.' ) ;
} ) ;
} ) ;
} ) ;
} ) . on ( 'error' , function ( e ) {
console . log ( 'Export Got error: ' + e . message ) ;
} ) ;
} else { // Reminder: Any changes to the script below must also be made to the above and vice versa.
// Download most recent translations for all languages.
https . get ( 'https://crowdin.com/download/project/' + crowdin _identifier + '.zip' , function ( res ) {
var data = [ ] , dataLen = 0 ;
res . on ( 'data' , function ( chunk ) {
data . push ( chunk ) ;
dataLen += chunk . length ;
} ) . on ( 'end' , function ( ) {
var buf = new Buffer ( dataLen ) ;
for ( var i = 0 , len = data . length , pos = 0 ; i < len ; i ++ ) {
data [ i ] . copy ( buf , pos ) ;
pos += data [ i ] . length ;
2015-07-21 09:07:24 -07:00
} ;
2015-08-15 09:05:43 -07:00
var zip = new AdmZip ( buf ) ;
zip . extractAllTo ( './' , true ) ;
console . log ( 'Done extracting ZIP file.' ) ;
var files = fs . readdirSync ( './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 ] ) ) ;
} ;
2015-07-21 09:07:24 -07:00
} ;
} ;
2015-08-15 09:05:43 -07:00
console . log ( 'Cleaned out completely untranslated appstore docs.' ) ;
var files = fs . readdirSync ( './po' ) ;
for ( var i in files ) {
if ( files [ i ] != 'template.pot' ) {
var po _file = fs . readFileSync ( path . join ( _ _dirname , '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 , 'po/' + 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 , 'po/' + files [ i ] ) , po _start + po _locale . slice ( 0 , 2 ) + po _end ) ;
po _start = '' ;
po _locale = '' ;
po _end = '' ;
} ;
break ;
2015-07-23 07:38:14 -07:00
} ;
2015-07-21 09:07:24 -07:00
} ;
2015-08-15 09:05:43 -07:00
if ( j == po _array . length - 1 ) { // All strings are exactly identical to English. Delete po file.
fs . unlinkSync ( path . join ( _ _dirname , 'po/' + files [ i ] ) ) ;
} ;
2015-07-21 09:07:24 -07:00
} ;
} ;
} ;
2015-08-15 09:05:43 -07:00
console . log ( 'Cleaned out completely untranslated po files.' ) ;
} ) ;
} ) ;
} ;