import { ITransUnit , INormalizedMessage , STATE_NEW } from 'ngx-i18nsupport-lib' ; import { TranslationFile } from './translation-file' ; import { NormalizedMessage } from './normalized-message' ; import { format , isNullOrUndefined } from 'util' ; import { AutoTranslateResult } from './auto-translate-result' ; import { IICUMessageTranslation } from 'ngx-i18nsupport-lib/dist' ; /**
 * A wrapper around ITransUnit.
 * Adds some support for easier GUI handling.
 * Created by martin on 24.03.2017.
 */ export class TranslationUnit { private _isDirty : boolean ; private _normalizedSourceContent : NormalizedMessage ; private _normalizedTargetContent : NormalizedMessage ; constructor ( private _translationFile , private _transUnit ) { this . _isDirty = false ; } public translationFile ( ) { return this . _translationFile ; } public id ( ) { if ( this . _transUnit ) { return this . _transUnit . id ; } else { return null ; } } public sourceContent ( ) { if ( this . _transUnit ) { return this . _transUnit . sourceContent ( ) ; } else { return null ; } } public sourceContentNormalized ( ) { if ( this . _transUnit ) { if ( ! this . _normalizedSourceContent ) { const original = this . _transUnit . sourceContent ( ) ; let normalizedMessage = null ; let parseError = null ; try { normalizedMessage = this . _transUnit . sourceContentNormalized ( ) ; } catch ( error ) { parseError = error . message ; } this . _normalizedSourceContent = new NormalizedMessage ( original , normalizedMessage , parseError , normalizedMessage ) ; } return this . _normalizedSourceContent ; } else { return null ; } } public targetContent ( ) { if ( this . _transUnit ) { return this . _transUnit . targetContent ( ) ; } else { return null ; } } public targetContentNormalized ( ) { if ( this . _transUnit ) { if ( ! this . _normalizedTargetContent ) { const original = this . _transUnit . targetContent ( ) ; let normalizedMessage = null ; let parseError = null ; try { normalizedMessage = this . _transUnit . targetContentNormalized ( ) ; } catch ( error ) { parseError = error . message ; } this . _normalizedTargetContent = new NormalizedMessage ( original , normalizedMessage , parseError , this . _transUnit . sourceContentNormalized ( ) ) ; } return this . _normalizedTargetContent ; } else { return null ; } } public description ( ) { if ( this . _transUnit ) { return this . _transUnit . description ( ) ; } else { return null ; } } public meaning ( ) { if ( this . _transUnit ) { return this . _transUnit . meaning ( ) ; } else { return null ; } } public sourceReferences ( ) { if ( this . _transUnit ) { return this . _transUnit . sourceReferences ( ) ; } else { return null ; } } public targetState ( ) { if ( this . _transUnit ) { return this . _transUnit . targetState ( ) ; } else { return null ; } } public setTargetState ( newState ) { if ( this . _transUnit ) { this . _transUnit . setTargetState ( newState ) ; } } public isDirty ( ) { return this . _isDirty ; } public isTranslated ( ) { return this . targetState ( ) && this . targetState ( ) !== STATE_NEW ; } public translate ( newTranslation ) { if ( this . _transUnit ) { this . _transUnit . translate ( newTranslation . nativeString ( ) ) ; this . _isDirty = true ; this . _normalizedSourceContent = null ; this . _normalizedTargetContent = null ; } } public autoTranslateNonICUUnit ( translatedMessage ) { return this . autoTranslate ( this . sourceContentNormalized ( ) . translate ( translatedMessage , true ) ) ; } public autoTranslateICUUnit ( translation ) { return this . autoTranslate ( this . sourceContentNormalized ( ) . translateICUMessage ( translation ) ) ; } param translatedMessage return { AutoTranslateResult } public autoTranslate ( translatedMessage ) { const errors = translatedMessage . validate ( true ) ; const warnings = translatedMessage . validateWarnings ( true ) ; if ( ! isNullOrUndefined ( errors ) ) { return AutoTranslateResult . Failed ( this , format ( 'errors detected, not translated: %s' , JSON . stringify ( errors ) ) ) ; } else if ( ! isNullOrUndefined ( warnings ) ) { return AutoTranslateResult . Failed ( this , format ( 'warnings detected, not translated: %s' , JSON . stringify ( warnings ) ) ) ; } else { this . translate ( translatedMessage ) ; return AutoTranslateResult . Success ( this ) ; } } }