-
-
Notifications
You must be signed in to change notification settings - Fork 337
/
directive.ts
177 lines (159 loc) · 4.66 KB
/
directive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { watch } from 'vue'
import { createI18nError, I18nErrorCodes } from './errors'
import { isString, isPlainObject, isNumber, inBrowser } from '@intlify/shared'
import type {
DirectiveBinding,
ObjectDirective,
WatchStopHandle,
ComponentInternalInstance
} from 'vue'
import type { I18n, I18nInternal } from './i18n'
import type { VueI18nInternal } from './legacy'
import type { Composer } from './composer'
import type { Locale, TranslateOptions, NamedValue } from '@intlify/core-base'
export type VTDirectiveValue = {
path: string
locale?: Locale
args?: NamedValue
choice?: number
plural?: number
}
declare global {
interface HTMLElement {
__i18nWatcher?: WatchStopHandle
__composer?: Composer
}
}
function getComposer(
i18n: I18n,
instance: ComponentInternalInstance
): Composer {
const i18nInternal = i18n as unknown as I18nInternal
if (i18n.mode === 'composition') {
return (i18nInternal.__getInstance(instance) || i18n.global) as Composer
} else {
const vueI18n = i18nInternal.__getInstance(instance)
return vueI18n != null
? (vueI18n as unknown as VueI18nInternal).__composer
: (i18n.global as unknown as VueI18nInternal).__composer
}
}
/**
* Translation Directive (`v-t`)
*
* @remarks
* Update the element `textContent` that localized with locale messages.
*
* You can use string syntax or object syntax.
*
* String syntax can be specified as a keypath of locale messages.
*
* If you can be used object syntax, you need to specify as the object key the following params
*
* ```
* - path: required, key of locale messages
* - locale: optional, locale
* - args: optional, for list or named formatting
* ```
*
* @example
* ```html
* <!-- string syntax: literal -->
* <p v-t="'foo.bar'"></p>
*
* <!-- string syntax: binding via data or computed props -->
* <p v-t="msg"></p>
*
* <!-- object syntax: literal -->
* <p v-t="{ path: 'hi', locale: 'ja', args: { name: 'kazupon' } }"></p>
*
* <!-- object syntax: binding via data or computed props -->
* <p v-t="{ path: greeting, args: { name: fullName } }"></p>
* ```
*
* @VueI18nDirective
*/
export type TranslationDirective<T = HTMLElement> = ObjectDirective<T>
export function vTDirective(i18n: I18n): TranslationDirective<HTMLElement> {
const _process = (binding: DirectiveBinding): [string, Composer] => {
const { instance, value } = binding
/* istanbul ignore if */
if (!instance || !instance.$) {
throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR)
}
const composer = getComposer(i18n, instance.$)
const parsedValue = parseValue(value)
return [
Reflect.apply(composer.t, composer, [...makeParams(parsedValue)]),
composer
]
}
const register = (el: HTMLElement, binding: DirectiveBinding): void => {
const [textContent, composer] = _process(binding)
if (inBrowser && i18n.global === composer) {
// global scope only
el.__i18nWatcher = watch(composer.locale, () => {
binding.instance && binding.instance.$forceUpdate()
})
}
el.__composer = composer
el.textContent = textContent
}
const unregister = (el: HTMLElement): void => {
if (inBrowser && el.__i18nWatcher) {
el.__i18nWatcher()
el.__i18nWatcher = undefined
delete el.__i18nWatcher
}
if (el.__composer) {
el.__composer = undefined
delete el.__composer
}
}
const update = (el: HTMLElement, { value }: DirectiveBinding): void => {
if (el.__composer) {
const composer = el.__composer
const parsedValue = parseValue(value)
el.textContent = Reflect.apply(composer.t, composer, [
...makeParams(parsedValue)
])
}
}
const getSSRProps = (binding: DirectiveBinding) => {
const [textContent] = _process(binding)
return { textContent }
}
return {
created: register,
unmounted: unregister,
beforeUpdate: update,
getSSRProps
} as TranslationDirective<HTMLElement>
}
function parseValue(value: unknown): VTDirectiveValue {
if (isString(value)) {
return { path: value }
} else if (isPlainObject(value)) {
if (!('path' in value)) {
throw createI18nError(I18nErrorCodes.REQUIRED_VALUE, 'path')
}
return value as VTDirectiveValue
} else {
throw createI18nError(I18nErrorCodes.INVALID_VALUE)
}
}
function makeParams(value: VTDirectiveValue): unknown[] {
const { path, locale, args, choice, plural } = value
const options = {} as TranslateOptions
const named: NamedValue = args || {}
if (isString(locale)) {
options.locale = locale
}
if (isNumber(choice)) {
options.plural = choice
}
if (isNumber(plural)) {
options.plural = plural
}
return [path, named, options]
}