1
1
import { SFCDescriptor , SFCBlock } from 'vue-template-compiler'
2
- import { Locale , MetaLocaleMessage , SFCI18nBlock , SFCFileInfo } from '../types'
2
+ import { Locale , MetaLocaleMessage , SFCI18nBlock , SFCFileInfo , FormatOptions } from '../types'
3
3
4
4
import { escape , reflectSFCDescriptor , parseContent , stringifyContent } from './utils'
5
5
6
6
import { debug as Debug } from 'debug'
7
7
const debug = Debug ( 'vue-i18n-locale-message:infuser' )
8
8
9
- export default function infuse ( basePath : string , sources : SFCFileInfo [ ] , meta : MetaLocaleMessage ) : SFCFileInfo [ ] {
9
+ export default function infuse ( basePath : string , sources : SFCFileInfo [ ] , meta : MetaLocaleMessage , options ?: FormatOptions ) : SFCFileInfo [ ] {
10
10
const descriptors = reflectSFCDescriptor ( basePath , sources )
11
11
12
12
return descriptors . map ( descriptor => {
13
13
return {
14
- content : generate ( meta , descriptor ) ,
14
+ content : generate ( meta , descriptor , options ) ,
15
15
path : descriptor . contentPath
16
16
} as SFCFileInfo
17
17
} )
18
18
}
19
19
20
- function generate ( meta : MetaLocaleMessage , descriptor : SFCDescriptor ) : string {
20
+ function generate ( meta : MetaLocaleMessage , descriptor : SFCDescriptor , options ?: FormatOptions ) : string {
21
21
const i18nBlocks = meta . components [ descriptor . contentPath ]
22
22
debug ( 'target i18n blocks\n' , i18nBlocks )
23
23
24
24
const blocks : SFCBlock [ ] = getBlocks ( descriptor )
25
25
blocks . forEach ( b => debug ( `block: type=${ b . type } , start=${ b . start } , end=${ b . end } ` ) )
26
26
27
27
const { raw } = descriptor
28
- const content = buildContent ( i18nBlocks , raw , blocks )
28
+ const content = buildContent ( i18nBlocks , raw , blocks , options )
29
29
debug ( `build content:\n${ content } ` )
30
30
debug ( `content size: raw=${ raw . length } , content=${ content . length } ` )
31
31
@@ -41,7 +41,7 @@ function getBlocks (descriptor: SFCDescriptor): SFCBlock[] {
41
41
return blocks
42
42
}
43
43
44
- function buildContent ( i18nBlocks : SFCI18nBlock [ ] , raw : string , blocks : SFCBlock [ ] ) : string {
44
+ function buildContent ( i18nBlocks : SFCI18nBlock [ ] , raw : string , blocks : SFCBlock [ ] , options ?: FormatOptions ) : string {
45
45
let offset = 0
46
46
let i18nBlockCounter = 0
47
47
let contents : string [ ] = [ ]
@@ -67,7 +67,7 @@ function buildContent (i18nBlocks: SFCI18nBlock[], raw: string, blocks: SFCBlock
67
67
}
68
68
69
69
contents = contents . concat ( raw . slice ( offset , block . start ) )
70
- const serialized = `\n${ stringifyContent ( messages , lang ) } `
70
+ const serialized = `\n${ stringifyContent ( messages , lang , options ) } `
71
71
contents = contents . concat ( serialized )
72
72
offset = block . end as number
73
73
i18nBlockCounter ++
@@ -81,15 +81,15 @@ function buildContent (i18nBlocks: SFCI18nBlock[], raw: string, blocks: SFCBlock
81
81
82
82
if ( i18nBlocks . length > i18nBlockCounter ) {
83
83
i18nBlocks . slice ( i18nBlockCounter ) . reduce ( ( contents , i18nBlock ) => {
84
- contents . push ( buildI18nTag ( i18nBlock ) )
84
+ contents . push ( buildI18nTag ( i18nBlock , options ) )
85
85
return contents
86
86
} , contents )
87
87
}
88
88
89
89
return contents . join ( '' )
90
90
}
91
91
92
- function buildI18nTag ( i18nBlock : SFCI18nBlock ) : string {
92
+ function buildI18nTag ( i18nBlock : SFCI18nBlock , options ?: FormatOptions ) : string {
93
93
const { locale, lang, messages } = i18nBlock
94
94
let tag = '<i18n'
95
95
if ( locale ) {
@@ -102,5 +102,5 @@ function buildI18nTag (i18nBlock: SFCI18nBlock): string {
102
102
103
103
return `\n
104
104
${ tag }
105
- ${ stringifyContent ( locale ? messages [ locale ] : messages , lang ) } </i18n>`
105
+ ${ stringifyContent ( locale ? messages [ locale ] : messages , lang , options ) } </i18n>`
106
106
}
0 commit comments