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

Commit 203ee85

Browse files
committed
feat(lang): support lang reactive changing
Closes #2 #15
1 parent f7d597a commit 203ee85

6 files changed

Lines changed: 135 additions & 25 deletions

File tree

config/webpack.dev.conf.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var webpack = require('webpack')
2+
3+
module.exports = {
4+
entry: './index.js',
5+
output: {
6+
path: './',
7+
publicPath: '/',
8+
filename: 'build.js'
9+
},
10+
devtool: 'source-map',
11+
module: {
12+
preLoaders: [{
13+
test: /\.js$/,
14+
exclude: /node_modules/,
15+
loader: 'eslint-loader'
16+
}],
17+
loaders: [{
18+
test: /\.js$/,
19+
exclude: /node_modules|vue\/dist/,
20+
loader: 'babel',
21+
query: {
22+
presets: ['es2015']
23+
}
24+
}]
25+
},
26+
devServer: {
27+
contentBase: './',
28+
port: 8080,
29+
hot: true,
30+
inline: true
31+
},
32+
plugins: [
33+
new webpack.HotModuleReplacementPlugin()
34+
]
35+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"rollup-plugin-babel": "^2.4.0",
5252
"rollup-plugin-replace": "^1.1.0",
5353
"uglify-js": "^2.6.1",
54-
"vue": "^0.12.0",
54+
"vue": "^1.0.21",
5555
"webpack": "^1.12.14",
5656
"webpack-dev-server": "^1.14.1"
5757
},
@@ -84,6 +84,7 @@
8484
"coolkids": "VUE_I18N_TYPE=sauce SAUCE=batch1 karma start config/karma.conf.js",
8585
"coverage": "VUE_I18N_TYPE=coverage karma start config/karma.conf.js",
8686
"coveralls": "VUE_I18N_TYPE=coveralls karma start config/karma.conf.js",
87+
"proto": "webpack-dev-server --quite --config config/webpack.dev.conf.js --host 0.0.0.0",
8788
"dev": "webpack-dev-server --quiet --config config/webpack.test.conf.js",
8889
"e2e": "webpack-dev-server --quiet --config config/webpack.e2e.conf.js & mocha -t 20000 --compilers js:espower-babel/guess test/e2e/test.js && kill $! || (kill $! && exit 1)",
8990
"ie": "VUE_I18N_TYPE=sauce SAUCE=batch2 karma start config/karma.conf.js",

src/config.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { getWatcher, getDep } from './util'
2+
3+
export default function (Vue, langVM) {
4+
const Watcher = getWatcher(langVM)
5+
const Dep = getDep(langVM)
6+
7+
function makeComputedGetter (getter, owner) {
8+
let watcher = new Watcher(owner, getter, null, {
9+
lazy: true
10+
})
11+
12+
return function computedGetter () {
13+
if (watcher.dirty) {
14+
watcher.evaluate()
15+
}
16+
if (Dep.target) {
17+
watcher.depend()
18+
}
19+
return watcher.value
20+
}
21+
}
22+
23+
// define Vue.config.lang configration
24+
Object.defineProperty(Vue.config, 'lang', {
25+
enumerable: true,
26+
configurable: true,
27+
get: makeComputedGetter(() => { return langVM.lang }, langVM),
28+
set: Vue.util.bind((val) => { langVM.lang = val }, langVM)
29+
})
30+
}

src/index.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import util, { warn } from './util'
2-
import extend from './extend'
1+
import { warn } from './util'
2+
import Override from './override'
3+
import Config from './config'
4+
import Extend from './extend'
5+
6+
let langVM // singleton
37

48

59
/**
@@ -15,26 +19,20 @@ function plugin (Vue, opts = { lang: 'en', locales: {} }) {
1519
return
1620
}
1721

18-
defineConfig(Vue.config, opts.lang)
19-
extend(Vue, opts.locales)
20-
}
21-
22+
setupLangVM(Vue, opts.lang)
2223

23-
/**
24-
* defineConfig
25-
*
26-
* This function define `lang` property to `Vue.config`.
27-
*
28-
* @param {Object} config
29-
* @param {String} lang
30-
* @private
31-
*/
24+
Override(Vue, langVM)
25+
Config(Vue, langVM)
26+
Extend(Vue, opts.locales)
27+
}
3228

33-
function defineConfig (config, lang) {
34-
Object.defineProperty(config, 'lang', {
35-
get: () => { return lang },
36-
set: (val) => { lang = val }
37-
})
29+
function setupLangVM (Vue, lang) {
30+
const silent = Vue.config.silent
31+
Vue.config.silent = true
32+
if (!langVM) {
33+
langVM = new Vue({ data: { lang: lang } })
34+
}
35+
Vue.config.silent = silent
3836
}
3937

4038
plugin.version = '2.4.1'

src/override.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export default function (Vue, langVM) {
2+
// override _init
3+
const init = Vue.prototype._init
4+
Vue.prototype._init = function (options) {
5+
options = options || {}
6+
let root = options._parent || options.parent || this
7+
let lang = root.$lang
8+
9+
if (lang) {
10+
this.$lang = lang
11+
} else {
12+
this.$lang = langVM
13+
}
14+
15+
this._langUnwatch = this.$lang.$watch('lang', (a, b) => {
16+
update(this)
17+
})
18+
19+
init.call(this, options)
20+
}
21+
22+
// override _destroy
23+
const destroy = Vue.prototype._destroy
24+
Vue.prototype._destroy = function () {
25+
if (this._langUnwatch) {
26+
this._langUnwatch()
27+
this._langUnwatch = null
28+
}
29+
30+
this.$lang = null
31+
destroy.apply(this, arguments)
32+
}
33+
}
34+
35+
function update (vm) {
36+
let i = vm._watchers.length
37+
while (i--) {
38+
vm._watchers[i].update(true) // shallow updates
39+
}
40+
}

test/specs/i18n.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,21 @@ describe('i18n', () => {
291291
})
292292

293293
context('ja', () => {
294-
it('should translate with japanese', () => {
294+
it('should translate with japanese', (done) => {
295295
Vue.config.lang = 'ja'
296-
assert(vm.$t('message.hello') === locales.ja.message.hello)
296+
Vue.nextTick(() => {
297+
assert(vm.$t('message.hello') === locales.ja.message.hello)
298+
done()
299+
})
297300
})
298301

299302
context('en', () => {
300-
it('should translate with english', () => {
303+
it('should translate with english', (done) => {
301304
Vue.config.lang = 'en'
302-
assert(vm.$t('message.hello') === locales.en.message.hello)
305+
Vue.nextTick(() => {
306+
assert(vm.$t('message.hello') === locales.en.message.hello)
307+
done()
308+
})
303309
})
304310
})
305311
})

0 commit comments

Comments
 (0)