@@ -42,6 +42,8 @@ export interface AugmentIndexHtmlOptions {
4242 loadOutputFile : LoadOutputFileFunctionType ;
4343 /** Used to sort the inseration of files in the HTML file */
4444 entrypoints : string [ ] ;
45+ /** Used to set the document default locale */
46+ lang ?: string ;
4547}
4648
4749export interface FileInfo {
@@ -56,6 +58,7 @@ export interface FileInfo {
5658 * after processing several configurations in order to build different sets of
5759 * bundles for differential serving.
5860 */
61+ // tslint:disable-next-line: no-big-function
5962export async function augmentIndexHtml ( params : AugmentIndexHtmlOptions ) : Promise < string > {
6063 const { loadOutputFile, files, noModuleFiles = [ ] , moduleFiles = [ ] , entrypoints } = params ;
6164
@@ -91,8 +94,10 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise
9194 const document = parse5 . parse ( params . inputContent , { treeAdapter, locationInfo : true } ) ;
9295 let headElement ;
9396 let bodyElement ;
97+ let htmlElement ;
9498 for ( const docChild of document . childNodes ) {
9599 if ( docChild . tagName === 'html' ) {
100+ htmlElement = docChild ;
96101 for ( const htmlChild of docChild . childNodes ) {
97102 if ( htmlChild . tagName === 'head' ) {
98103 headElement = htmlChild ;
@@ -241,6 +246,33 @@ export async function augmentIndexHtml(params: AugmentIndexHtmlOptions): Promise
241246
242247 indexSource . insert ( styleInsertionPoint , parse5 . serialize ( styleElements , { treeAdapter } ) ) ;
243248
249+ // Adjust document locale if specified
250+ if ( typeof params . lang == 'string' ) {
251+
252+ const htmlFragment = treeAdapter . createDocumentFragment ( ) ;
253+
254+ let langAttribute ;
255+ for ( const attribute of htmlElement . attrs ) {
256+ if ( attribute . name === 'lang' ) {
257+ langAttribute = attribute ;
258+ }
259+ }
260+ if ( langAttribute ) {
261+ langAttribute . value = params . lang ;
262+ } else {
263+ htmlElement . attrs . push ( { name : 'lang' , value : params . lang } ) ;
264+ }
265+ // we want only openning tag
266+ htmlElement . childNodes = [ ] ;
267+
268+ treeAdapter . appendChild ( htmlFragment , htmlElement ) ;
269+ indexSource . replace (
270+ htmlElement . __location . startTag . startOffset ,
271+ htmlElement . __location . startTag . endOffset - 1 ,
272+ parse5 . serialize ( htmlFragment , { treeAdapter } ) . replace ( '</html>' , '' ) ,
273+ ) ;
274+ }
275+
244276 return indexSource . source ( ) ;
245277}
246278
0 commit comments