12
12
13
13
This library exports the following extensions:
14
14
15
+
15
16
## :star : Features
16
- - module: ` v-t ` custom directive compiler module for the following:
17
- - ` @vue/compiler-core ` (` options ` at ` baseCompile ` function)
18
- - ` @vue/compiler-dom ` (` options ` at ` compile ` function)
19
- - ` @vue/compiler-sfc ` (` compilerOptions ` at ` compileTemplate ` function)
20
- - ` vue-loader ` (` compilerOptions ` at ` options ` )
17
+
18
+ - Server-side rendering for ` v-t ` custom directive
19
+ - Pre-Translation
20
+
21
21
22
22
## :cd : Installation
23
23
24
24
``` sh
25
25
$ npm i --save-dev @intlify/vue-i18n-extensions@alpha
26
26
```
27
27
28
+
28
29
## :rocket : Extensions
29
30
30
- ### module: ` v-t ` custom directive compiler module
31
- This module is ` v-t ` custom directive module for vue compilers. You can specify it.
31
+ ### Server-side rendering for ` v-t ` custom directive
32
+
33
+ You can use tnrasform offered with this package, to support Server-side rendering for ` v-t ` custom directives.
34
+
35
+ In order to use this feature, you need to specify to Vue compiler option.
36
+ The following example that use ` compile ` of ` @vue/compiler-ssr ` :
37
+
38
+ ``` js
39
+ import * as runtimeDom from ' @vue/runtime-dom'
40
+ import { compile } from ' @vue/compiler-ssr'
41
+ import { defineComponent , createSSRApp } from ' vue'
42
+ import { renderToString } from ' @vue/server-renderer'
43
+ import { createI18n , useI18n } from ' vue-i18n'
44
+ import { transformVTDirective } from ' @intlify/vue-i18n-extensions'
45
+
46
+ // create i18n instance
47
+ const i18n = createI18n ({
48
+ locale: ' ja' ,
49
+ messages: {}
50
+ })
51
+
52
+ // get transform from `transformVTDirective` function
53
+ const transformVT = transformVTDirective ()
54
+
55
+ // compile your source
56
+ const source = ` <div v-t="{ path: 'dessert', locale: 'en', plural: 2, args: { name: 'banana' } }"/>`
57
+ const { code } = compile (source, {
58
+ mode: ' function' ,
59
+ directiveTransforms: { t: transformVT } // <- you need to specify to `directiveTransforms` option!
60
+ })
61
+
62
+ // render functionalization
63
+ const render = Function (' require' , ' Vue' , code)(require, runtimeDom)
64
+
65
+ // e.g. set render function to App component
66
+ const App = defineComponent ({
67
+ setup () {
68
+ return useI18n ({
69
+ locale: ' en' ,
70
+ inheritLocale: false ,
71
+ messages: {
72
+ en: {
73
+ apple: ' no apples | one apple | {count} apples' ,
74
+ banana: ' no bananas | {n} banana | {n} bananas' ,
75
+ dessert: ' I eat @:{name}!'
76
+ }
77
+ }
78
+ })
79
+ },
80
+ ssrRender: render
81
+ })
82
+
83
+ // create SSR app
84
+ const app = createSSRApp (App)
85
+
86
+ // install vue-i18n
87
+ app .use (i18n)
88
+
89
+ console .log (await renderToString (app))
90
+ // output -> <div>I eat 2 bananas!</div>`
91
+ ```
92
+
93
+
94
+ ### Pre-Translation with using ` v-t ` custom directive
95
+
96
+ You can pre-translation i18n locale messsages of vue-i18n.
97
+
98
+ > :warning : NOTE: Only the scope of global i18n locale messages can be pre-translated !!
32
99
33
- The following example that use ` compile ` function of ` @vue/compiler-dom ` :
100
+ > :warning : NOTE: Currently only ` v-t ` custom directive is supported !!
101
+
102
+ In order to use this feature, you need to specify to Vue compiler option.
103
+ The following example that use ` compile ` of ` @vue/compiler-dom ` :
34
104
35
105
``` js
36
106
import { compile } from ' @vue/compiler-dom'
37
107
import { createI18n } from ' vue-i18n'
38
- import { defineTransformVT } from ' @intlify/vue-i18n-extensions'
108
+ import { transformVTDirective } from ' @intlify/vue-i18n-extensions'
39
109
110
+ // create i18n instance
40
111
const i18n = createI18n ({
41
112
locale: ' ja' ,
42
113
messages: {
@@ -48,23 +119,24 @@ const i18n = createI18n({
48
119
}
49
120
}
50
121
})
51
- const transformVT = defineTransformVT (i18n)
122
+
123
+ // get transform from `transformVTDirective` function, with `i18n` option
124
+ const transformVT = transformVTDirective ({ i18n })
52
125
53
126
const { code } = compile (` <p v-t="'hello'"></p>` , {
54
127
mode: ' function' ,
55
128
hoistStatic: true ,
56
129
prefixIdentifiers: true ,
57
- directiveTransforms: { t: transformVT }
130
+ directiveTransforms: { t: transformVT } // <- you need to specify to `directiveTransforms` option!
58
131
})
59
- console .log (codel)
60
- /* output -> 'hello'
61
- const { createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = Vue
132
+ console .log (code)
133
+ /*
134
+ output ->
135
+ const { createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = Vue
62
136
63
- const _hoisted_1 = { textContent: "こんにちは" }
64
-
65
- return function render(_ctx, _cache) {
66
- return (_openBlock(), _createBlock("p", _hoisted_1))
67
- }
137
+ return function render(_ctx, _cache) {
138
+ return (_openBlock(), _createBlock(\\"div\\", null, \\"こんにちは!\\"))
139
+ }
68
140
*/
69
141
```
70
142
@@ -73,7 +145,7 @@ The following configration example of `vue-loader`:
73
145
``` js
74
146
const { VueLoaderPlugin } = require (' vue-loader' )
75
147
const { createI18n } = require (' vue-i18n' )
76
- const { defineTransformVT } = require (' @intlify/vue-i18n-extensions' )
148
+ const { transformVTDirective } = require (' @intlify/vue-i18n-extensions' )
77
149
78
150
const i18n = createI18n ({
79
151
locale: ' ja' ,
@@ -86,7 +158,7 @@ const i18n = createI18n({
86
158
}
87
159
}
88
160
})
89
- const transformVT = defineTransformVT (i18n)
161
+ const transformVT = transformVTDirective (i18n)
90
162
91
163
module .exports = {
92
164
module: {
@@ -112,6 +184,16 @@ module.exports = {
112
184
}
113
185
```
114
186
187
+ You can specify the transform resulting from ` transformVTDirective ` in the compiler options for each package offered by vue-next, and tool chains:
188
+
189
+ - ` @vue/compiler-core ` (` options ` at ` baseCompile ` function)
190
+ - ` @vue/compiler-dom ` (` options ` at ` compile ` function)
191
+ - ` @vue/compiler-ssr ` (` options ` at ` compile ` function)
192
+ - ` @vue/compiler-sfc ` (` compilerOptions ` at ` compileTemplate ` function)
193
+ - ` vue-loader ` (` compilerOptions ` at ` options ` )
194
+ - ` rollup-plugin-vue `
195
+
196
+
115
197
## :copyright : License
116
198
117
199
[ MIT] ( http://opensource.org/licenses/MIT )
0 commit comments