Skip to content

Commit

Permalink
⭐ new: hot reloading (#71) by @gglnx
Browse files Browse the repository at this point in the history
* Add support for hot reloading

* Add missing langVM

* Update hot reload doc

* Add testing for hot reloading
  • Loading branch information
gglnx authored and kazupon committed Oct 28, 2016
1 parent 5f0004f commit 7bb94ac
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 11 deletions.
1 change: 1 addition & 0 deletions gitbook/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ Internationalization plugin of Vue.js
* [Fallback Translation](fallback.md)
* [Component Locale](component.md)
* [Dynamic Locale](dynamic.md)
* [Hot reload](hot-reload.md)
* [API References](api.md)
13 changes: 13 additions & 0 deletions gitbook/hot-reload.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Hot reload

You can watch for changes in translation files and hot reload changes into your application.

```javascript
// Support hot updates
if (module.hot) {
module.hot.accept(['./en', './ja'], () => {
Vue.locale('en', require('./en').default);
Vue.locale('ja', require('./ja').default);
});
}
```
13 changes: 5 additions & 8 deletions src/asset.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import warn from './warn'

const locales = Object.create(null) // locales store


export default function (Vue) {
export default function (Vue, langVM) {
/**
* Register or retrieve a global locale definition.
*
Expand All @@ -14,15 +11,15 @@ export default function (Vue) {

Vue.locale = (id, definition, cb) => {
if (definition === undefined) { // gettter
return locales[id]
return langVM.locales[id]
} else { // setter
if (definition === null) {
locales[id] = undefined
delete locales[id]
langVM.locales[id] = undefined
delete langVM.locales[id]
} else {
setLocale(id, definition, locale => {
if (locale) {
locales[id] = locale
langVM.locales[id] = locale
} else {
warn('failed set `' + id + '` locale')
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function plugin (Vue, opts = {}) {
const lang = 'en'
setupLangVM(Vue, lang)

Asset(Vue)
Asset(Vue, langVM)
Override(Vue, langVM, version)
Config(Vue, langVM, lang)
Extend(Vue)
Expand All @@ -42,7 +42,7 @@ function setupLangVM (Vue, lang) {
const silent = Vue.config.silent
Vue.config.silent = true
if (!langVM) {
langVM = new Vue({ data: { lang } })
langVM = new Vue({ data: { lang, locales: {} } })
}
Vue.config.silent = silent
}
Expand Down
4 changes: 3 additions & 1 deletion src/override.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ export default function (Vue, langVM, version) {

if (!this.$parent) { // root
this.$lang = langVM
this._langUnwatch = this.$lang.$watch('lang', (a, b) => {
this._langUnwatch = this.$lang.$watch('$data', (a, b) => {
update(this)
}, {
deep: true
})
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/specs/fixture/locales-updated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import locales from './locales'

locales.en.message.hello = 'the world updated'

export default locales
43 changes: 43 additions & 0 deletions test/specs/i18n.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from 'power-assert'
import Vue from 'vue'
import locales from './fixture/locales'
import updatedLocales from './fixture/locales-updated'


describe('i18n', () => {
Expand Down Expand Up @@ -519,6 +520,48 @@ describe('i18n', () => {
})


describe('hot reload', () => {
let el
beforeEach(() => {
el = document.createElement('div')
document.body.appendChild(el)
})

it('should translate', done => {
const options = {
el,
data () {
return { lang: 'en' }
}
}

if (version >= 2) {
options.render = function (h) {
return h('p', {}, [this.$t('message.hello', this.lang)])
}
} else {
options.template = '<p>{{ $t("message.hello", lang) }}</p>'
}

const vm = new Vue(options)

Vue.nextTick(() => {
assert.equal(vm.$el.textContent, locales.en.message.hello)

// Update translation
Object.keys(updatedLocales).forEach(lang => {
Vue.locale(lang, updatedLocales[lang])
})

Vue.nextTick(() => {
assert.equal(vm.$el.textContent, updatedLocales.en.message.hello)
done()
})
})
})
})


describe('translate component', () => {
let el
beforeEach(() => {
Expand Down

0 comments on commit 7bb94ac

Please sign in to comment.