Skip to content

Commit 74c4396

Browse files
kazuponDannyFeliz
andauthored
support for Vue 3.1 (#578)
* support Vue 3.1 * Update packages/vue-i18n/src/components/NumberFormat.ts Co-authored-by: Danny Feliz <[email protected]> * Update packages/vue-i18n/src/components/DatetimeFormat.ts Co-authored-by: Danny Feliz <[email protected]> Co-authored-by: Danny Feliz <[email protected]>
1 parent ea3682f commit 74c4396

File tree

8 files changed

+167
-37
lines changed

8 files changed

+167
-37
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"@types/node": "^14.14.37",
7070
"@typescript-eslint/eslint-plugin": "^4.28.0",
7171
"@typescript-eslint/parser": "^4.28.0",
72-
"@vue/server-renderer": "^3.0.0",
72+
"@vue/server-renderer": "^3.1.0",
7373
"algoliasearch": "^4.9.0",
7474
"api-docs-gen": "^0.3.0",
7575
"brotli": "^1.3.2",
@@ -126,7 +126,7 @@
126126
"typescript": "4.2.3",
127127
"typescript-eslint-language-service": "^4.1.3",
128128
"vitepress": "^0.14.0",
129-
"vue": "^3.0.0",
129+
"vue": "^3.1.0",
130130
"yarn-deduplicate": "^3.1.0",
131131
"yorkie": "^2.0.0"
132132
},

packages/vue-i18n/src/components/DatetimeFormat.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,24 @@ import { DatetimePartsSymbol } from '../composer'
33
import { renderFormatter } from './formatRenderer'
44
import { baseFormatProps } from './base'
55
import { assign } from '@intlify/shared'
6+
import { defineComponent } from 'vue'
67

7-
import type { RenderFunction, SetupContext } from 'vue'
8+
/* eslint-disable @typescript-eslint/no-unused-vars */
9+
import type {
10+
RenderFunction,
11+
// NOTE: avoid https://github.com/microsoft/rushstack/issues/1050
12+
// @ts-ignore
13+
DefineComponent,
14+
// @ts-ignore
15+
ComponentOptionsMixin,
16+
// @ts-ignore
17+
AllowedComponentProps,
18+
// @ts-ignore
19+
VNodeProps,
20+
// @ts-ignore
21+
ComponentCustomProps
22+
} from 'vue'
23+
/* eslint-enable @typescript-eslint/no-unused-vars */
824
import type { DateTimeOptions } from '@intlify/core-base'
925
import type { Composer, ComposerInternal } from '../composer'
1026
import type { FormattableProps } from './formatRenderer'
@@ -59,7 +75,7 @@ const DATETIME_FORMAT_KEYS = [
5975
*
6076
* @VueI18nComponent
6177
*/
62-
export const DatetimeFormat = {
78+
export const DatetimeFormat = /* #__PURE__*/ defineComponent({
6379
/* eslint-disable */
6480
name: 'i18n-d',
6581
props: assign(
@@ -75,7 +91,7 @@ export const DatetimeFormat = {
7591
baseFormatProps
7692
),
7793
/* eslint-enable */
78-
setup(props: DatetimeFormatProps, context: SetupContext): RenderFunction {
94+
setup(props, context): RenderFunction {
7995
const i18n =
8096
props.i18n ||
8197
(useI18n({ useScope: 'parent' }) as Composer & ComposerInternal)
@@ -86,9 +102,13 @@ export const DatetimeFormat = {
86102
Intl.DateTimeFormatOptions,
87103
DateTimeOptions,
88104
Intl.DateTimeFormatPart
89-
>(props, context, DATETIME_FORMAT_KEYS, (...args: unknown[]) =>
90-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
91-
(i18n as any)[DatetimePartsSymbol](...args)
105+
>(
106+
props as DatetimeFormatProps,
107+
context,
108+
DATETIME_FORMAT_KEYS,
109+
(...args: unknown[]) =>
110+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
111+
(i18n as any)[DatetimePartsSymbol](...args)
92112
)
93113
}
94-
}
114+
})

packages/vue-i18n/src/components/NumberFormat.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,24 @@ import { NumberPartsSymbol } from '../composer'
33
import { renderFormatter } from './formatRenderer'
44
import { baseFormatProps } from './base'
55
import { assign } from '@intlify/shared'
6+
import { defineComponent } from 'vue'
67

7-
import type { SetupContext, RenderFunction } from 'vue'
8+
/* eslint-disable @typescript-eslint/no-unused-vars */
9+
import type {
10+
RenderFunction,
11+
// NOTE: avoid https://github.com/microsoft/rushstack/issues/1050
12+
// @ts-ignore
13+
DefineComponent,
14+
// @ts-ignore
15+
ComponentOptionsMixin,
16+
// @ts-ignore
17+
AllowedComponentProps,
18+
// @ts-ignore
19+
VNodeProps,
20+
// @ts-ignore
21+
ComponentCustomProps
22+
} from 'vue'
23+
/* eslint-enable @typescript-eslint/no-unused-vars */
824
import type { NumberOptions } from '@intlify/core-base'
925
import type { Composer, ComposerInternal } from '../composer'
1026
import type { FormattableProps } from './formatRenderer'
@@ -54,7 +70,7 @@ const NUMBER_FORMAT_KEYS = [
5470
*
5571
* @VueI18nComponent
5672
*/
57-
export const NumberFormat = {
73+
export const NumberFormat = /* #__PURE__*/ defineComponent({
5874
/* eslint-disable */
5975
name: 'i18n-n',
6076
props: assign(
@@ -70,7 +86,7 @@ export const NumberFormat = {
7086
baseFormatProps
7187
),
7288
/* eslint-enable */
73-
setup(props: NumberFormatProps, context: SetupContext): RenderFunction {
89+
setup(props, context): RenderFunction {
7490
const i18n =
7591
props.i18n ||
7692
(useI18n({ useScope: 'parent' }) as Composer & ComposerInternal)
@@ -81,9 +97,13 @@ export const NumberFormat = {
8197
Intl.NumberFormatOptions,
8298
NumberOptions,
8399
Intl.NumberFormatPart
84-
>(props, context, NUMBER_FORMAT_KEYS, (...args: unknown[]) =>
85-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
86-
(i18n as any)[NumberPartsSymbol](...args)
100+
>(
101+
props as NumberFormatProps,
102+
context,
103+
NUMBER_FORMAT_KEYS,
104+
(...args: unknown[]) =>
105+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
106+
(i18n as any)[NumberPartsSymbol](...args)
87107
)
88108
}
89-
}
109+
})

packages/vue-i18n/src/components/Translation.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1-
import { h, Fragment } from 'vue'
1+
import { h, Fragment, defineComponent } from 'vue'
22
import { isNumber, isString, isObject } from '@intlify/shared'
33
import { TransrateVNodeSymbol } from '../composer'
44
import { useI18n } from '../i18n'
55
import { baseFormatProps } from './base'
66
import { assign } from '@intlify/shared'
77

8-
import type { SetupContext, VNodeChild, RenderFunction } from 'vue'
8+
/* eslint-disable @typescript-eslint/no-unused-vars */
9+
import type {
10+
SetupContext,
11+
VNodeChild,
12+
// NOTE: avoid https://github.com/microsoft/rushstack/issues/1050
13+
// @ts-ignore
14+
DefineComponent,
15+
// @ts-ignore
16+
ComponentOptionsMixin,
17+
// @ts-ignore
18+
AllowedComponentProps,
19+
// @ts-ignore
20+
VNodeProps,
21+
// @ts-ignore
22+
ComponentCustomProps
23+
} from 'vue'
24+
/* eslint-enable @typescript-eslint/no-unused-vars */
925
import type { Composer, ComposerInternal } from '../composer'
1026
import type { TranslateOptions } from '@intlify/core-base'
1127
import type { NamedValue } from '@intlify/runtime'
@@ -78,7 +94,7 @@ export interface TranslationProps extends BaseFormatProps {
7894
*
7995
* @VueI18nComponent
8096
*/
81-
export const Translation = {
97+
export const Translation = /* #__PURE__*/ defineComponent({
8298
/* eslint-disable */
8399
name: 'i18n-t',
84100
props: assign(
@@ -96,11 +112,13 @@ export const Translation = {
96112
baseFormatProps
97113
),
98114
/* eslint-enable */
99-
setup(props: TranslationProps, context: SetupContext): RenderFunction {
115+
setup(props, context) {
100116
const { slots, attrs } = context
117+
// NOTE: avoid https://github.com/microsoft/rushstack/issues/1050
101118
const i18n =
102119
props.i18n ||
103-
(useI18n({ useScope: props.scope }) as Composer & ComposerInternal)
120+
(useI18n({ useScope: props.scope as 'global' | 'parent' }) as Composer &
121+
ComposerInternal)
104122
const keys = Object.keys(slots).filter(key => key !== '_')
105123

106124
return (): VNodeChild => {
@@ -124,7 +142,7 @@ export const Translation = {
124142
: h(Fragment, assignedAttrs, children)
125143
}
126144
}
127-
}
145+
})
128146

129147
function getInterpolateArg(
130148
{ slots }: SetupContext,

packages/vue-i18n/src/plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ export function apply(app: App, i18n: I18n, ...options: unknown[]): void {
6060
// install components
6161
app.component(
6262
!useI18nComponentName ? Translation.name : 'i18n',
63-
Translation as Component
63+
Translation
6464
)
65-
app.component(NumberFormat.name, NumberFormat as Component)
65+
app.component(NumberFormat.name, NumberFormat)
6666
app.component(DatetimeFormat.name, DatetimeFormat as Component)
6767
}
6868

packages/vue-i18n/test/__snapshots__/composer.test.ts.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ Array [
2222
"slotScopeIds": null,
2323
"ssContent": null,
2424
"ssFallback": null,
25-
"staticCount": 0,
2625
"suspense": null,
2726
"target": null,
2827
"targetAnchor": null,

packages/vue-i18n/test/ssr.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineComponent, createSSRApp } from 'vue'
1+
import { h, defineComponent, resolveComponent, createSSRApp } from 'vue'
22
import { renderToString } from '@vue/server-renderer'
33
import { createI18n, useI18n } from '../src/index'
44

@@ -11,16 +11,16 @@ test('composition mode', async () => {
1111

1212
const App = defineComponent({
1313
setup() {
14-
return useI18n({
14+
const { t } = useI18n({
1515
locale: 'ja',
1616
inheritLocale: false,
1717
messages: {
1818
ja: { hello: 'こんにちは!' },
1919
en: { hello: 'hello!' }
2020
}
2121
})
22-
},
23-
template: `<p>{{ t('hello') }}</p>`
22+
return () => h('p', t('hello'))
23+
}
2424
})
2525
const app = createSSRApp(App)
2626
app.use(i18n)
@@ -37,9 +37,8 @@ test('legacy mode', async () => {
3737
}
3838
})
3939

40-
const App = defineComponent({
41-
template: `<p>{{ $t('hello') }}</p>`
42-
})
40+
// NOTE: template: `<p>{{ $t('hello') }}</p>`
41+
const App = () => h('p', i18n.global.t('hello'))
4342
const app = createSSRApp(App)
4443
app.use(i18n)
4544

@@ -55,16 +54,17 @@ test('component: i18n-t', async () => {
5554

5655
const App = defineComponent({
5756
setup() {
58-
return useI18n({
57+
useI18n({
5958
locale: 'ja',
6059
inheritLocale: false,
6160
messages: {
6261
ja: { hello: 'こんにちは!' },
6362
en: { hello: 'hello!' }
6463
}
6564
})
66-
},
67-
template: `<i18n-t tag="p" keypath="hello"/>`
65+
return () => h(resolveComponent('i18n-t'), { tag: 'p', keypath: 'hello' })
66+
}
67+
// template: `<i18n-t tag="p" keypath="hello"/>`
6868
})
6969
const app = createSSRApp(App)
7070
app.use(i18n)

0 commit comments

Comments
 (0)