@@ -20,7 +20,6 @@ import {Bundle, Message, ProgramMessage, Placeholder} from '../messages';
2020import {
2121 getOneElementByTagNameOrThrow ,
2222 getNonEmptyAttributeOrThrow ,
23- formatXml ,
2423} from './xml-utils' ;
2524
2625/**
@@ -187,10 +186,12 @@ export class XliffFormatter implements Formatter {
187186 }
188187
189188 const doc = new xmldom . DOMImplementation ( ) . createDocument ( '' , '' , null ) ;
189+ const indent = ( node : Element | Document , level = 0 ) =>
190+ node . appendChild ( doc . createTextNode ( '\n' + Array ( level + 1 ) . join ( ' ' ) ) ) ;
190191 doc . appendChild (
191192 doc . createProcessingInstruction ( 'xml' , 'version="1.0" encoding="UTF-8"' )
192193 ) ;
193- doc . appendChild ( doc . createTextNode ( '\n' ) ) ;
194+ indent ( doc ) ;
194195
195196 // https://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#xliff
196197 const xliff = doc . createElement ( 'xliff' ) ;
@@ -201,27 +202,32 @@ export class XliffFormatter implements Formatter {
201202 'urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-strict.xsd'
202203 ) ;
203204 doc . appendChild ( xliff ) ;
205+ indent ( xliff ) ;
204206
205207 // https://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#file
206208 const file = doc . createElement ( 'file' ) ;
207209 xliff . appendChild ( file ) ;
210+ file . setAttribute ( 'target-language' , targetLocale ) ;
211+ file . setAttribute ( 'source-language' , this . config . sourceLocale ) ;
208212 // TODO The spec requires the source filename in the "original" attribute,
209213 // but we don't currently track filenames.
210214 file . setAttribute ( 'original' , 'lit-localize-inputs' ) ;
211215 // Plaintext seems right, as opposed to HTML, since our translatable
212216 // message text is just text, and all HTML markup is encoded into <ph>
213217 // elements.
214218 file . setAttribute ( 'datatype' , 'plaintext' ) ;
215- file . setAttribute ( 'source-language' , this . config . sourceLocale ) ;
216- file . setAttribute ( 'target-language' , targetLocale ) ;
219+ indent ( file ) ;
217220
221+ // https://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#body
218222 const body = doc . createElement ( 'body' ) ;
219223 file . appendChild ( body ) ;
224+ indent ( body ) ;
220225
221226 for ( const { name, contents : sourceContents , descStack} of sourceMessages ) {
222227 // https://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#trans-unit
223228 const transUnit = doc . createElement ( 'trans-unit' ) ;
224229 body . appendChild ( transUnit ) ;
230+ indent ( transUnit , 1 ) ;
225231 transUnit . setAttribute ( 'id' , name ) ;
226232
227233 if ( descStack . length > 0 ) {
@@ -245,12 +251,18 @@ export class XliffFormatter implements Formatter {
245251 for ( const child of this . encodeContents ( doc , translation . contents ) ) {
246252 target . appendChild ( child ) ;
247253 }
254+ indent ( transUnit , 1 ) ;
248255 transUnit . appendChild ( target ) ;
249256 }
257+ indent ( transUnit ) ;
258+ indent ( body ) ;
250259 }
260+ indent ( file ) ;
261+ indent ( xliff ) ;
262+ indent ( doc ) ;
251263 const serializer = new xmldom . XMLSerializer ( ) ;
252264 const xmlStr = serializer . serializeToString ( doc ) ;
253- return formatXml ( xmlStr ) ;
265+ return xmlStr ;
254266 }
255267
256268 /**
0 commit comments