import fs from 'fs' ; import path from 'path' ; import _ from 'lodash' ; import fse from 'fs-extra' ; import { t } from '../i18n' ; import Logger from '../util/logger' ; import { formatJSON } from '../util/helper' ; import { iI18nConf , iExtractResult , iTransObj } from '../types' ; param originalScannedWordInfo returns const getScannedUniqueKeys = ( originalScannedWordInfo ) => { const wordList = _ . flattenDeep ( originalScannedWordInfo ) ; const groupedWordList = _ . chain ( wordList ) . groupBy ( 'key' ) . value ( ) ; const scannedUniqueKeys = Object . keys ( groupedWordList ) ; return scannedUniqueKeys ; } ; param targetLang param sourceLang param transFile param AUWordings param extractResult param existObj const fillTransFile = ( targetLang , sourceLang , transFile , AUWordings , extractResult , existObj ? ) => { let extractWordingCount = 0 ; let newTransObj ; if ( AUWordings . length > 0 ) { const AUObj = { } ; AUWordings . map ( ( key ) => { AUObj [ key ] = targetLang === sourceLang ? key : '' ; extractWordingCount += 1 ; } ) ; if ( existObj ) { newTransObj = { ... existObj , ... AUObj } ; } else { newTransObj = AUObj ; } extractResult [ targetLang ] = extractWordingCount ; fse . outputFileSync ( transFile , formatJSON ( newTransObj ) ) ; Logger . success ( t ( '【提取】【{{targetLang}}】词条提取已完成！' , { targetLang , } ) ) ; } else { Logger . warning ( t ( '【提取】【{{targetLang}}】本次无词条变动！' , { targetLang , } ) ) ; } } ; param originalScannedWordInfo param i18nConfig const extractWording = ( originalScannedWordInfo , i18nConf ) : iExtractResult => { const extractResult = { EXTRACT_FILE : 0 } ; const { localeDir , transFileName , transFileExt , parsedLanguages , sourceLanguage , } = i18nConf ; const scannedWordings = getScannedUniqueKeys ( originalScannedWordInfo ) ; parsedLanguages ?. map ( ( lang ) => { const transFile = `${ path . resolve ( localeDir , lang , transFileName ) }.${ transFileExt }` ; const isTransFilesExited = fs . existsSync ( transFile ) ; if ( isTransFilesExited ) { const existObj = fse . readJSONSync ( transFile ) ; const existWording = Object . keys ( existObj ) ; const AUWordings = _ . difference ( scannedWordings , existWording ) ; fillTransFile ( lang , sourceLanguage , transFile , AUWordings , extractResult , existObj ) ; } else { let sourceTransKeys = [ ] ; const sourceTransFile = `${ path . resolve ( localeDir , sourceLanguage , transFileName ) }.${ transFileExt }` ; const isSourceTransFilesExited = fs . existsSync ( sourceTransFile ) ; if ( isSourceTransFilesExited ) { sourceTransKeys = Object . keys ( fse . readJSONSync ( sourceTransFile ) ) ; } fillTransFile ( lang , sourceLanguage , transFile , _ . uniq ( [ ... sourceTransKeys , ... scannedWordings ] ) , extractResult ) ; } } ) ; return extractResult ; } ; export default extractWording ;