From 2b6e25916720e3d761aa9488612a0e8d01cdee3b Mon Sep 17 00:00:00 2001 From: Vito Laera Date: Thu, 30 May 2019 23:50:07 +0200 Subject: [PATCH 1/4] feat: allow component to receive a VueI18n factory --- src/mixin.js | 3 ++ test/unit/factory.test.js | 63 +++++++++++++++++++++++++++++++++++++++ types/index.d.ts | 9 +++++- 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 test/unit/factory.test.js diff --git a/src/mixin.js b/src/mixin.js index 42e9ce31f..2eeb35e1b 100644 --- a/src/mixin.js +++ b/src/mixin.js @@ -9,6 +9,9 @@ export default { options.i18n = options.i18n || (options.__i18n ? {} : null) if (options.i18n) { + if (typeof options.i18n === 'function') { + options.i18n = options.i18n() + } if (options.i18n instanceof VueI18n) { // init locale messages via custom blocks if (options.__i18n) { diff --git a/test/unit/factory.test.js b/test/unit/factory.test.js new file mode 100644 index 000000000..c83f3f882 --- /dev/null +++ b/test/unit/factory.test.js @@ -0,0 +1,63 @@ +describe('i18n factory', () => { + let InstanceBasedComponent, FactoryBasedComponent + const messages = { + 'en-US': { + who: 'root', + fallback: 'fallback' + }, + 'ja-JP': { + who: 'ルート', + fallback: 'フォールバック' + } + } + + const i18nFactory = () => new VueI18n({ + locale: 'en-US', + messages + }) + + const i18n = i18nFactory() + + beforeEach(done => { + InstanceBasedComponent = Vue.extend({ + i18n, // normal i18n instance injection + render (h) { + return h('div', {}, [ + h('p', { ref: 'who' }, [this.$t('who')]) + ]) + } + }) + done() + }) + + beforeEach(done => { + FactoryBasedComponent = Vue.extend({ + i18n: i18nFactory, // normal i18n instance injection + render (h) { + return h('div', {}, [ + h('p', { ref: 'who' }, [this.$t('who')]) + ]) + } + }) + done() + }) + + it('should initialize a VueI18n factory', () => { + const vm = new FactoryBasedComponent() + assert(vm.$i18n instanceof VueI18n) + }) + + it('should have different VueI18n instances on different components instances', () => { + const vm = new FactoryBasedComponent() + const vm2 = new FactoryBasedComponent() + assert.notEqual(vm.$i18n, vm2.$i18n) + }) + + it('should behave normally if factory is not provided', () => { + const vm = new InstanceBasedComponent() + const vm2 = new InstanceBasedComponent() + assert(vm.$i18n instanceof VueI18n) + assert(vm2.$i18n instanceof VueI18n) + assert.strictEqual(vm.$i18n, vm2.$i18n) + }) +}) diff --git a/types/index.d.ts b/types/index.d.ts index 07438a427..67f6a50ef 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -213,12 +213,19 @@ declare module 'vue/types/vue' { } declare module 'vue/types/options' { + + type VueI18nFactory = () => { + messages?: VueI18n.LocaleMessages; + dateTimeFormats?: VueI18n.DateTimeFormats; + numberFormats?: VueI18n.NumberFormats; + } + interface ComponentOptions { i18n?: { messages?: VueI18n.LocaleMessages; dateTimeFormats?: VueI18n.DateTimeFormats; numberFormats?: VueI18n.NumberFormats; - }; + } | VueI18nFactory; } } From bb2e6c1100f6692ba3feaf751acf3081a8cb88ff Mon Sep 17 00:00:00 2001 From: Vito Laera Date: Fri, 31 May 2019 00:00:22 +0200 Subject: [PATCH 2/4] fix(factory-test): wrong comment --- test/unit/factory.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/factory.test.js b/test/unit/factory.test.js index c83f3f882..931be372f 100644 --- a/test/unit/factory.test.js +++ b/test/unit/factory.test.js @@ -32,7 +32,7 @@ describe('i18n factory', () => { beforeEach(done => { FactoryBasedComponent = Vue.extend({ - i18n: i18nFactory, // normal i18n instance injection + i18n: i18nFactory, // i18n factory injection render (h) { return h('div', {}, [ h('p', { ref: 'who' }, [this.$t('who')]) From 9eba9c41cf5ebd1779b6a1211b342f8e8d54781b Mon Sep 17 00:00:00 2001 From: Vito Laera Date: Fri, 31 May 2019 00:07:06 +0200 Subject: [PATCH 3/4] docs(factory): update readme --- vuepress/api/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vuepress/api/README.md b/vuepress/api/README.md index bee8a34cf..726473387 100644 --- a/vuepress/api/README.md +++ b/vuepress/api/README.md @@ -10,9 +10,10 @@ sidebar: auto #### i18n - * **Type:** `I18nOptions` + * **Type:** `I18nOptions | VueI18nFactory` Component based localization option. +A VueI18n factory can be provided. * **See also:** `VueI18n` class constructor options From cba84087a396e1c2baab823101301797e2b142e3 Mon Sep 17 00:00:00 2001 From: Vito Laera Date: Mon, 19 Aug 2019 14:16:27 +0200 Subject: [PATCH 4/4] Update vuepress/api/README.md Co-Authored-By: TATSUNO Yasuhiro --- vuepress/api/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vuepress/api/README.md b/vuepress/api/README.md index 3be0a3dc3..62ebbb506 100644 --- a/vuepress/api/README.md +++ b/vuepress/api/README.md @@ -13,7 +13,7 @@ sidebar: auto * **Type:** `I18nOptions | VueI18nFactory` Component based localization option. -A VueI18n factory can be provided. +A VueI18n factory can be provided. Factory is useful when you need to create multiple VueI18n instances of same configurations (e.g. micro-frontent) . * **See also:** `VueI18n` class constructor options