From a60ea8b68dfb1aecfb8a0c15a2f8375b3798e94c Mon Sep 17 00:00:00 2001 From: jaredzhu1993 <34728950+jaredzhu1993@users.noreply.github.com> Date: Mon, 3 Sep 2018 16:47:55 +0800 Subject: [PATCH] :bug: bug(install): add support for Vue.extend vue-i18n instance (#420) by @jaredzhu1993 --- src/install.js | 8 ++++++-- test/unit/basic.test.js | 13 +++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/install.js b/src/install.js index 8066afd57..a93ce41ce 100644 --- a/src/install.js +++ b/src/install.js @@ -28,7 +28,11 @@ export function install (_Vue) { Vue.directive('t', { bind, update, unbind }) Vue.component(component.name, component) - // use object-based merge strategy + // use simple mergeStrategies to prevent i18n instance lose '__proto__' const strats = Vue.config.optionMergeStrategies - strats.i18n = strats.methods + strats.i18n = function (parentVal, childVal) { + return childVal === undefined + ? parentVal + : childVal + } } diff --git a/test/unit/basic.test.js b/test/unit/basic.test.js index 78b88e9b1..44c7598bb 100644 --- a/test/unit/basic.test.js +++ b/test/unit/basic.test.js @@ -595,6 +595,19 @@ describe('basic', () => { }) }) + describe('i18n#Vue.extend({ i18n })', () => { + it('should be translated', () => { + const el = document.createElement('div') + const Constructor = Vue.extend({ i18n }) + const vm = new Constructor({ + render (h) { + return h('p', { ref: 'text' }, [this.$t('message.hello')]) + } + }).$mount(el) + assert.equal(vm.$refs.text.textContent, messages.en.message.hello) + }) + }) + let desc = VueI18n.availabilities.dateTimeFormat ? describe : describe.skip desc('i18n#d', () => { let dt