import { STATE_TRANSLATED , ITranslationMessagesFile , INormalizedMessage , ITransUnit , INote } from './internalapi' ; import { AbstractTranslationMessagesFile } from './abstract-translation-messages-file' ; import { isNullOrUndefined , isString } from 'util' ; import { ParsedMessage } from './parsed-message' ; import { AbstractMessageParser } from './abstract-message-parser' ; /**
 * Created by roobm on 10.05.2017.
 * Abstract superclass for all implementations of ITransUnit.
 */ export abstract class AbstractTransUnit implements ITransUnit { private _sourceContentNormalized : ParsedMessage ; protected constructor ( protected _element , protected _id , protected _translationMessagesFile ) { } public get id ( ) : string { return this . _id ; } /**
     * The file the unit belongs to.,
     */ translationMessagesFile ( ) { return this . _translationMessagesFile ; } /**
     * The original text value, that is to be translated.
     * Contains all markup, depends on the concrete format used.
     */ abstract sourceContent ( ) ; /**
     * 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 true ; } param newContent abstract setSourceContent ( newContent ) ; /**
     * The original text value, that is to be translated, as normalized message.
     */ public sourceContentNormalized ( ) { if ( isNullOrUndefined ( this . _sourceContentNormalized ) ) { this . _sourceContentNormalized = this . createSourceContentNormalized ( ) ; } return this . _sourceContentNormalized ; } /**
     * The original text value, that is to be translated, as normalized message.
     */ abstract createSourceContentNormalized ( ) ; /**
     * The translated value.
     * Contains all markup, depends on the concrete format used.
     */ abstract targetContent ( ) ; /**
     * The translated value as normalized message.
     * All placeholders are replaced with {{n}} (starting at 0)
     * and all embedded html is replaced by direct html markup.
     */ abstract targetContentNormalized ( ) ; /**
     * State of the translation as stored in the xml.
     */ abstract nativeTargetState ( ) ; /**
     * State of the translation.
     * (on of new, translated, final)
     * Return values are defined as Constants STATE_...
     */ public targetState ( ) { const nativeState = this . nativeTargetState ( ) ; return this . mapNativeStateToState ( nativeState ) ; } param state returns throws protected abstract mapStateToNativeState ( state ) ; param nativeState protected abstract mapNativeStateToState ( nativeState ) ; param nativeState protected abstract setNativeTargetState ( nativeState ) ; param newState setTargetState ( newState ) { this . setNativeTargetState ( this . mapStateToNativeState ( newState ) ) ; if ( this . translationMessagesFile ( ) instanceof AbstractTranslationMessagesFile ) { ( < AbstractTranslationMessagesFile > this . translationMessagesFile ( ) ) . countNumbers ( ) ; } } /**
     * 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.
     */ abstract sourceReferences ( ) ; /**
     * 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 true ; } param sourceRefs abstract setSourceReferences ( sourceRefs ) ; /**
     * The description set in the template as value of the i18n-attribute.
     * e.g. i18n="mydescription".
     */ abstract description ( ) ; /**
     * 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".
     */ abstract meaning ( ) ; /**
     * 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 true ; } param description abstract setDescription ( description ) ; param meaning abstract setMeaning ( meaning ) ; /**
     * Get all notes of the trans-unit.
     * Notes are remarks made by a translator.
     * (description and meaning are not included here!)
     */ abstract notes ( ) ; /**
     * Test, wether setting of notes is supported.
     * If not, setNotes will do nothing.
     * xtb does not support this, all other formats do.
     */ abstract supportsSetNotes ( ) ; param newNotes throws abstract setNotes ( newNotes ) ; param newNotes throws protected checkNotes ( newNotes ) { const errorInFromNote = newNotes . find ( ( note ) => note . from === 'description' || note . from === 'meaning' ) ; if ( ! isNullOrUndefined ( errorInFromNote ) ) { throw new Error ( 'description or meaning are not allowed as from atttribute' ) ; } } return public asXmlElement ( ) { return this . _element ; } /**
     * 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)
     */ abstract cloneWithSourceAsTarget ( isDefaultLang , copyContent , targetFile ) ; /**
     * Copy source to target to use it as dummy translation.
     * (internal usage only, a client should call createTranslationFileForLang on ITranslationMessageFile)
     */ abstract useSourceAsTarget ( isDefaultLang , copyContent ) ; param translation public translate ( translation ) { let translationNative ; if ( isString ( translation ) ) { translationNative = < string > translation ; } else { translationNative = ( < INormalizedMessage > translation ) . asNativeString ( ) ; } this . translateNative ( translationNative ) ; this . setTargetState ( STATE_TRANSLATED ) ; } /**
     * Return a parser used for normalized messages.
     */ protected abstract messageParser ( ) ; param message return public isICUMessage ( message ) { return this . messageParser ( ) . isICUMessageStart ( message ) ; } param translation protected abstract translateNative ( translation ) ; }