File tree Expand file tree Collapse file tree 2 files changed +43
-9
lines changed Expand file tree Collapse file tree 2 files changed +43
-9
lines changed Original file line number Diff line number Diff line change @@ -218,30 +218,37 @@ function getLocaleMessages(
218218 return ret
219219}
220220
221- function addPreCompileMessages (
221+ export function addPreCompileMessages (
222222 messages : LocaleMessages ,
223223 functions : MessageFunctions
224224) : void {
225225 const keys = Object . keys ( functions )
226226 keys . forEach ( key => {
227227 const compiled = functions [ key ]
228228 const { l, k } = JSON . parse ( key )
229- const targetLocaleMessage = ( messages [ l ] = messages [ l ] || { } )
229+ if ( ! messages [ l ] ) {
230+ messages [ l ] = { }
231+ }
232+ const targetLocaleMessage = messages [ l ]
230233 const paths = parsePath ( k )
231234 if ( paths != null ) {
232235 const len = paths . length
233236 let last = targetLocaleMessage as any // eslint-disable-line @typescript-eslint/no-explicit-any
234237 let i = 0
235238 while ( i < len ) {
236239 const path = paths [ i ]
237- const val = last [ path ]
238- if ( val != null ) {
239- last [ path ] = { }
240+ if ( i === len - 1 ) {
241+ last [ path ] = compiled
242+ break
243+ } else {
244+ let val = last [ path ]
245+ if ( ! val ) {
246+ last [ path ] = val = { }
247+ }
248+ last = val
249+ i ++
240250 }
241- last = val
242- i ++
243251 }
244- last = compiled
245252 }
246253 } )
247254}
Original file line number Diff line number Diff line change @@ -7,7 +7,12 @@ jest.mock('../src/utils', () => ({
77} ) )
88import { warn } from '../src/utils'
99
10- import { createComposer , MissingHandler } from '../src/composer'
10+ import {
11+ createComposer ,
12+ MissingHandler ,
13+ addPreCompileMessages
14+ } from '../src/composer'
15+ import { generateFormatCacheKey } from '../src/utils'
1116import { watch } from 'vue'
1217
1318describe ( 'locale' , ( ) => {
@@ -716,4 +721,26 @@ describe('__i18n', () => {
716721 } )
717722} )
718723
724+ test ( 'addPreCompileMessages' , ( ) => {
725+ const messages = { }
726+ const functions = Object . create ( null )
727+ const msg1 = ( ) => { }
728+ const msg2 = ( ) => { }
729+ functions [ generateFormatCacheKey ( 'en' , 'hello' , 'hello,world' ) ] = msg1
730+ functions [
731+ generateFormatCacheKey ( 'ja' , 'foo.bar.hello' , 'こんにちは、世界' )
732+ ] = msg2
733+ addPreCompileMessages ( messages , functions )
734+ expect ( messages [ 'en' ] ) . toMatchObject ( {
735+ hello : msg1
736+ } )
737+ expect ( messages [ 'ja' ] ) . toMatchObject ( {
738+ foo : {
739+ bar : {
740+ hello : msg2
741+ }
742+ }
743+ } )
744+ } )
745+
719746/* eslint-enable @typescript-eslint/no-empty-function */
You can’t perform that action at this time.
0 commit comments