Skip to content

Commit 1c8043b

Browse files
authored
feat: export type prop from runtime (#188)
1 parent ad53bc1 commit 1c8043b

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/composer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,8 @@ export function createComposer<
759759
const interpolate = (val: unknown): MessageType<VNode> => val as VNode
760760
const processor = {
761761
normalize,
762-
interpolate
762+
interpolate,
763+
type: 'vnode'
763764
} as MessageProcessor<VNode>
764765

765766
// __transrateVNode, using for `i18n-t` component

src/message/runtime.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export const enum HelperNameMap {
8383
PLURAL = 'plural',
8484
LINKED = 'linked',
8585
MESSAGE = 'message',
86+
TYPE = 'type',
8687
INTERPOLATE = 'interpolate',
8788
NORMALIZE = 'normalize'
8889
}
@@ -94,6 +95,7 @@ export interface MessageContext<T = string> {
9495
plural(messages: T[]): T
9596
linked(key: Path, modifier?: string): MessageType<T>
9697
message(key: Path): MessageFunction<T>
98+
type: string
9799
interpolate: MessageInterpolate<T>
98100
normalize: MessageNormalize<T>
99101
}
@@ -202,6 +204,11 @@ export function createMessageContext<T = string, N = {}>(
202204
? options.processor.interpolate
203205
: ((DEFAULT_INTERPOLATE as unknown) as MessageInterpolate<T>)
204206

207+
const type =
208+
isPlainObject(options.processor) && isString(options.processor.type)
209+
? options.processor.type
210+
: DEFAULT_MESSAGE_DATA_TYPE
211+
205212
const ctx = {
206213
[HelperNameMap.LIST]: list,
207214
[HelperNameMap.NAMED]: named,
@@ -212,6 +219,7 @@ export function createMessageContext<T = string, N = {}>(
212219
return isString(modifier) ? _modifier(modifier)(msg as T) : msg
213220
},
214221
[HelperNameMap.MESSAGE]: message,
222+
[HelperNameMap.TYPE]: type,
215223
[HelperNameMap.INTERPOLATE]: interpolate,
216224
[HelperNameMap.NORMALIZE]: normalize
217225
}

test/message/runtime.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,11 @@ describe('custom process', () => {
280280
const ctx = createMessageContext<string | MockVNode>({
281281
processor: {
282282
normalize,
283-
interpolate
283+
interpolate,
284+
type: 'mock'
284285
}
285286
})
287+
expect(ctx.type).toEqual('mock')
286288
expect(msg(ctx)).toMatchSnapshot()
287289
})
288290

0 commit comments

Comments
 (0)