Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

Commit 7bb94ac

Browse files
gglnxkazupon
authored andcommitted
⭐ new: hot reloading (#71) by @gglnx
* Add support for hot reloading * Add missing langVM * Update hot reload doc * Add testing for hot reloading
1 parent 5f0004f commit 7bb94ac

File tree

7 files changed

+72
-11
lines changed

7 files changed

+72
-11
lines changed

gitbook/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ Internationalization plugin of Vue.js
1111
* [Fallback Translation](fallback.md)
1212
* [Component Locale](component.md)
1313
* [Dynamic Locale](dynamic.md)
14+
* [Hot reload](hot-reload.md)
1415
* [API References](api.md)

gitbook/hot-reload.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Hot reload
2+
3+
You can watch for changes in translation files and hot reload changes into your application.
4+
5+
```javascript
6+
// Support hot updates
7+
if (module.hot) {
8+
module.hot.accept(['./en', './ja'], () => {
9+
Vue.locale('en', require('./en').default);
10+
Vue.locale('ja', require('./ja').default);
11+
});
12+
}
13+
```

src/asset.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import warn from './warn'
22

3-
const locales = Object.create(null) // locales store
4-
5-
6-
export default function (Vue) {
3+
export default function (Vue, langVM) {
74
/**
85
* Register or retrieve a global locale definition.
96
*
@@ -14,15 +11,15 @@ export default function (Vue) {
1411

1512
Vue.locale = (id, definition, cb) => {
1613
if (definition === undefined) { // gettter
17-
return locales[id]
14+
return langVM.locales[id]
1815
} else { // setter
1916
if (definition === null) {
20-
locales[id] = undefined
21-
delete locales[id]
17+
langVM.locales[id] = undefined
18+
delete langVM.locales[id]
2219
} else {
2320
setLocale(id, definition, locale => {
2421
if (locale) {
25-
locales[id] = locale
22+
langVM.locales[id] = locale
2623
} else {
2724
warn('failed set `' + id + '` locale')
2825
}

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function plugin (Vue, opts = {}) {
3232
const lang = 'en'
3333
setupLangVM(Vue, lang)
3434

35-
Asset(Vue)
35+
Asset(Vue, langVM)
3636
Override(Vue, langVM, version)
3737
Config(Vue, langVM, lang)
3838
Extend(Vue)
@@ -42,7 +42,7 @@ function setupLangVM (Vue, lang) {
4242
const silent = Vue.config.silent
4343
Vue.config.silent = true
4444
if (!langVM) {
45-
langVM = new Vue({ data: { lang } })
45+
langVM = new Vue({ data: { lang, locales: {} } })
4646
}
4747
Vue.config.silent = silent
4848
}

src/override.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ export default function (Vue, langVM, version) {
1717

1818
if (!this.$parent) { // root
1919
this.$lang = langVM
20-
this._langUnwatch = this.$lang.$watch('lang', (a, b) => {
20+
this._langUnwatch = this.$lang.$watch('$data', (a, b) => {
2121
update(this)
22+
}, {
23+
deep: true
2224
})
2325
}
2426
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import locales from './locales'
2+
3+
locales.en.message.hello = 'the world updated'
4+
5+
export default locales

test/specs/i18n.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from 'power-assert'
22
import Vue from 'vue'
33
import locales from './fixture/locales'
4+
import updatedLocales from './fixture/locales-updated'
45

56

67
describe('i18n', () => {
@@ -519,6 +520,48 @@ describe('i18n', () => {
519520
})
520521

521522

523+
describe('hot reload', () => {
524+
let el
525+
beforeEach(() => {
526+
el = document.createElement('div')
527+
document.body.appendChild(el)
528+
})
529+
530+
it('should translate', done => {
531+
const options = {
532+
el,
533+
data () {
534+
return { lang: 'en' }
535+
}
536+
}
537+
538+
if (version >= 2) {
539+
options.render = function (h) {
540+
return h('p', {}, [this.$t('message.hello', this.lang)])
541+
}
542+
} else {
543+
options.template = '<p>{{ $t("message.hello", lang) }}</p>'
544+
}
545+
546+
const vm = new Vue(options)
547+
548+
Vue.nextTick(() => {
549+
assert.equal(vm.$el.textContent, locales.en.message.hello)
550+
551+
// Update translation
552+
Object.keys(updatedLocales).forEach(lang => {
553+
Vue.locale(lang, updatedLocales[lang])
554+
})
555+
556+
Vue.nextTick(() => {
557+
assert.equal(vm.$el.textContent, updatedLocales.en.message.hello)
558+
done()
559+
})
560+
})
561+
})
562+
})
563+
564+
522565
describe('translate component', () => {
523566
let el
524567
beforeEach(() => {

0 commit comments

Comments
 (0)