|
| 1 | +import { defineComponent, createSSRApp } from 'vue' |
| 2 | +import { renderToString } from '@vue/server-renderer' |
| 3 | +import { createI18n, useI18n } from '../src/index' |
| 4 | + |
| 5 | +test('composable mode', async () => { |
| 6 | + const i18n = createI18n({ |
| 7 | + locale: 'en', |
| 8 | + messages: {} |
| 9 | + }) |
| 10 | + |
| 11 | + const App = defineComponent({ |
| 12 | + template: `<p>{{ t('hello') }}</p>`, |
| 13 | + setup() { |
| 14 | + return useI18n({ |
| 15 | + locale: 'ja', |
| 16 | + inheritLocale: false, |
| 17 | + messages: { |
| 18 | + ja: { hello: 'こんにちは!' }, |
| 19 | + en: { hello: 'hello!' } |
| 20 | + } |
| 21 | + }) |
| 22 | + } |
| 23 | + }) |
| 24 | + const app = createSSRApp(App) |
| 25 | + app.use(i18n) |
| 26 | + |
| 27 | + expect(await renderToString(app)).toMatch(`<p>こんにちは!</p>`) |
| 28 | +}) |
| 29 | + |
| 30 | +test('legacy mode', async () => { |
| 31 | + const i18n = createI18n({ |
| 32 | + legacy: true, |
| 33 | + locale: 'ja', |
| 34 | + messages: { |
| 35 | + ja: { hello: 'こんにちは!' }, |
| 36 | + en: { hello: 'hello!' } |
| 37 | + } |
| 38 | + }) |
| 39 | + |
| 40 | + const App = defineComponent({ |
| 41 | + template: `<p>{{ $t('hello') }}</p>` |
| 42 | + }) |
| 43 | + const app = createSSRApp(App) |
| 44 | + app.use(i18n) |
| 45 | + |
| 46 | + expect(await renderToString(app)).toMatch(`<p>こんにちは!</p>`) |
| 47 | +}) |
| 48 | + |
| 49 | +test('component: i18n-t', async () => { |
| 50 | + const i18n = createI18n({ |
| 51 | + locale: 'en', |
| 52 | + messages: {} |
| 53 | + }) |
| 54 | + |
| 55 | + const App = defineComponent({ |
| 56 | + template: `<i18n-t tag="p" keypath="hello"/>`, |
| 57 | + setup() { |
| 58 | + return useI18n({ |
| 59 | + locale: 'ja', |
| 60 | + inheritLocale: false, |
| 61 | + messages: { |
| 62 | + ja: { hello: 'こんにちは!' }, |
| 63 | + en: { hello: 'hello!' } |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
| 67 | + }) |
| 68 | + const app = createSSRApp(App) |
| 69 | + app.use(i18n) |
| 70 | + |
| 71 | + expect(await renderToString(app)).toMatch(`<p>こんにちは!</p>`) |
| 72 | +}) |
0 commit comments