@@ -319,6 +319,7 @@ export async function loadNamespaceDictionary (path: string) {
319
319
type ParsedLocaleMessagePathInfo = {
320
320
locale : Locale
321
321
filename ?: string
322
+ base ?: string
322
323
}
323
324
324
325
function getLocaleMessagePathInfo ( fullPath : string , bundleMatch ?: string ) : ParsedLocaleMessagePathInfo {
@@ -329,16 +330,18 @@ function getLocaleMessagePathInfo (fullPath: string, bundleMatch?: string): Pars
329
330
const match = re . exec ( fullPath )
330
331
debug ( 'getLocaleMessagePathInfo: regex match' , match )
331
332
if ( ! match ) {
332
- return { locale : '' , filename : '' }
333
+ return { locale : '' , filename : '' , base : '' }
333
334
} else {
334
335
if ( match . groups ) {
335
336
const locale = match . groups . locale || ''
336
- const filename = match . groups . filename || ''
337
- return { locale, filename }
337
+ const filename = match . groups . filename || match . groups . basekey || ''
338
+ const base = match . groups . base || ''
339
+ return { locale, filename, base }
338
340
} else {
339
341
return {
340
342
locale : match [ 1 ] ? match [ 1 ] : '' ,
341
- filename : match [ 2 ] ? match [ 2 ] : ''
343
+ filename : match [ 2 ] ? match [ 2 ] : '' ,
344
+ base : ''
342
345
}
343
346
}
344
347
}
@@ -389,6 +392,7 @@ type ExternalLocaleMessagesParseInfo = {
389
392
namespace : string
390
393
locale : Locale
391
394
filename ?: string
395
+ base ?: string
392
396
}
393
397
394
398
// TODO: should be selected more other library ...
@@ -406,18 +410,50 @@ export function splitLocaleMessages (
406
410
if ( ! bundle ) { return { sfc : messages } }
407
411
408
412
const bundleTargetPaths = bundle . split ( ',' ) . filter ( p => p )
409
- const externalLocaleMessagesParseInfo = bundleTargetPaths . reduce ( ( info , targetPath ) => {
413
+ const externalExists = bundleTargetPaths . reduce ( ( info , targetPath ) => {
410
414
const namespace = dictionary [ targetPath ] || ''
411
415
const globedPaths = glob . sync ( targetPath ) . map ( p => resolve ( p ) )
412
416
debug ( 'splitLocaleMessages globedPaths' , globedPaths )
413
417
return globedPaths . reduce ( ( info , fullPath ) => {
414
- const { locale, filename } = getLocaleMessagePathInfo ( fullPath , bundleMatch )
418
+ const { locale, filename, base } = getLocaleMessagePathInfo ( fullPath , bundleMatch )
415
419
if ( ! locale ) { return info }
416
- info . push ( { path : fullPath , locale, namespace, filename } )
420
+ info . set ( fullPath , { path : fullPath , locale, namespace, filename, base } )
417
421
return info
418
422
} , info )
423
+ } , new Map < string , ExternalLocaleMessagesParseInfo > ( ) )
424
+ const _externalExists = [ ...externalExists . values ( ) ]
425
+ debug ( 'splitLocaleMessages: externalLocaleMessagesParseInfo (exisit):' , _externalExists )
426
+
427
+ const locales = Object . keys ( messages )
428
+ const externalAdditional = _externalExists . reduce ( ( ext , { path, locale, namespace, filename, base } ) => {
429
+ const additionalLocales = locales . filter ( l => l !== locale )
430
+ additionalLocales . forEach ( l => {
431
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
432
+ if ( bundleMatch && filename && base && messages [ l ] && ( messages [ l ] as any ) [ namespace ] && ( ( messages [ l ] as any ) [ namespace ] as any ) [ filename ] ) {
433
+ const re = new RegExp ( bundleMatch , 'ig' )
434
+ const match = re . exec ( path )
435
+ debug ( 'splitLocaleMessages: regex match' , match )
436
+ if ( match && match . groups ) {
437
+ const groupLen = Object . keys ( match . groups ) . length
438
+ let buildingPath = ''
439
+ for ( let i = 0 ; i < groupLen ; i ++ ) {
440
+ if ( match [ i + 1 ] ) {
441
+ buildingPath = `${ buildingPath } ${ match [ i + 1 ] === filename ? filename : match [ i + 1 ] === l ? l : match [ i + 1 ] } /`
442
+ }
443
+ if ( i === groupLen - 1 ) {
444
+ buildingPath = `${ buildingPath } .json`
445
+ }
446
+ }
447
+ ext . push ( { path : buildingPath , locale : l , namespace, filename, base } )
448
+ }
449
+ }
450
+ } )
451
+ return ext
419
452
} , [ ] as ExternalLocaleMessagesParseInfo [ ] )
420
- debug ( 'splitLocaleMessages: externalLocaleMessagesParseInfo:' , externalLocaleMessagesParseInfo )
453
+ debug ( 'splitLocaleMessages: externalLocaleMessagesParseInfo (addiotnal):' , externalAdditional )
454
+
455
+ const externalLocaleMessagesParseInfo = [ ..._externalExists , ...externalAdditional ]
456
+ debug ( 'splitLocaleMessages: externalLocaleMessagesParseInfo (added):' , externalLocaleMessagesParseInfo )
421
457
422
458
debug ( 'splitLocaleMessages: messages (before):' , messages )
423
459
const metaExternalLocaleMessages = externalLocaleMessagesParseInfo . reduce ( ( meta , { path, locale, namespace, filename } ) => {
0 commit comments