1212
1313This library exports the following extensions:
1414
15+
1516## :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+
2121
2222## :cd : Installation
2323
2424``` sh
2525$ npm i --save-dev @intlify/vue-i18n-extensions@alpha
2626```
2727
28+
2829## :rocket : Extensions
2930
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 !!
3299
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 ` :
34104
35105``` js
36106import { compile } from ' @vue/compiler-dom'
37107import { createI18n } from ' vue-i18n'
38- import { defineTransformVT } from ' @intlify/vue-i18n-extensions'
108+ import { transformVTDirective } from ' @intlify/vue-i18n-extensions'
39109
110+ // create i18n instance
40111const i18n = createI18n ({
41112 locale: ' ja' ,
42113 messages: {
@@ -48,23 +119,24 @@ const i18n = createI18n({
48119 }
49120 }
50121})
51- const transformVT = defineTransformVT (i18n)
122+
123+ // get transform from `transformVTDirective` function, with `i18n` option
124+ const transformVT = transformVTDirective ({ i18n })
52125
53126const { code } = compile (` <p v-t="'hello'"></p>` , {
54127 mode: ' function' ,
55128 hoistStatic: true ,
56129 prefixIdentifiers: true ,
57- directiveTransforms: { t: transformVT }
130+ directiveTransforms: { t: transformVT } // <- you need to specify to `directiveTransforms` option!
58131})
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
62136
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+ }
68140*/
69141```
70142
@@ -73,7 +145,7 @@ The following configration example of `vue-loader`:
73145``` js
74146const { VueLoaderPlugin } = require (' vue-loader' )
75147const { createI18n } = require (' vue-i18n' )
76- const { defineTransformVT } = require (' @intlify/vue-i18n-extensions' )
148+ const { transformVTDirective } = require (' @intlify/vue-i18n-extensions' )
77149
78150const i18n = createI18n ({
79151 locale: ' ja' ,
@@ -86,7 +158,7 @@ const i18n = createI18n({
86158 }
87159 }
88160})
89- const transformVT = defineTransformVT (i18n)
161+ const transformVT = transformVTDirective (i18n)
90162
91163module .exports = {
92164 module: {
@@ -112,6 +184,16 @@ module.exports = {
112184}
113185```
114186
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+
115197## :copyright : License
116198
117199[ MIT] ( http://opensource.org/licenses/MIT )
0 commit comments