Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing composer instance to Translation component #453

Merged
merged 6 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/vue-i18n/src/components/DatetimeFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ export const DatetimeFormat = {
),
/* eslint-enable */
setup(props: DatetimeFormatProps, context: SetupContext): RenderFunction {
const i18n = useI18n({ useScope: 'parent' }) as Composer & ComposerInternal
const i18n =
props.i18n ||
(useI18n({ useScope: 'parent' }) as Composer & ComposerInternal)

return renderFormatter<
FormattableProps<number | Date, Intl.DateTimeFormatOptions>,
Expand Down
4 changes: 3 additions & 1 deletion packages/vue-i18n/src/components/NumberFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ export const NumberFormat = {
),
/* eslint-enable */
setup(props: NumberFormatProps, context: SetupContext): RenderFunction {
const i18n = useI18n({ useScope: 'parent' }) as Composer & ComposerInternal
const i18n =
props.i18n ||
(useI18n({ useScope: 'parent' }) as Composer & ComposerInternal)

return renderFormatter<
FormattableProps<number, Intl.NumberFormatOptions>,
Expand Down
5 changes: 3 additions & 2 deletions packages/vue-i18n/src/components/Translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ export const Translation = {
/* eslint-enable */
setup(props: TranslationProps, context: SetupContext): RenderFunction {
const { slots, attrs } = context
const i18n = useI18n({ useScope: props.scope }) as Composer &
ComposerInternal
const i18n =
props.i18n ||
(useI18n({ useScope: props.scope }) as Composer & ComposerInternal)
const keys = Object.keys(slots).filter(key => key !== '_')

return (): VNodeChild => {
Expand Down
10 changes: 9 additions & 1 deletion packages/vue-i18n/src/components/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { Locale } from '@intlify/core-base'
import { PropType } from '@vue/runtime-core'
import { Composer } from '../composer'
import type { I18nScope } from '../i18n'

export type ComponetI18nScope = Exclude<I18nScope, 'local'>
Expand Down Expand Up @@ -37,6 +39,11 @@ export interface BaseFormatProps {
* If the parent is a global scope, the global scope is used, if it's a local scope, the local scope is used.
*/
scope?: ComponetI18nScope
/**
* @remarks
* An existing i18n Composer instance to use for translating
*/
i18n?: Composer
}

export const baseFormatProps = {
Expand All @@ -51,5 +58,6 @@ export const baseFormatProps = {
validator: (val: ComponetI18nScope): boolean =>
val === 'parent' || val === 'global',
default: 'parent' as ComponetI18nScope
}
},
i18n: { type: Object as PropType<Composer> }
}