Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for hot reloading #71

Merged
merged 4 commits into from
Oct 28, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', './cn'], () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please adjust the indent 2 spaces level.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it :)

Vue.locale('en', require('./en').default);
Vue.locale('cn', require('./cn').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