1 |
|
2 |
|
3 | import { warn, isPlainObject, looseEqual } from './util'
|
4 |
|
5 | export function bind (el: any, binding: Object, vnode: any): void {
|
6 | if (!assert(el, vnode)) { return }
|
7 |
|
8 | t(el, binding, vnode)
|
9 | }
|
10 |
|
11 | export function update (el: any, binding: Object, vnode: any, oldVNode: any): void {
|
12 | if (!assert(el, vnode)) { return }
|
13 |
|
14 | const i18n: any = vnode.context.$i18n
|
15 | if (localeEqual(el, vnode) &&
|
16 | (looseEqual(binding.value, binding.oldValue) &&
|
17 | looseEqual(el._localeMessage, i18n.getLocaleMessage(i18n.locale)))) { return }
|
18 |
|
19 | t(el, binding, vnode)
|
20 | }
|
21 |
|
22 | export function unbind (el: any, binding: Object, vnode: any, oldVNode: any): void {
|
23 | const vm: any = vnode.context
|
24 | if (!vm) {
|
25 | warn('Vue instance does not exists in VNode context')
|
26 | return
|
27 | }
|
28 |
|
29 | const i18n: any = vnode.context.$i18n || {}
|
30 | if (!binding.modifiers.preserve && !i18n.preserveDirectiveContent) {
|
31 | el.textContent = ''
|
32 | }
|
33 | el._vt = undefined
|
34 | delete el['_vt']
|
35 | el._locale = undefined
|
36 | delete el['_locale']
|
37 | el._localeMessage = undefined
|
38 | delete el['_localeMessage']
|
39 | }
|
40 |
|
41 | function assert (el: any, vnode: any): boolean {
|
42 | const vm: any = vnode.context
|
43 | if (!vm) {
|
44 | warn('Vue instance does not exists in VNode context')
|
45 | return false
|
46 | }
|
47 |
|
48 | if (!vm.$i18n) {
|
49 | warn('VueI18n instance does not exists in Vue instance')
|
50 | return false
|
51 | }
|
52 |
|
53 | return true
|
54 | }
|
55 |
|
56 | function localeEqual (el: any, vnode: any): boolean {
|
57 | const vm: any = vnode.context
|
58 | return el._locale === vm.$i18n.locale
|
59 | }
|
60 |
|
61 | function t (el: any, binding: Object, vnode: any): void {
|
62 | const value: any = binding.value
|
63 |
|
64 | const { path, locale, args, choice } = parseValue(value)
|
65 | if (!path && !locale && !args) {
|
66 | warn('value type not supported')
|
67 | return
|
68 | }
|
69 |
|
70 | if (!path) {
|
71 | warn('`path` is required in v-t directive')
|
72 | return
|
73 | }
|
74 |
|
75 | const vm: any = vnode.context
|
76 | if (choice) {
|
77 | el._vt = el.textContent = vm.$i18n.tc(path, choice, ...makeParams(locale, args))
|
78 | } else {
|
79 | el._vt = el.textContent = vm.$i18n.t(path, ...makeParams(locale, args))
|
80 | }
|
81 | el._locale = vm.$i18n.locale
|
82 | el._localeMessage = vm.$i18n.getLocaleMessage(vm.$i18n.locale)
|
83 | }
|
84 |
|
85 | function parseValue (value: any): Object {
|
86 | let path: ?string
|
87 | let locale: ?Locale
|
88 | let args: any
|
89 | let choice: ?number
|
90 |
|
91 | if (typeof value === 'string') {
|
92 | path = value
|
93 | } else if (isPlainObject(value)) {
|
94 | path = value.path
|
95 | locale = value.locale
|
96 | args = value.args
|
97 | choice = value.choice
|
98 | }
|
99 |
|
100 | return { path, locale, args, choice }
|
101 | }
|
102 |
|
103 | function makeParams (locale: Locale, args: any): Array<any> {
|
104 | const params: Array<any> = []
|
105 |
|
106 | locale && params.push(locale)
|
107 | if (args && (Array.isArray(args) || isPlainObject(args))) {
|
108 | params.push(args)
|
109 | }
|
110 |
|
111 | return params
|
112 | }
|