|
1 |
| -import { h, defineComponent, resolveComponent, createSSRApp } from 'vue' |
| 1 | +import { |
| 2 | + h, |
| 3 | + ref, |
| 4 | + defineComponent, |
| 5 | + resolveComponent, |
| 6 | + resolveDirective, |
| 7 | + withDirectives, |
| 8 | + createSSRApp |
| 9 | +} from 'vue' |
2 | 10 | import { renderToString } from '@vue/server-renderer'
|
3 | 11 | import {
|
4 | 12 | compileToFunction,
|
@@ -85,3 +93,113 @@ test('component: i18n-t', async () => {
|
85 | 93 |
|
86 | 94 | expect(await renderToString(app)).toMatch(`<p>こんにちは!</p>`)
|
87 | 95 | })
|
| 96 | + |
| 97 | +describe('custom directive: v-t', () => { |
| 98 | + test('basic', async () => { |
| 99 | + const i18n = createI18n({ |
| 100 | + legacy: false, |
| 101 | + locale: 'en', |
| 102 | + messages: { |
| 103 | + en: { |
| 104 | + hello: 'hello!' |
| 105 | + } |
| 106 | + } |
| 107 | + }) |
| 108 | + |
| 109 | + const App = defineComponent({ |
| 110 | + setup() { |
| 111 | + // <p v-t="'hello'"></p> |
| 112 | + const t = resolveDirective('t') |
| 113 | + return () => { |
| 114 | + return withDirectives(h('p'), [[t!, 'hello']]) |
| 115 | + } |
| 116 | + } |
| 117 | + }) |
| 118 | + const app = createSSRApp(App) |
| 119 | + app.use(i18n) |
| 120 | + |
| 121 | + expect(await renderToString(app)).toEqual('<p>hello!</p>') |
| 122 | + }) |
| 123 | + |
| 124 | + test('binding', async () => { |
| 125 | + const i18n = createI18n({ |
| 126 | + locale: 'en', |
| 127 | + messages: { |
| 128 | + en: { |
| 129 | + hello: 'hello!' |
| 130 | + } |
| 131 | + } |
| 132 | + }) |
| 133 | + |
| 134 | + const App = defineComponent({ |
| 135 | + setup() { |
| 136 | + // <p v-t="msg"></p> |
| 137 | + const msg = ref('hello') |
| 138 | + const t = resolveDirective('t') |
| 139 | + return () => { |
| 140 | + return withDirectives(h('p'), [[t!, msg.value]]) |
| 141 | + } |
| 142 | + } |
| 143 | + }) |
| 144 | + const app = createSSRApp(App) |
| 145 | + app.use(i18n) |
| 146 | + |
| 147 | + expect(await renderToString(app)).toEqual('<p>hello!</p>') |
| 148 | + }) |
| 149 | + |
| 150 | + test('object literal', async () => { |
| 151 | + const i18n = createI18n({ |
| 152 | + locale: 'en', |
| 153 | + messages: { |
| 154 | + en: { |
| 155 | + hello: 'hello, {name}!' |
| 156 | + }, |
| 157 | + ja: { |
| 158 | + hello: 'こんにちは、{name}!' |
| 159 | + } |
| 160 | + } |
| 161 | + }) |
| 162 | + |
| 163 | + const App = defineComponent({ |
| 164 | + setup() { |
| 165 | + // <p v-t="{ path: 'hello', locale: 'ja', args: { name: name.value } }"></p> |
| 166 | + const name = ref('kazupon') |
| 167 | + const t = resolveDirective('t') |
| 168 | + return () => { |
| 169 | + return withDirectives(h('p'), [ |
| 170 | + [t!, { path: 'hello', locale: 'ja', args: { name: name.value } }] |
| 171 | + ]) |
| 172 | + } |
| 173 | + } |
| 174 | + }) |
| 175 | + const app = createSSRApp(App) |
| 176 | + app.use(i18n) |
| 177 | + |
| 178 | + expect(await renderToString(app)).toEqual('<p>こんにちは、kazupon!</p>') |
| 179 | + }) |
| 180 | + |
| 181 | + test('plural', async () => { |
| 182 | + const i18n = createI18n({ |
| 183 | + locale: 'en', |
| 184 | + messages: { |
| 185 | + en: { |
| 186 | + banana: 'no bananas | {n} banana | {n} bananas' |
| 187 | + } |
| 188 | + } |
| 189 | + }) |
| 190 | + |
| 191 | + const App = defineComponent({ |
| 192 | + setup() { |
| 193 | + // <p v-t="{ path: 'banana', choice: 2 }"></p> |
| 194 | + const t = resolveDirective('t') |
| 195 | + return () => { |
| 196 | + return withDirectives(h('p'), [[t!, { path: 'banana', choice: 2 }]]) |
| 197 | + } |
| 198 | + } |
| 199 | + }) |
| 200 | + const app = createSSRApp(App) |
| 201 | + app.use(i18n) |
| 202 | + |
| 203 | + expect(await renderToString(app)).toEqual('<p>2 bananas</p>') |
| 204 | + }) |
| 205 | +}) |
0 commit comments