import { ITranslationMessagesFileFactory } from '../api/i-translation-messages-file-factory' ; import { ITranslationMessagesFile } from '../api/i-translation-messages-file' ; import { ITransUnit } from '../api/i-trans-unit' ; import { FORMAT_XMB , FILETYPE_XMB , FORMAT_XTB } from '../api/constants' ; import { format } from 'util' ; import { XmbTransUnit } from './xmb-trans-unit' ; import { AbstractTranslationMessagesFile } from './abstract-translation-messages-file' ; /**
 * Created by martin on 10.03.2017.
 * xmb-File access.
 */ /**
 * Doctype of xtb translation file corresponding with thos xmb file.
 */ export const XTB_DOCTYPE = `<!DOCTYPE translationbundle [
  <!ELEMENT translationbundle (translation)*>
  <!ATTLIST translationbundle lang CDATA #REQUIRED>
  <!ELEMENT translation (#PCDATA|ph)*>
  <!ATTLIST translation id CDATA #REQUIRED>
  <!ELEMENT ph EMPTY>
  <!ATTLIST ph name CDATA #REQUIRED>
]>` ; export class XmbFile extends AbstractTranslationMessagesFile implements ITranslationMessagesFile { param _translationMessageFileFactory param xmlString param path param encoding return constructor ( private _translationMessageFileFactory , xmlString , path , encoding ) { super ( ) ; this . _warnings = [ ] ; this . _numberOfTransUnitsWithMissingId = 0 ; this . initializeFromContent ( xmlString , path , encoding ) ; } private initializeFromContent ( xmlString , path , encoding ) { this . parseContent ( xmlString , path , encoding ) ; if ( this . _parsedDocument . getElementsByTagName ( 'messagebundle' ) . length !== 1 ) { throw new Error ( format ( 'File "%s" seems to be no xmb file (should contain a messagebundle element)' , path ) ) ; } return this ; } protected initializeTransUnits ( ) { this . transUnits = [ ] ; const transUnitsInFile = this . _parsedDocument . getElementsByTagName ( 'msg' ) ; for ( let i = 0 ; i < transUnitsInFile . length ; i ++ ) { const msg = transUnitsInFile . item ( i ) ; const id = msg . getAttribute ( 'id' ) ; if ( ! id ) { this . _warnings . push ( format ( 'oops, msg without "id" found in master, please check file %s' , this . _filename ) ) ; } this . transUnits . push ( new XmbTransUnit ( msg , id , this ) ) ; } } /**
     * File format as it is used in config files.
     * Currently 'xlf', 'xmb', 'xmb2'
     * Returns one of the constants FORMAT_..
     */ public i18nFormat ( ) { return FORMAT_XMB ; } /**
     * File type.
     * Here 'XMB'
     */ public fileType ( ) { return FILETYPE_XMB ; } /**
     * return tag names of all elements that have mixed content.
     * These elements will not be beautified.
     * Typical candidates are source and target.
     */ protected elementsWithMixedContent ( ) { return [ 'message' ] ; } return private guessLanguageFromFilename ( ) { if ( this . _filename ) { const parts = this . _filename . split ( '.' ) ; if ( parts . length > 2 && parts [ parts . length - 1 ] . toLowerCase ( ) === 'xmb' ) { return parts [ parts . length - 2 ] ; } } return null ; } return public sourceLanguage ( ) { return this . guessLanguageFromFilename ( ) ; } param language public setSourceLanguage ( language ) { } return public targetLanguage ( ) { return this . guessLanguageFromFilename ( ) ; } param language public setTargetLanguage ( language ) { } param foreignTransUnit param isDefaultLang param copyContent param importAfterElement return throws importNewTransUnit ( foreignTransUnit , isDefaultLang , copyContent , importAfterElement ? ) { throw Error ( 'xmb file cannot be used to store translations, use xtb file' ) ; } param lang param filename param isDefaultLang param copyContent public createTranslationFileForLang ( lang , filename , isDefaultLang , copyContent ) { const translationbundleXMLSource = '<?xml version="1.0" encoding="UTF-8"?>\n' + XTB_DOCTYPE + '\n<translationbundle>\n</translationbundle>\n' ; const translationFile = this . _translationMessageFileFactory . createFileFromFileContent ( FORMAT_XTB , translationbundleXMLSource , filename , this . encoding ( ) , { xmlContent : this . editedContent ( ) , path : this . filename ( ) , encoding : this . encoding ( ) } ) ; translationFile . setNewTransUnitTargetPraefix ( this . targetPraefix ) ; translationFile . setNewTransUnitTargetSuffix ( this . targetSuffix ) ; translationFile . setTargetLanguage ( lang ) ; translationFile . setNewTransUnitTargetPraefix ( this . getNewTransUnitTargetPraefix ( ) ) ; translationFile . setNewTransUnitTargetSuffix ( this . getNewTransUnitTargetSuffix ( ) ) ; this . forEachTransUnit ( ( tu ) => { translationFile . importNewTransUnit ( tu , isDefaultLang , copyContent ) ; } ) ; return translationFile ; } }