@@ -20,7 +20,6 @@ import {ProgramMessage, Message, Bundle, Placeholder} from '../messages';
2020import {
2121 getOneElementByTagNameOrThrow ,
2222 getNonEmptyAttributeOrThrow ,
23- formatXml ,
2423} from './xml-utils' ;
2524
2625/**
@@ -147,12 +146,16 @@ class XlbFormatter implements Formatter {
147146 */
148147 async writeOutput ( sourceMessages : ProgramMessage [ ] ) : Promise < void > {
149148 const doc = new xmldom . DOMImplementation ( ) . createDocument ( '' , '' , null ) ;
149+ const indent = ( node : Element | Document , level = 0 ) =>
150+ node . appendChild ( doc . createTextNode ( '\n' + Array ( level + 1 ) . join ( ' ' ) ) ) ;
150151 doc . appendChild (
151152 doc . createProcessingInstruction ( 'xml' , 'version="1.0" encoding="UTF-8"' )
152153 ) ;
154+ indent ( doc ) ;
153155 const bundle = doc . createElement ( 'localizationbundle' ) ;
154156 bundle . setAttribute ( 'locale' , this . config . sourceLocale ) ;
155157 doc . appendChild ( bundle ) ;
158+ indent ( bundle , 1 ) ;
156159 const messagesNode = doc . createElement ( 'messages' ) ;
157160 bundle . appendChild ( messagesNode ) ;
158161 for ( const { name, contents, descStack} of sourceMessages ) {
@@ -161,6 +164,7 @@ class XlbFormatter implements Formatter {
161164 if ( descStack . length > 0 ) {
162165 messageNode . setAttribute ( 'desc' , descStack . join ( ' / ' ) ) ;
163166 }
167+ indent ( messagesNode , 2 ) ;
164168 messagesNode . appendChild ( messageNode ) ;
165169 for ( const content of contents ) {
166170 if ( typeof content === 'string' ) {
@@ -173,57 +177,14 @@ class XlbFormatter implements Formatter {
173177 }
174178 }
175179 }
180+ indent ( messagesNode , 1 ) ;
181+ indent ( bundle ) ;
182+ indent ( doc ) ;
176183 const serialized = new xmldom . XMLSerializer ( ) . serializeToString ( doc ) ;
177- const formatted = formatXml ( serialized ) ;
178184 await fsExtra . writeFile (
179185 this . config . resolve ( this . xlbConfig . outputFile ) ,
180- formatted ,
186+ serialized ,
181187 'utf8'
182188 ) ;
183189 }
184190}
185-
186- /**
187- * Generate an XLB XML file for the given messages. This file contains the
188- * canonical set of messages that will be translatd.
189- */
190- export function generateXlb (
191- messages : ProgramMessage [ ] ,
192- locale : Locale
193- ) : string {
194- const doc = new xmldom . DOMImplementation ( ) . createDocument ( '' , '' , null ) ;
195- doc . appendChild (
196- doc . createProcessingInstruction ( 'xml' , 'version="1.0" encoding="UTF-8"' )
197- ) ;
198- doc . appendChild ( doc . createTextNode ( '\n' ) ) ;
199- const bundle = doc . createElement ( 'localizationbundle' ) ;
200- bundle . setAttribute ( 'locale' , locale ) ;
201- doc . appendChild ( bundle ) ;
202- bundle . appendChild ( doc . createTextNode ( '\n ' ) ) ;
203- const messagesNode = doc . createElement ( 'messages' ) ;
204- bundle . appendChild ( messagesNode ) ;
205- for ( const { name, contents, descStack} of messages ) {
206- messagesNode . appendChild ( doc . createTextNode ( '\n ' ) ) ;
207- const messageNode = doc . createElement ( 'msg' ) ;
208- messageNode . setAttribute ( 'name' , name ) ;
209- if ( descStack . length > 0 ) {
210- messageNode . setAttribute ( 'desc' , descStack . join ( ' / ' ) ) ;
211- }
212- messagesNode . appendChild ( messageNode ) ;
213- for ( const content of contents ) {
214- if ( typeof content === 'string' ) {
215- messageNode . appendChild ( doc . createTextNode ( content ) ) ;
216- } else {
217- const { untranslatable} = content ;
218- const ph = doc . createElement ( 'ph' ) ;
219- ph . appendChild ( doc . createTextNode ( untranslatable ) ) ;
220- messageNode . appendChild ( ph ) ;
221- }
222- }
223- }
224- messagesNode . appendChild ( doc . createTextNode ( '\n ' ) ) ;
225- bundle . appendChild ( doc . createTextNode ( '\n' ) ) ;
226- doc . appendChild ( doc . createTextNode ( '\n' ) ) ;
227- const serialized = new xmldom . XMLSerializer ( ) . serializeToString ( doc ) ;
228- return serialized ;
229- }
0 commit comments