Skip to content

Commit 9f00161

Browse files
authored
improvement: display message function details (#250)
1 parent b3fc823 commit 9f00161

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

packages/vue-i18n/src/devtools.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
DevToolsTimelineEvents,
88
DevToolsTimelineLayerMaps
99
} from '@intlify/core-base'
10+
import { isFunction, isObject } from '@intlify/shared'
1011

1112
import type { App } from 'vue'
1213
import type {
@@ -194,7 +195,7 @@ function inspectComposer(
194195
type,
195196
key: 'messages',
196197
editable: false,
197-
value: composer.messages.value
198+
value: getLocaleMessageValue(composer.messages.value)
198199
})
199200
instanceData.state.push({
200201
type,
@@ -210,6 +211,48 @@ function inspectComposer(
210211
})
211212
}
212213

214+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
215+
function getLocaleMessageValue(messages: any): Record<string, unknown> {
216+
const value: Record<string, unknown> = {}
217+
Object.keys(messages).forEach((key: string) => {
218+
const v: unknown = messages[key]
219+
if (isFunction(v) && 'source' in v) {
220+
value[key] = getMessageFunctionDetails(v)
221+
} else if (isObject(v)) {
222+
value[key] = getLocaleMessageValue(v)
223+
} else {
224+
value[key] = v
225+
}
226+
})
227+
return value
228+
}
229+
230+
const ESC: Record<string, string> = {
231+
'<': '&lt;',
232+
'>': '&gt;',
233+
'"': '&quot;',
234+
'&': '&amp;'
235+
}
236+
237+
function escape(s: string): string {
238+
return s.replace(/[<>"&]/g, escapeChar)
239+
}
240+
241+
function escapeChar(a: string): string {
242+
return ESC[a] || a
243+
}
244+
245+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
246+
function getMessageFunctionDetails(func: any): Record<string, unknown> {
247+
const argString = func.source ? `("${escape(func.source)}")` : `(?)`
248+
return {
249+
_custom: {
250+
type: 'function',
251+
display: `<span>ƒ</span> ${argString}`
252+
}
253+
}
254+
}
255+
213256
function registerScope<
214257
Messages,
215258
DateTimeFormats,
@@ -308,7 +351,7 @@ function makeScopeInspectState(composer: Composer): CustomInspectorState {
308351
type: localeMessagesType,
309352
key: 'messages',
310353
editable: false,
311-
value: composer.messages.value
354+
value: getLocaleMessageValue(composer.messages.value)
312355
}
313356
]
314357
state[localeMessagesType] = localeMessagesStates

0 commit comments

Comments
 (0)