Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ export {
UseI18nOptions
} from './i18n'
export {
Translation,
TranslationProps,
NumberFormat,
NumberFormatProps,
DatetimeFormat,
DatetimeFormatProps,
FormattableProps,
BaseFormatProps,
Expand Down
21 changes: 15 additions & 6 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { I18nSymbol, I18n, I18nInternal } from './i18n'
import { Translation, NumberFormat, DatetimeFormat } from './components'
import { vTDirective } from './directive'
import { I18nWarnCodes, getWarnMessage } from './warnings'
import { isPlainObject, warn } from './utils'
import { isPlainObject, warn, isBoolean } from './utils'

/**
* I18n plugin options
Expand All @@ -13,6 +13,7 @@ import { isPlainObject, warn } from './utils'
*/
export interface I18nPluginOptions {
useI18nComponentName?: boolean
globalInstall?: boolean
}

export function apply(
Expand All @@ -24,19 +25,27 @@ export function apply(
? (options[0] as I18nPluginOptions)
: {}
const useI18nComponentName = !!pluginOptions.useI18nComponentName
const globalInstall = isBoolean(pluginOptions.globalInstall)
? pluginOptions.globalInstall
: true

if (__DEV__ && useI18nComponentName) {
if (__DEV__ && globalInstall && useI18nComponentName) {
warn(
getWarnMessage(I18nWarnCodes.COMPONENT_NAME_LEGACY_COMPATIBLE, {
name: Translation.name
})
)
}

// install components
app.component(!useI18nComponentName ? Translation.name : 'i18n', Translation)
app.component(NumberFormat.name, NumberFormat)
app.component(DatetimeFormat.name, DatetimeFormat)
if (globalInstall) {
// install components
app.component(
!useI18nComponentName ? Translation.name : 'i18n',
Translation
)
app.component(NumberFormat.name, NumberFormat)
app.component(DatetimeFormat.name, DatetimeFormat)
}

// install directive
app.directive('t', vTDirective(i18n))
Expand Down
20 changes: 20 additions & 0 deletions test/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,24 @@ describe('useI18nComponentName option', () => {
})
})

describe('globalInstall option', () => {
test('default', () => {
const app = createApp({})
const i18n = {} as I18n & I18nInternal
const spy = jest.spyOn(app, 'component')

apply(app, i18n)
expect(spy).toHaveBeenCalledTimes(3)
})

test('false', () => {
const app = createApp({})
const i18n = {} as I18n & I18nInternal
const spy = jest.spyOn(app, 'component')

apply(app, i18n, { globalInstall: false })
expect(spy).not.toHaveBeenCalled()
})
})

/* eslint-enable @typescript-eslint/no-empty-function */