import { ITranslationMessagesFile , ITransUnit , NORMALIZATION_FORMAT_NGXTRANSLATE } from '@ngx-i18nsupport/ngx-i18nsupport-lib' ; import { FileUtil } from '../common/file-util' ; import { isNullOrUndefined } from '../common/util' ; import { NgxTranslateExtractionPattern } from './ngx-translate-extraction-pattern' ; /**
 * Created by roobm on 15.03.2017.
 * A tool for extracting messages in ngx-translate format.
 * Generates a json-file to be used with ngx-translate.
 */ /**
 * The interface used for translations in ngx-translate.
 * A hash that contains either the translation or another hash.
 */ interface NgxTranslations { [ id ] : NgxTranslations | string ; } /**
 * internal,
 * a message with id (a dot-separated string).
 */ interface NgxMessage { id : string ; message : string ; } export class NgxTranslateExtractor { public static DefaultExtractionPattern = '@@|ngx-translate' ; private extractionPattern : NgxTranslateExtractionPattern ; param extractionPatternString return public static checkPattern ( extractionPatternString ) { try { if ( new NgxTranslateExtractionPattern ( extractionPatternString ) ) { return null ; } } catch ( error ) { return error . message ; } return null ; } public static extract ( messagesFile , extractionPattern , outputFile ) { new NgxTranslateExtractor ( messagesFile , extractionPattern ) . extractTo ( outputFile ) ; } constructor ( private messagesFile , extractionPatternString ) { this . extractionPattern = new NgxTranslateExtractionPattern ( extractionPatternString ) ; } param outputFile public extractTo ( outputFile ) { const translations = this . toNgxTranslations ( this . extract ( ) ) ; if ( translations && Object . keys ( translations ) . length > 0 ) { FileUtil . replaceContent ( outputFile , JSON . stringify ( translations , null , 4 ) , 'UTF-8' ) ; } else { if ( FileUtil . exists ( outputFile ) ) { FileUtil . deleteFile ( outputFile ) ; } } } return private extract ( ) { const result = [ ] ; this . messagesFile . forEachTransUnit ( ( tu ) => { const ngxId = this . ngxTranslateIdFromTU ( tu ) ; if ( ngxId ) { const messagetext = tu . targetContentNormalized ( ) . asDisplayString ( NORMALIZATION_FORMAT_NGXTRANSLATE ) ; result . push ( { id : ngxId , message : messagetext } ) ; } } ) ; return result ; } param tu return private ngxTranslateIdFromTU ( tu ) { if ( this . isExplicitlySetId ( tu . id ) ) { if ( this . extractionPattern . isExplicitIdMatched ( tu . id ) ) { return tu . id ; } else { return null ; } } const description = tu . description ( ) ; if ( description && this . extractionPattern . isDescriptionMatched ( description ) ) { return tu . meaning ( ) ; } } param id return private isExplicitlySetId ( id ) { if ( isNullOrUndefined ( id ) ) { return false ; } const reForGeneratedId = /^[0-9a-f]{11,}$/ ; return ! reForGeneratedId . test ( id ) ; } param msgList private toNgxTranslations ( msgList ) { const translationObject = { } ; msgList . forEach ( ( msg ) => { this . putInTranslationObject ( translationObject , msg ) ; } ) ; return translationObject ; } param translationObject param msg private putInTranslationObject ( translationObject , msg ) { let firstPartOfId ; let restOfId ; const indexOfDot = msg . id . indexOf ( '.' ) ; if ( indexOfDot === 0 || indexOfDot === ( msg . id . length - 1 ) ) { throw new Error ( 'bad nxg-translate id "' + msg . id + '"' ) ; } if ( indexOfDot < 0 ) { firstPartOfId = msg . id ; restOfId = '' ; } else { firstPartOfId = msg . id . substring ( 0 , indexOfDot ) ; restOfId = msg . id . substring ( indexOfDot + 1 ) ; } let object = translationObject [ firstPartOfId ] ; if ( isNullOrUndefined ( object ) ) { if ( restOfId === '' ) { translationObject [ firstPartOfId ] = msg . message ; return ; } object = { } ; translationObject [ firstPartOfId ] = object ; } else { if ( restOfId === '' ) { throw new Error ( 'duplicate id praefix "' + msg . id + '"' ) ; } } this . putInTranslationObject ( < NgxTranslations > object , { id : restOfId , message : msg . message } ) ; } }