author zongwenjian
 *  desc require ( 'ts-node' ) . register ( { compilerOptions : { module : 'commonjs' } } ) ; import * as path from 'path' ; import * as fs from 'fs' ; import * as baiduTranslate from 'baidu-translate' ; import { tsvFormatRows } from 'd3-dsv' ; import { getProjectConfig , getLangDir , withTimeout , translateText } from './utils' ; import { importMessages } from './import' ; import { getAllUntranslatedTexts } from './mock' ; const CONFIG = getProjectConfig ( ) ; param text param toLang function translateTextByBaidu ( text , toLang ) { const { baiduApiKey : { appId , appKey } , baiduLangMap } = CONFIG ; return withTimeout ( new Promise ( ( resolve , reject ) => { baiduTranslate ( appId , appKey , baiduLangMap [ toLang ] , 'zh' ) ( text ) . then ( data => { if ( data && data . trans_result ) { resolve ( data . trans_result ) ; } else { reject ( `\n百度翻译api调用异常 error_code: ${ data . error_code }, error_msg: ${ data . error_msg }` ) ; } } ) . catch ( err => { reject ( err ) ; } ) ; } ) , 3000 ) ; } /** 文案首字母大小 变量小写 */ function textToUpperCaseByFirstWord ( text ) { return text ? `${ text . charAt ( 0 ) . toUpperCase ( ) }${ text . slice ( 1 ) }` . replace ( /(\{.*?\})/g , text => text . toLowerCase ( ) ) : '' ; } param untranslatedTexts param toLang async function googleTranslateTexts ( untranslatedTexts , toLang ) { const translateAllTexts = Object . keys ( untranslatedTexts ) . map ( key => { return translateText ( untranslatedTexts [ key ] , toLang ) . then ( translatedText => [ key , translatedText ] ) ; } ) ; return new Promise ( resolve => { const result = { } ; Promise . all ( translateAllTexts ) . then ( res => { res . forEach ( ( [ key , translatedText ] ) => { result [ key ] = translatedText ; } ) ; resolve ( result ) ; } ) ; } ) ; } param untranslatedTexts param toLang async function baiduTranslateTexts ( untranslatedTexts , toLang ) { return new Promise ( async ( resolve ) => { const result = { } ; const untranslatedKeys = Object . keys ( untranslatedTexts ) ; const taskLists = { } ; let lastIndex = 0 ; untranslatedKeys . reduce ( ( pre , next , index ) => { const byteLen = Buffer . byteLength ( pre , 'utf8' ) ; if ( byteLen > 5500 ) { taskLists [ lastIndex ] = ( ) => { return new Promise ( resolve => { setTimeout ( ( ) => { resolve ( translateTextByBaidu ( pre , toLang ) ) ; } , 1500 ) ; } ) ; } ; lastIndex = index ; return untranslatedTexts [ next ] ; } else if ( index === untranslatedKeys . length - 1 ) { taskLists [ lastIndex ] = ( ) => { return new Promise ( resolve => { setTimeout ( ( ) => { resolve ( translateTextByBaidu ( `${ pre }\n${ untranslatedTexts [ next ] }` , toLang ) ) ; } , 1500 ) ; } ) ; } ; } return `${ pre }\n${ untranslatedTexts [ next ] }` ; } , '' ) ; const taskKeys = Object . keys ( taskLists ) ; if ( taskKeys . length > 0 ) { for ( var i = 0 ; i < taskKeys . length ; i ++ ) { const langIndexKey = taskKeys [ i ] ; const taskItemFun = taskLists [ langIndexKey ] ; const data = await taskItemFun ( ) ; ( data || [ ] ) . forEach ( ( { dst } , index ) => { const currTextKey = untranslatedKeys [ Number ( langIndexKey ) + index ] ; result [ currTextKey ] = textToUpperCaseByFirstWord ( dst ) ; } ) ; } } resolve ( result ) ; } ) ; } param dstLang async function runTranslateApi ( dstLang , origin ) { const untranslatedTexts = getAllUntranslatedTexts ( dstLang ) ; let mocks = { } ; if ( origin === 'Google' ) { mocks = await googleTranslateTexts ( untranslatedTexts , dstLang ) ; } else { mocks = await baiduTranslateTexts ( untranslatedTexts , dstLang ) ; } const messagesToTranslate = Object . keys ( mocks ) . map ( key => [ key , mocks [ key ] ] ) ; if ( messagesToTranslate . length === 0 ) { return Promise . resolve ( ) ; } const content = tsvFormatRows ( messagesToTranslate ) ; return new Promise ( ( resolve , reject ) => { const filePath = path . resolve ( getLangDir ( dstLang ) , `${ dstLang }_translate.tsv` ) ; fs . writeFile ( filePath , content , err => { if ( err ) { reject ( err ) ; } else { console . log ( `${ dstLang } 自动翻译完成` ) ; importMessages ( filePath , dstLang ) ; resolve ( ) ; } } ) ; } ) ; } param origin async function translate ( origin ) { const langs = CONFIG . distLangs ; if ( origin === 'Google' ) { const mockPromise = langs . map ( lang => { return runTranslateApi ( lang , origin ) ; } ) ; return Promise . all ( mockPromise ) ; } else { for ( var i = 0 ; i < langs . length ; i ++ ) { await runTranslateApi ( langs [ i ] , origin ) ; } return Promise . resolve ( ) ; } } export { translate , baiduTranslateTexts , googleTranslateTexts } ;