import { isNullOrUndefined } from 'util' ; import { ITranslationMessagesFile } from '../api/i-translation-messages-file' ; import { INormalizedMessage } from '../api/i-normalized-message' ; import { ITransUnit } from '../api/i-trans-unit' ; import { INote } from '../api/i-note' ; import { DOMUtilities } from './dom-utilities' ; import { AbstractTransUnit } from './abstract-trans-unit' ; import { XmbMessageParser } from './xmb-message-parser' ; import { ParsedMessage } from './parsed-message' ; import { AbstractMessageParser } from './abstract-message-parser' ; /**
 * Created by martin on 23.05.2017.
 * A Translation Unit in an XTB file.
 */ export class XtbTransUnit extends AbstractTransUnit implements ITransUnit { private _sourceTransUnitFromMaster : AbstractTransUnit ; constructor ( _element , _id , _translationMessagesFile , _sourceTransUnitFromMaster ) { super ( _element , _id , _translationMessagesFile ) ; this . _sourceTransUnitFromMaster = _sourceTransUnitFromMaster ; } return public sourceContent ( ) { if ( this . _sourceTransUnitFromMaster ) { return this . _sourceTransUnitFromMaster . sourceContent ( ) ; } else { return null ; } } /**
     * Test, wether setting of source content is supported.
     * If not, setSourceContent in trans-unit will do nothing.
     * xtb does not support this, all other formats do.
     */ supportsSetSourceContent ( ) { return false ; } param newContent public setSourceContent ( newContent ) { } /**
     * Return a parser used for normalized messages.
     */ protected messageParser ( ) { return new XmbMessageParser ( ) ; } /**
     * The original text value, that is to be translated, as normalized message.
     */ public createSourceContentNormalized ( ) { if ( this . _sourceTransUnitFromMaster ) { return this . _sourceTransUnitFromMaster . createSourceContentNormalized ( ) ; } else { return null ; } } /**
     * the translated value (containing all markup, depends on the concrete format used).
     */ public targetContent ( ) { return DOMUtilities . getXMLContent ( this . _element ) ; } /**
     * the translated value, but all placeholders are replaced with {{n}} (starting at 0)
     * and all embedded html is replaced by direct html markup.
     */ targetContentNormalized ( ) { return this . messageParser ( ) . createNormalizedMessageFromXML ( this . _element , this . sourceContentNormalized ( ) ) ; } /**
     * State of the translation.
     * (not supported in xmb)
     * If we have a master, we assumed it is translated if the content is not the same as the masters one.
     */ public nativeTargetState ( ) { if ( this . _sourceTransUnitFromMaster ) { const sourceContent = this . _sourceTransUnitFromMaster . sourceContent ( ) ; if ( ! sourceContent || sourceContent === this . targetContent ( ) || ! this . targetContent ( ) ) { return 'new' ; } else { return 'final' ; } } return null ; } param state returns throws protected mapStateToNativeState ( state ) { return state ; } param nativeState protected mapNativeStateToState ( nativeState ) { return nativeState ; } param nativeState protected setNativeTargetState ( nativeState ) { } /**
     * All the source elements in the trans unit.
     * The source element is a reference to the original template.
     * It contains the name of the template file and a line number with the position inside the template.
     * It is just a help for translators to find the context for the translation.
     * This is set when using Angular 4.0 or greater.
     * Otherwise it just returns an empty array.
     */ public sourceReferences ( ) { if ( this . _sourceTransUnitFromMaster ) { return this . _sourceTransUnitFromMaster . sourceReferences ( ) ; } else { return [ ] ; } } /**
     * Test, wether setting of source refs is supported.
     * If not, setSourceReferences will do nothing.
     * xtb does not support this, all other formats do.
     */ public supportsSetSourceReferences ( ) { return false ; } param sourceRefs public setSourceReferences ( sourceRefs ) { } /**
     * The description set in the template as value of the i18n-attribute.
     * e.g. i18n="mydescription".
     * In xtb only the master stores it.
     */ public description ( ) { if ( this . _sourceTransUnitFromMaster ) { return this . _sourceTransUnitFromMaster . description ( ) ; } else { return null ; } } /**
     * The meaning (intent) set in the template as value of the i18n-attribute.
     * This is the part in front of the | symbol.
     * e.g. i18n="meaning|mydescription".
     * In xtb only the master stores it.
     */ public meaning ( ) { if ( this . _sourceTransUnitFromMaster ) { return this . _sourceTransUnitFromMaster . meaning ( ) ; } else { return null ; } } /**
     * Test, wether setting of description and meaning is supported.
     * If not, setDescription and setMeaning will do nothing.
     * xtb does not support this, all other formats do.
     */ public supportsSetDescriptionAndMeaning ( ) { return false ; } param description public setDescription ( description ) { } param meaning public setMeaning ( meaning ) { } /**
     * Get all notes of the trans-unit.
     * There are NO notes in xmb/xtb
     */ public notes ( ) { return [ ] ; } /**
     * Test, wether setting of notes is supported.
     * If not, setNotes will do nothing.
     * xtb does not support this, all other formats do.
     */ public supportsSetNotes ( ) { return false ; } param newNotes public setNotes ( newNotes ) { } /**
     * Copy source to target to use it as dummy translation.
     * Returns a changed copy of this trans unit.
     * receiver is not changed.
     * (internal usage only, a client should call importNewTransUnit on ITranslationMessageFile)
     * In xtb there is nothing to do, because there is only a target, no source.
     */ public cloneWithSourceAsTarget ( isDefaultLang , copyContent , targetFile ) { return this ; } /**
     * Copy source to target to use it as dummy translation.
     * (internal usage only, a client should call createTranslationFileForLang on ITranslationMessageFile)
     */ public useSourceAsTarget ( isDefaultLang , copyContent ) { } param translation protected translateNative ( translation ) { const target = this . _element ; if ( isNullOrUndefined ( translation ) ) { translation = '' ; } DOMUtilities . replaceContentWithXMLContent ( target , translation ) ; } }