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

Commit c29007e

Browse files
lzxbkazupon
authored andcommitted
🐛 bug(extend): Fix this not found #259 (#260) by @lzxb
* bug(extend) Fix this not found * 👕test(issues) featrue #258 test
1 parent 894be36 commit c29007e

2 files changed

Lines changed: 72 additions & 22 deletions

File tree

src/extend.js

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,47 @@
11
/* @flow */
22

33
export default function extend (Vue: any): void {
4-
Vue.prototype.$t = function (key: Path, ...values: any): TranslateResult {
5-
const i18n = this.$i18n
6-
return i18n._t(key, i18n.locale, i18n._getMessages(), this, ...values)
7-
}
8-
9-
Vue.prototype.$tc = function (key: Path, choice?: number, ...values: any): TranslateResult {
10-
const i18n = this.$i18n
11-
return i18n._tc(key, i18n.locale, i18n._getMessages(), this, choice, ...values)
12-
}
13-
14-
Vue.prototype.$te = function (key: Path, locale?: Locale): boolean {
15-
const i18n = this.$i18n
16-
return i18n._te(key, i18n.locale, i18n._getMessages(), locale)
17-
}
18-
19-
Vue.prototype.$d = function (value: number | Date, ...args: any): DateTimeFormatResult {
20-
return this.$i18n.d(value, ...args)
21-
}
22-
23-
Vue.prototype.$n = function (value: number, ...args: any): NumberFormatResult {
24-
return this.$i18n.n(value, ...args)
25-
}
4+
// $FlowFixMe
5+
Object.defineProperty(Vue.prototype, '$t', {
6+
get () {
7+
return (key: Path, ...values: any): TranslateResult => {
8+
const i18n = this.$i18n
9+
return i18n._t(key, i18n.locale, i18n._getMessages(), this, ...values)
10+
}
11+
}
12+
})
13+
// $FlowFixMe
14+
Object.defineProperty(Vue.prototype, '$tc', {
15+
get () {
16+
return (key: Path, choice?: number, ...values: any): TranslateResult => {
17+
const i18n = this.$i18n
18+
return i18n._tc(key, i18n.locale, i18n._getMessages(), this, choice, ...values)
19+
}
20+
}
21+
})
22+
// $FlowFixMe
23+
Object.defineProperty(Vue.prototype, '$te', {
24+
get () {
25+
return (key: Path, locale?: Locale): boolean => {
26+
const i18n = this.$i18n
27+
return i18n._te(key, i18n.locale, i18n._getMessages(), locale)
28+
}
29+
}
30+
})
31+
// $FlowFixMe
32+
Object.defineProperty(Vue.prototype, '$d', {
33+
get () {
34+
return (value: number | Date, ...args: any): DateTimeFormatResult => {
35+
return this.$i18n.d(value, ...args)
36+
}
37+
}
38+
})
39+
// $FlowFixMe
40+
Object.defineProperty(Vue.prototype, '$n', {
41+
get () {
42+
return (value: number, ...args: any): NumberFormatResult => {
43+
return this.$i18n.n(value, ...args)
44+
}
45+
}
46+
})
2647
}

test/unit/issues.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,33 @@ describe('issues', () => {
285285
}).then(done)
286286
})
287287
})
288+
289+
describe('#259', () => {
290+
it('this points to the right', (done) => {
291+
const vm = new Vue({
292+
i18n: new VueI18n({
293+
locale: 'en',
294+
messages: {
295+
en: {
296+
'hello': 'hello #259'
297+
},
298+
ja: {
299+
'hello': 'こんにちは #259'
300+
}
301+
}
302+
})
303+
})
304+
const $t = vm.$t
305+
const $tc = vm.$t
306+
const $te = vm.$t
307+
const $d = vm.$t
308+
const $n = vm.$t
309+
assert.equal($t('hello'), 'hello #259')
310+
assert.equal($tc('hello'), 'hello #259')
311+
assert.equal($te('hello'), 'hello #259')
312+
assert.equal($d('hello'), 'hello #259')
313+
assert.equal($n('hello'), 'hello #259')
314+
done()
315+
})
316+
})
288317
})

0 commit comments

Comments
 (0)