Skip to content

Commit 3697079

Browse files
authored
improvement: tweaks interface for devtools (#104)
1 parent d6f12ed commit 3697079

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/i18n.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ import { isEmptyObject, warn } from './utils'
3131
import { devtoolsRegisterI18n } from './devtools'
3232
import { VERSION } from './misc'
3333

34+
declare module '@vue/runtime-core' {
35+
// eslint-disable-next-line
36+
interface App<HostElement = any> {
37+
__VUE_I18N__?: I18n & I18nInternal
38+
}
39+
}
40+
3441
/**
3542
* I18n Options for `createI18n`
3643
*
@@ -89,6 +96,7 @@ export interface I18n<Messages = {}, DateTimeFormats = {}, NumberFormats = {}> {
8996
* @internal
9097
*/
9198
export interface I18nInternal {
99+
__instances: Map<ComponentInternalInstance, VueI18n | Composer>
92100
__getInstance<
93101
Messages,
94102
DateTimeFormats,
@@ -248,7 +256,11 @@ export function createI18n<
248256
get mode(): I18nMode {
249257
return __legacyMode ? 'legacy' : 'composable'
250258
},
259+
// install plugin
251260
install(app: App, ...options: unknown[]): void {
261+
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
262+
app.__VUE_I18N__ = i18n as I18n & I18nInternal
263+
}
252264
apply<Messages, DateTimeFormats, NumberFormats>(app, i18n, ...options)
253265
if (__legacyMode) {
254266
app.mixin(
@@ -264,6 +276,7 @@ export function createI18n<
264276
)
265277
}
266278
},
279+
// global composer accsessor
267280
get global(): Composer<Messages, DateTimeFormats, NumberFormats> {
268281
return __legacyMode
269282
? (((__global as unknown) as VueI18nInternal<
@@ -273,18 +286,23 @@ export function createI18n<
273286
>).__composer as Composer<Messages, DateTimeFormats, NumberFormats>)
274287
: (__global as Composer<Messages, DateTimeFormats, NumberFormats>)
275288
},
289+
// @internal
290+
__instances,
291+
// @internal
276292
__getInstance<
277293
M extends Messages,
278294
Instance extends VueI18n<M> | Composer<M>
279295
>(component: ComponentInternalInstance): Instance | null {
280296
return ((__instances.get(component) as unknown) as Instance) || null
281297
},
298+
// @internal
282299
__setInstance<
283300
M extends Messages,
284301
Instance extends VueI18n<M> | Composer<M>
285302
>(component: ComponentInternalInstance, instance: Instance): void {
286303
__instances.set(component, instance)
287304
},
305+
// @internal
288306
__deleteInstance(component: ComponentInternalInstance): void {
289307
__instances.delete(component)
290308
}
@@ -504,15 +522,19 @@ function setupLifeCycle<Messages, DateTimeFormats, NumberFormats>(
504522
): void {
505523
onMounted(() => {
506524
// inject composer instance to DOM for intlify-devtools
507-
if (target.vnode.el) {
508-
target.vnode.el.__intlify__ = composer
525+
if ((__DEV__ || __FEATURE_PROD_DEVTOOLS__) && target.vnode.el) {
526+
target.vnode.el.__INTLIFY__ = composer
509527
}
510528
}, target)
511529

512530
onUnmounted(() => {
513531
// remove composer instance from DOM for intlify-devtools
514-
if (target.vnode.el && target.vnode.el.__INTLIFY__) {
515-
delete target.vnode.el.__intlify__
532+
if (
533+
(__DEV__ || __FEATURE_PROD_DEVTOOLS__) &&
534+
target.vnode.el &&
535+
target.vnode.el.__INTLIFY__
536+
) {
537+
delete target.vnode.el.__INTLIFY__
516538
}
517539
i18n.__deleteInstance(target)
518540
}, target)

src/mixin.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ export function defineMixin<Messages, DateTimeFormats, NumberFormats>(
230230
},
231231

232232
mounted(): void {
233-
this.$el.__INTLIFY__ = this.$i18n.__composer
233+
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
234+
this.$el.__INTLIFY__ = this.$i18n.__composer
235+
}
234236
},
235237

236238
beforeDestroy(): void {
@@ -240,7 +242,9 @@ export function defineMixin<Messages, DateTimeFormats, NumberFormats>(
240242
throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR)
241243
}
242244

243-
delete this.$el.__INTLIFY__
245+
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
246+
delete this.$el.__INTLIFY__
247+
}
244248

245249
delete this.$t
246250
delete this.$tc

0 commit comments

Comments
 (0)