Skip to content

Commit b76d40d

Browse files
Support passing composer instance to Translation component (#453)
* Support passing composer instance to <i18n-t> Necessary for using the scope of the compilation parent component when placed inside a slot of a custom component. * Fix formatting error * Add i18n prop to TranslationProps definition * Use OR operator for fallback value * Move i18n prop to BaseFormatProps * Remove unnecesary prop entry
1 parent 0f08367 commit b76d40d

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ export const DatetimeFormat = {
7676
),
7777
/* eslint-enable */
7878
setup(props: DatetimeFormatProps, context: SetupContext): RenderFunction {
79-
const i18n = useI18n({ useScope: 'parent' }) as Composer & ComposerInternal
79+
const i18n =
80+
props.i18n ||
81+
(useI18n({ useScope: 'parent' }) as Composer & ComposerInternal)
8082

8183
return renderFormatter<
8284
FormattableProps<number | Date, Intl.DateTimeFormatOptions>,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ export const NumberFormat = {
7171
),
7272
/* eslint-enable */
7373
setup(props: NumberFormatProps, context: SetupContext): RenderFunction {
74-
const i18n = useI18n({ useScope: 'parent' }) as Composer & ComposerInternal
74+
const i18n =
75+
props.i18n ||
76+
(useI18n({ useScope: 'parent' }) as Composer & ComposerInternal)
7577

7678
return renderFormatter<
7779
FormattableProps<number, Intl.NumberFormatOptions>,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ export const Translation = {
9898
/* eslint-enable */
9999
setup(props: TranslationProps, context: SetupContext): RenderFunction {
100100
const { slots, attrs } = context
101-
const i18n = useI18n({ useScope: props.scope }) as Composer &
102-
ComposerInternal
101+
const i18n =
102+
props.i18n ||
103+
(useI18n({ useScope: props.scope }) as Composer & ComposerInternal)
103104
const keys = Object.keys(slots).filter(key => key !== '_')
104105

105106
return (): VNodeChild => {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { Locale } from '@intlify/core-base'
2+
import { PropType } from '@vue/runtime-core'
3+
import { Composer } from '../composer'
24
import type { I18nScope } from '../i18n'
35

46
export type ComponetI18nScope = Exclude<I18nScope, 'local'>
@@ -37,6 +39,11 @@ export interface BaseFormatProps {
3739
* If the parent is a global scope, the global scope is used, if it's a local scope, the local scope is used.
3840
*/
3941
scope?: ComponetI18nScope
42+
/**
43+
* @remarks
44+
* An existing i18n Composer instance to use for translating
45+
*/
46+
i18n?: Composer
4047
}
4148

4249
export const baseFormatProps = {
@@ -51,5 +58,6 @@ export const baseFormatProps = {
5158
validator: (val: ComponetI18nScope): boolean =>
5259
val === 'parent' || val === 'global',
5360
default: 'parent' as ComponetI18nScope
54-
}
61+
},
62+
i18n: { type: Object as PropType<Composer> }
5563
}

0 commit comments

Comments
 (0)