diff --git a/src/mixin.js b/src/mixin.js index ea6b2ac98..7272e8833 100644 --- a/src/mixin.js +++ b/src/mixin.js @@ -10,6 +10,19 @@ export default { if (options.i18n) { if (options.i18n instanceof VueI18n) { + // init locale messages via custom blocks + if (options.__i18n) { + try { + const localeMessages = JSON.parse(options.__i18n) + Object.keys(localeMessages).forEach((locale: Locale) => { + options.i18n.mergeLocaleMessage(locale, localeMessages[locale]) + }) + } catch (e) { + if (process.env.NODE_ENV !== 'production') { + warn(`Cannot parse locale messages via custom blocks.`) + } + } + } this._i18n = options.i18n this._i18nWatcher = this._i18n.watchI18nData(() => this.$forceUpdate()) } else if (isPlainObject(options.i18n)) { diff --git a/test/unit/issues.test.js b/test/unit/issues.test.js index bd639fc5e..1b3d8f0cd 100644 --- a/test/unit/issues.test.js +++ b/test/unit/issues.test.js @@ -70,4 +70,26 @@ describe('issues', () => { ) }) }) + + describe('#169', () => { + it('should be translated', done => { + const Component = Vue.extend({ + __i18n: JSON.stringify({ + en: { custom: 'custom block!' } + }), + render (h) { + return h('p', { ref: 'custom' }, [this.$t('custom')]) + } + }) + const vm = new Component({ + i18n: new VueI18n({ + locale: 'en', + messages + }) + }).$mount() + nextTick(() => { + assert.equal(vm.$refs.custom.textContent, 'custom block!') + }).then(done) + }) + }) })