import { INormalizedMessage , IICUMessage , IICUMessageTranslation } from '@ngx-i18nsupport/ngx-i18nsupport-lib' ; import { ValidationErrors } from '@angular/forms' ; import { isNullOrUndefined } from '../common/util' ; import { AutoTranslateServiceAPI } from './auto-translate-service-api' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; /**
 * Created by martin on 19.05.2017.
 * Wrapper around INormalizedMessage for GUI usage.
 * Holds the normalized form and the original.
 */ export class NormalizedMessage { /**
     * Original source as string.
     */ private _original : string ; /**
     * normalized message.
     * null if original is unparsable.
     */ private _normalizedMessage : INormalizedMessage ; /**
     * Parse error if message was unparsable.
     */ private _parseError : string ; /**
     * Errors and warnings, lazy evaluated.
     */ private _errors : ValidationErrors ; private _errorsInitialized = false ; private _warnings : ValidationErrors ; private _warningsInitialized = false ; private _sourceMessage : INormalizedMessage ; param original param normalizedMessage param parseError constructor ( original , normalizedMessage , parseError , sourceMessage ) { this . _original = original ; this . _normalizedMessage = normalizedMessage ; this . _parseError = parseError ; this . _sourceMessage = sourceMessage ; this . _errorsInitialized = false ; this . _warningsInitialized = false ; } /**
     * Return a copy of the message.
     */ public copy ( ) { return new NormalizedMessage ( this . _original , this . _normalizedMessage , this . _parseError , this . _sourceMessage ) ; } public dislayText ( normalize ) { if ( normalize ) { if ( this . _normalizedMessage ) { if ( this . isICUMessage ( ) ) { return this . _normalizedMessage . asDisplayString ( ) + ' ' + this . _normalizedMessage . getICUMessage ( ) . asNativeString ( ) ; } else { return this . _normalizedMessage . asDisplayString ( ) ; } } else { return this . _parseError ; } } else { return this . _original ; } } /**
     * Test, wether it is an ICU message.
     */ isICUMessage ( ) { return this . _normalizedMessage && ! isNullOrUndefined ( this . _normalizedMessage . getICUMessage ( ) ) ; } getICUMessage ( ) { return this . _normalizedMessage ? this . _normalizedMessage . getICUMessage ( ) : null ; } public translate ( newValue , normalize ) { let newOriginal ; let newMessage ; let parseError ; if ( normalize ) { try { newMessage = this . _sourceMessage . translate ( newValue ) ; newOriginal = newMessage . asNativeString ( ) ; parseError = null ; } catch ( error ) { parseError = error . message ; newMessage = null ; newOriginal = null ; } } else { newOriginal = newValue ; try { newMessage = this . _sourceMessage . translateNativeString ( newValue ) ; parseError = null ; } catch ( error ) { parseError = error . message ; } } return new NormalizedMessage ( newOriginal , newMessage , parseError , this . _sourceMessage ) ; } param autoTranslateService param sourceLanguage param targetLanguage return public autoTranslateUsingService ( autoTranslateService , sourceLanguage , targetLanguage ) { if ( this . getICUMessage ( ) ) { return this . autoTranslateICUMessageUsingService ( autoTranslateService , sourceLanguage , targetLanguage ) ; } else { return autoTranslateService . translate ( this . dislayText ( true ) , sourceLanguage , targetLanguage ) . pipe ( map ( ( translation ) => { if ( ! isNullOrUndefined ( translation ) ) { return this . translate ( translation , true ) ; } else { return null ; } } ) ) ; } } private autoTranslateICUMessageUsingService ( autoTranslateService , sourceLanguage , targetLanguage ) { const icuMessage = this . getICUMessage ( ) ; const categories = icuMessage . getCategories ( ) ; if ( categories . find ( ( category ) => ! isNullOrUndefined ( category . getMessageNormalized ( ) . getICUMessage ( ) ) ) ) { throw new Error ( 'nested ICU message not supported' ) ; } const allMessages = categories . map ( ( category ) => category . getMessageNormalized ( ) . asDisplayString ( ) ) ; return autoTranslateService . translateMultipleStrings ( allMessages , sourceLanguage , targetLanguage ) . pipe ( map ( ( translations ) => { const icuTranslation = { } ; for ( let i = 0 ; i < translations . length ; i ++ ) { const translationText = translations [ i ] ; icuTranslation [ categories [ i ] . getCategory ( ) ] = translationText ; } const result = this . translateICUMessage ( icuTranslation ) ; return result ; } ) ) ; } public translateICUMessage ( newValue ) { let newOriginal ; let newMessage ; let parseError ; try { if ( this . _normalizedMessage ) { newMessage = this . _normalizedMessage . translateICUMessage ( newValue ) ; } else { newMessage = this . _sourceMessage . translateICUMessage ( newValue ) ; } newOriginal = newMessage . asNativeString ( ) ; parseError = null ; } catch ( error ) { parseError = error . message ; newMessage = null ; newOriginal = null ; } return new NormalizedMessage ( newOriginal , newMessage , parseError , this . _sourceMessage ) ; } public nativeString ( ) { if ( this . _normalizedMessage ) { return this . _normalizedMessage . asNativeString ( ) ; } else { return this . _original ; } } public validate ( normalize ) { if ( ! this . _errorsInitialized ) { if ( normalize ) { if ( this . _normalizedMessage ) { this . _errors = this . _normalizedMessage . validate ( ) ; } else { this . _errors = { 'parseError' : this . _parseError } ; } } else { this . _errors = null ; } this . _errorsInitialized = true ; } return this . _errors ; } public validateWarnings ( normalize ) { if ( ! this . _warningsInitialized ) { if ( normalize ) { if ( this . _normalizedMessage ) { this . _warnings = this . _normalizedMessage . validateWarnings ( ) ; } else { this . _warnings = null ; } } else { this . _warnings = null ; } this . _warningsInitialized = true ; } return this . _warnings ; } }