/**
 * Created by roobm on 21.03.2017.
 */ import { ITranslationMessagesFile } from './i-translation-messages-file' ; import { XliffFile } from '../impl/xliff-file' ; import { XmbFile } from '../impl/xmb-file' ; import { format } from 'util' ; import { Xliff2File } from '../impl/xliff2-file' ; import { FORMAT_XLIFF12 , FORMAT_XLIFF20 , FORMAT_XMB , FORMAT_XTB } from './constants' ; import { XtbFile } from '../impl/xtb-file' ; import { ITranslationMessagesFileFactory } from './i-translation-messages-file-factory' ; /**
 * Helper class to read translation files depending on format.
 * This is part of the public api
 */ export class TranslationMessagesFileFactory implements ITranslationMessagesFileFactory { param i18nFormat param xmlContent param path param encoding param optionalMaster return public static fromFileContent ( i18nFormat , xmlContent , path , encoding , optionalMaster ? ) { return new TranslationMessagesFileFactory ( ) . createFileFromFileContent ( i18nFormat , xmlContent , path , encoding , optionalMaster ) ; } param xmlContent param path param encoding param optionalMaster return public static fromUnknownFormatFileContent ( xmlContent , path , encoding , optionalMaster ? ) { return new TranslationMessagesFileFactory ( ) . createFileFromUnknownFormatFileContent ( xmlContent , path , encoding , optionalMaster ) ; } param i18nFormat param xmlContent param path param encoding param optionalMaster return createFileFromFileContent ( i18nFormat , xmlContent , path , encoding , optionalMaster ? ) { if ( i18nFormat === FORMAT_XLIFF12 ) { return new XliffFile ( xmlContent , path , encoding ) ; } if ( i18nFormat === FORMAT_XLIFF20 ) { return new Xliff2File ( xmlContent , path , encoding ) ; } if ( i18nFormat === FORMAT_XMB ) { return new XmbFile ( this , xmlContent , path , encoding ) ; } if ( i18nFormat === FORMAT_XTB ) { return new XtbFile ( this , xmlContent , path , encoding , optionalMaster ) ; } throw new Error ( format ( 'oops, unsupported format "%s"' , i18nFormat ) ) ; } param xmlContent param path param encoding param optionalMaster return createFileFromUnknownFormatFileContent ( xmlContent , path , encoding , optionalMaster ? ) { let formatCandidates = [ FORMAT_XLIFF12 , FORMAT_XLIFF20 , FORMAT_XMB , FORMAT_XTB ] ; if ( path && path . endsWith ( 'xmb' ) ) { formatCandidates = [ FORMAT_XMB , FORMAT_XTB , FORMAT_XLIFF12 , FORMAT_XLIFF20 ] ; } if ( path && path . endsWith ( 'xtb' ) ) { formatCandidates = [ FORMAT_XTB , FORMAT_XMB , FORMAT_XLIFF12 , FORMAT_XLIFF20 ] ; } for ( let i = 0 ; i < formatCandidates . length ; i ++ ) { const formatCandidate = formatCandidates [ i ] ; try { const translationFile = TranslationMessagesFileFactory . fromFileContent ( formatCandidate , xmlContent , path , encoding , optionalMaster ) ; if ( translationFile ) { return translationFile ; } } catch ( e ) { } } throw new Error ( format ( 'could not identify file format, it is neiter XLIFF (1.2 or 2.0) nor XMB/XTB' ) ) ; } }