Skip to content

Commit

Permalink
🐛 bug: fix cannat single file component translation
Browse files Browse the repository at this point in the history
Closes #169
  • Loading branch information
kazupon committed Jun 3, 2017
1 parent 94a8a03 commit 687d406
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
22 changes: 22 additions & 0 deletions test/unit/issues.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})

0 comments on commit 687d406

Please sign in to comment.