import path from 'path' ; import fs from 'fs' ; import fse from 'fs-extra' ; import * as tencentCloud from 'tencentcloud-sdk-nodejs' ; import { t } from '../i18n' ; import Logger from '../util/logger' ; import { formatJSON } from '../util/helper' ; import { iI18nConf , TransType , iActionResult } from '../types' ; param targetLang param unTranslateKeys param i18nConf param source returns const translateFromSourceFile = ( targetLang , unTranslateKeys , i18nConf , source ) => { let transCount = 0 ; const { targetTransDir , targetTransFile } = i18nConf ; const targetTranslateFile = `${ path . resolve ( targetTransDir , targetLang , targetTransFile ) }` ; const isTargetTranslateFileExited = fs . existsSync ( targetTranslateFile ) ; if ( isTargetTranslateFileExited ) { const target = fse . readJSONSync ( targetTranslateFile , { throws : false } ) || { } ; unTranslateKeys . map ( ( key ) => { const transValue = typeof target [ key ] === 'undefined' ? '' : target [ key ] ; if ( transValue ) { transCount += 1 ; } source [ key ] = transValue ; } ) ; } else { Logger . error ( t ( '【翻译错误】翻译文件{{targetTranslateFile}}不存在 请检查命令行【translate】后的语言 或 i18n.config.json 中配置' , { targetTranslateFile , } ) ) ; } return transCount ; } ; param targetLang param unTranslateKeys param i18nConf param source returns const translateFromTMT = async ( targetLang , unTranslateKeys , i18nConf , source ) => { const { secretId , secretKey } = i18nConf ; if ( ! secretId && ! secretKey ) { Logger . error ( t ( '【翻译错误】secretId或secretKey不存在 请检查i18n.config.json 中【secretId】或【secretKey】配置' ) ) ; return - 1 ; } let transCount = 0 ; const TMTClient = tencentCloud . tmt . v20180321 . Client ; const clientConfig = { credential : { secretId , secretKey , } , region : 'ap-shanghai' , profile : { signMethod : 'HmacSHA256' , httpProfile : { reqMethod : 'POST' , reqTimeout : 30 , } , } , } ; const client = new TMTClient ( clientConfig ) ; const { TargetTextList } = await client . TextTranslateBatch ( { Source : 'auto' , Target : targetLang , ProjectId : 0 , SourceTextList : unTranslateKeys , } ) ; const transObj = { } ; if ( TargetTextList ) { for ( let i = 0 ; i < unTranslateKeys . length ; i += 1 ) { transObj [ unTranslateKeys [ i ] ] = TargetTextList [ i ] ; } } unTranslateKeys . map ( ( key ) => { const transValue = typeof transObj [ key ] === 'undefined' ? '' : transObj [ key ] ; if ( transValue ) { transCount += 1 ; } source [ key ] = transValue ; } ) ; return transCount ; } ; param languages param i18nConf param transType returns const translate = async ( languages , i18nConf , transType ) : Promise < iActionResult > => { const transResult = { TRANSLATE_FILE : 0 } ; const tranTypeWording = transType === TransType . SourceFile ? t ( '【翻译 - 从源文件翻译】' ) : t ( '【翻译 - TMT机器翻译】' ) ; const { localeDir , transFileName , transFileExt } = i18nConf ; const curLanguages = typeof languages === 'string' ? languages . split ( ',' ) : languages ; await Promise . all ( curLanguages . map ( async ( lang ) => { let transCount = 0 ; const transFile = `${ path . resolve ( localeDir , lang , transFileName ) }.${ transFileExt }` ; const isTransFilesExited = fs . existsSync ( transFile ) ; if ( isTransFilesExited ) { const source = fse . readJSONSync ( transFile ) ; const unTranslatedWord = Object . fromEntries ( Object . entries ( source ) . filter ( ( [ key , value ] ) => value === '' ) ) ; const unTransKeys = Object . keys ( unTranslatedWord ) ; if ( unTransKeys . length > 0 ) { if ( transType === TransType . SourceFile ) { transCount = translateFromSourceFile ( lang , unTransKeys , i18nConf , source ) ; } else { transCount = await translateFromTMT ( lang , unTransKeys , i18nConf , source ) ; } if ( transCount > 0 ) { transResult [ lang ] = transCount ; fse . outputFileSync ( transFile , formatJSON ( source ) ) ; Logger . success ( t ( '{{tranTypeWording}}【{{lang}}】已完成！' , { tranTypeWording , lang , } ) ) ; } else if ( transCount === 0 ) { Logger . warning ( t ( '{{tranTypeWording}}【{{lang}}】本次无词条被翻译！' , { tranTypeWording , lang , } ) ) ; } } else { Logger . warning ( t ( '{{tranTypeWording}}【{{lang}}】本次没有词条需要翻译！' , { tranTypeWording , lang , } ) ) ; } } else { Logger . error ( t ( '【翻译错误】待翻译文件{{transFile}}不存在 请检查命令行【translate】后的语言 或 i18n.config.json 中【languages】配置' , { transFile , } ) ) ; } } ) ) ; return transResult ; } ; export default translate ;