From ad6962b10e01c43621c4db277572755dbd478661 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Thu, 4 Jun 2020 01:10:40 +0900 Subject: [PATCH] feat: accept object resource custom block --- src/composer.ts | 4 ++-- test/composer.test.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/composer.ts b/src/composer.ts index 12fae70be..04fd39fcc 100644 --- a/src/composer.ts +++ b/src/composer.ts @@ -84,7 +84,7 @@ export type PreCompileHandler = () => { functions: MessageFunctions } -export type CustomBlocks = string[] | PreCompileHandler +export type CustomBlocks = Array | PreCompileHandler /** * Composer Options @@ -222,7 +222,7 @@ function getLocaleMessages( // merge locale messages of i18n custom block if (isArray(__i18n)) { __i18n.forEach(raw => { - ret = Object.assign(ret, JSON.parse(raw)) + ret = Object.assign(ret, isString(raw) ? JSON.parse(raw) : raw) }) return ret } diff --git a/test/composer.test.ts b/test/composer.test.ts index d4046dd21..98f1daf81 100644 --- a/test/composer.test.ts +++ b/test/composer.test.ts @@ -849,6 +849,35 @@ describe('__i18n', () => { } }) }) + + test('locale messages object', () => { + const { messages } = createComposer({ + __i18n: [ + { en: { hello: 'Hello,world!' } }, + { + ja: { + hello: 'こんにちは、世界!', + nest: { + foo: { + bar: 'ばー' + } + } + } + } + ] + }) + expect(messages.value).toEqual({ + en: { hello: 'Hello,world!' }, + ja: { + hello: 'こんにちは、世界!', + nest: { + foo: { + bar: 'ばー' + } + } + } + }) + }) }) describe('__transrateVNode', () => {