From 3750caf0fbb83951b7fbc57c98b7b0c0f883468c Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Sat, 2 May 2020 17:24:45 +0900 Subject: [PATCH 1/2] fix: pre-compile locale messages registration bug --- src/composer.ts | 23 +++++++++++++++-------- test/composer.test.ts | 22 +++++++++++++++++++++- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/composer.ts b/src/composer.ts index 9a309f37d..2bc0deb1b 100644 --- a/src/composer.ts +++ b/src/composer.ts @@ -218,7 +218,7 @@ function getLocaleMessages( return ret } -function addPreCompileMessages( +export function addPreCompileMessages( messages: LocaleMessages, functions: MessageFunctions ): void { @@ -226,7 +226,10 @@ function addPreCompileMessages( keys.forEach(key => { const compiled = functions[key] const { l, k } = JSON.parse(key) - const targetLocaleMessage = (messages[l] = messages[l] || {}) + if (!messages[l]) { + messages[l] = {} + } + const targetLocaleMessage = messages[l] const paths = parsePath(k) if (paths != null) { const len = paths.length @@ -234,14 +237,18 @@ function addPreCompileMessages( let i = 0 while (i < len) { const path = paths[i] - const val = last[path] - if (val != null) { - last[path] = {} + if (i === len - 1) { + last[path] = compiled + break + } else { + let val = last[path] + if (!val) { + last[path] = val = {} + } + last = val + i++ } - last = val - i++ } - last = compiled } }) } diff --git a/test/composer.test.ts b/test/composer.test.ts index 3c4d38cd3..fbfd4b73e 100644 --- a/test/composer.test.ts +++ b/test/composer.test.ts @@ -7,7 +7,8 @@ jest.mock('../src/utils', () => ({ })) import { warn } from '../src/utils' -import { createComposer, MissingHandler } from '../src/composer' +import { createComposer, MissingHandler, addPreCompileMessages } from '../src/composer' +import { generateFormatCacheKey } from '../src/utils' import { watch } from 'vue' describe('locale', () => { @@ -716,4 +717,23 @@ describe('__i18n', () => { }) }) +test('addPreCompileMessages', () => { + const messages = {} + const functions = Object.create(null) + const msg1 = () => {} + const msg2 = () => {} + functions[generateFormatCacheKey('en', 'hello', 'hello,world')] = msg1 + functions[generateFormatCacheKey('ja', 'foo.bar.hello', 'こんにちは、世界')] = msg2 + addPreCompileMessages(messages, functions) + expect(messages['en']).toMatchObject({ + hello: msg1 + }) + expect(messages['ja']).toMatchObject({ + foo: { + bar: { + hello: msg2 + } + } +}) + /* eslint-enable @typescript-eslint/no-empty-function */ From fc3913899e26847970d8ca6389204ec010c4db93 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Sat, 2 May 2020 17:28:20 +0900 Subject: [PATCH 2/2] fix: lint error --- test/composer.test.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test/composer.test.ts b/test/composer.test.ts index fbfd4b73e..88ea31214 100644 --- a/test/composer.test.ts +++ b/test/composer.test.ts @@ -7,7 +7,11 @@ jest.mock('../src/utils', () => ({ })) import { warn } from '../src/utils' -import { createComposer, MissingHandler, addPreCompileMessages } from '../src/composer' +import { + createComposer, + MissingHandler, + addPreCompileMessages +} from '../src/composer' import { generateFormatCacheKey } from '../src/utils' import { watch } from 'vue' @@ -723,7 +727,9 @@ test('addPreCompileMessages', () => { const msg1 = () => {} const msg2 = () => {} functions[generateFormatCacheKey('en', 'hello', 'hello,world')] = msg1 - functions[generateFormatCacheKey('ja', 'foo.bar.hello', 'こんにちは、世界')] = msg2 + functions[ + generateFormatCacheKey('ja', 'foo.bar.hello', 'こんにちは、世界') + ] = msg2 addPreCompileMessages(messages, functions) expect(messages['en']).toMatchObject({ hello: msg1 @@ -734,6 +740,7 @@ test('addPreCompileMessages', () => { hello: msg2 } } + }) }) /* eslint-enable @typescript-eslint/no-empty-function */