Skip to content

Commit

Permalink
💥 breaking(extend): fix this context binding
Browse files Browse the repository at this point in the history
closes #306
closes #286
closes #260

NOTE:
  revert #260 issue, back to revert.
  • Loading branch information
kazupon committed Jun 22, 2018
1 parent 9fb6698 commit aa0e831
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 75 deletions.
67 changes: 25 additions & 42 deletions src/extend.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,30 @@
/* @flow */

export default function extend (Vue: any): void {
// $FlowFixMe
Object.defineProperty(Vue.prototype, '$t', {
get () {
return (key: Path, ...values: any): TranslateResult => {
const i18n = this.$i18n
return i18n._t(key, i18n.locale, i18n._getMessages(), this, ...values)
}
}
})
// $FlowFixMe
Object.defineProperty(Vue.prototype, '$tc', {
get () {
return (key: Path, choice?: number, ...values: any): TranslateResult => {
const i18n = this.$i18n
return i18n._tc(key, i18n.locale, i18n._getMessages(), this, choice, ...values)
}
}
})
// $FlowFixMe
Object.defineProperty(Vue.prototype, '$te', {
get () {
return (key: Path, locale?: Locale): boolean => {
const i18n = this.$i18n
return i18n._te(key, i18n.locale, i18n._getMessages(), locale)
}
}
})
// $FlowFixMe
Object.defineProperty(Vue.prototype, '$d', {
get () {
return (value: number | Date, ...args: any): DateTimeFormatResult => {
return this.$i18n.d(value, ...args)
}
}
})
// $FlowFixMe
Object.defineProperty(Vue.prototype, '$n', {
get () {
return (value: number, ...args: any): NumberFormatResult => {
return this.$i18n.n(value, ...args)
}
}
Object.defineProperty(Vue.prototype, '$i18n', {
get () { return this._i18n }
})

Vue.prototype.$t = function (key: Path, ...values: any): TranslateResult {
const i18n = this.$i18n
return i18n._t(key, i18n.locale, i18n._getMessages(), this, ...values)
}

Vue.prototype.$tc = function (key: Path, choice?: number, ...values: any): TranslateResult {
const i18n = this.$i18n
return i18n._tc(key, i18n.locale, i18n._getMessages(), this, choice, ...values)
}

Vue.prototype.$te = function (key: Path, locale?: Locale): boolean {
const i18n = this.$i18n
return i18n._te(key, i18n.locale, i18n._getMessages(), locale)
}

Vue.prototype.$d = function (value: number | Date, ...args: any): DateTimeFormatResult {
return this.$i18n.d(value, ...args)
}

Vue.prototype.$n = function (value: number, ...args: any): NumberFormatResult {
return this.$i18n.n(value, ...args)
}
}
4 changes: 0 additions & 4 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ export function install (_Vue) {
return
}

Object.defineProperty(Vue.prototype, '$i18n', {
get () { return this._i18n }
})

extend(Vue)
Vue.mixin(mixin)
Vue.directive('t', { bind, update, unbind })
Expand Down
29 changes: 0 additions & 29 deletions test/unit/issues.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,35 +287,6 @@ describe('issues', () => {
})
})

describe('#259', () => {
it('this points to the right', done => {
const vm = new Vue({
i18n: new VueI18n({
locale: 'en',
messages: {
en: {
'hello': 'hello #259'
},
ja: {
'hello': 'こんにちは #259'
}
}
})
})
const $t = vm.$t
const $tc = vm.$t
const $te = vm.$t
const $d = vm.$t
const $n = vm.$t
assert.equal($t('hello'), 'hello #259')
assert.equal($tc('hello'), 'hello #259')
assert.equal($te('hello'), 'hello #259')
assert.equal($d('hello'), 'hello #259')
assert.equal($n('hello'), 'hello #259')
done()
})
})

describe('#377', () => {
it('should be destroyed', done => {
const el = document.createElement('div')
Expand Down

0 comments on commit aa0e831

Please sign in to comment.