Skip to content

Commit 99aff78

Browse files
authored
fix: allow AST on rt (#1455)
* fix: allow AST on rt * fix: wrong types
1 parent 09fd304 commit 99aff78

File tree

6 files changed

+175
-13
lines changed

6 files changed

+175
-13
lines changed

packages/core-base/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { initFeatureFlags } from './misc'
33
export {
44
CompileError,
55
CompileErrorCodes,
6+
ResourceNode,
67
createCompileError
78
} from '@intlify/message-compiler'
89
export * from './resolver'

packages/core-base/src/translate.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ export function translate<
390390
// resolve message format
391391
// eslint-disable-next-line prefer-const
392392
let [formatScope, targetLocale, message]: [
393-
PathValue | MessageFunction<Message>,
393+
PathValue | MessageFunction<Message> | ResourceNode,
394394
Locale | undefined,
395395
LocaleMessageValue<Message>
396396
] = !resolvedMessage
@@ -657,7 +657,7 @@ function compileMessageFormat<Messages, Message>(
657657
context: CoreContext<Message, Messages>,
658658
key: string,
659659
targetLocale: string,
660-
format: PathValue,
660+
format: PathValue | ResourceNode | MessageFunction<Message>,
661661
cacheBaseKey: string,
662662
errorDetector: () => void
663663
): MessageFunctionInternal {
@@ -707,7 +707,7 @@ function compileMessageFormat<Messages, Message>(
707707
if (emitter && start) {
708708
emitter.emit(VueDevToolsTimelineEvents.MESSAGE_COMPILATION, {
709709
type: VueDevToolsTimelineEvents.MESSAGE_COMPILATION,
710-
message: format,
710+
message: format as string | ResourceNode | MessageFunction,
711711
time: end - start,
712712
groupId: `${'translate'}:${key}`
713713
})
@@ -767,11 +767,16 @@ function evaluateMessage<Messages, Message>(
767767
/** @internal */
768768
export function parseTranslateArgs<Message = string>(
769769
...args: unknown[]
770-
): [Path | MessageFunction<Message>, TranslateOptions] {
770+
): [Path | MessageFunction<Message> | ResourceNode, TranslateOptions] {
771771
const [arg1, arg2, arg3] = args
772772
const options = {} as TranslateOptions
773773

774-
if (!isString(arg1) && !isNumber(arg1) && !isMessageFunction(arg1)) {
774+
if (
775+
!isString(arg1) &&
776+
!isNumber(arg1) &&
777+
!isMessageFunction(arg1) &&
778+
!isMessageAST(arg1)
779+
) {
775780
throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT)
776781
}
777782

packages/vue-devtools/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import type {
33
Path,
44
PathValue,
55
Locale,
6+
MessageFunction,
7+
ResourceNode,
68
CoreMissingType
79
} from '@intlify/core-base'
810

@@ -37,7 +39,7 @@ export const enum VueDevToolsTimelineEvents {
3739

3840
export type VueDevToolsTimelineEventPayloads = {
3941
[VueDevToolsTimelineEvents.COMPILE_ERROR]: {
40-
message: PathValue
42+
message: string
4143
error: string
4244
start?: number
4345
end?: number
@@ -65,7 +67,7 @@ export type VueDevToolsTimelineEventPayloads = {
6567
}
6668
[VueDevToolsTimelineEvents.MESSAGE_COMPILATION]: {
6769
type: VueDevToolsTimelineEvents.MESSAGE_COMPILATION
68-
message: PathValue
70+
message: string | ResourceNode | MessageFunction
6971
time: number
7072
groupId?: string
7173
}

packages/vue-i18n-core/src/composer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ import type {
9999
LocaleParams,
100100
ResourceValue,
101101
ResourcePath,
102+
ResourceNode,
102103
PickupPaths,
103104
PickupFormatPathKeys,
104105
RemoveIndexSignature,
@@ -114,7 +115,7 @@ export { DEFAULT_LOCALE } from '@intlify/core-base'
114115
export const DEVTOOLS_META = '__INTLIFY_META__'
115116

116117
/** @VueI18nComposition */
117-
export type VueMessageType = string | VNode
118+
export type VueMessageType = string | ResourceNode | VNode
118119

119120
/**
120121
* The type definition of Locale Message

packages/vue-i18n-core/test/composer.test.ts

Lines changed: 156 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { watch, watchEffect, nextTick, Text, createVNode } from 'vue'
3030
import {
3131
Locale,
3232
compileToFunction,
33+
compile,
3334
registerMessageCompiler,
3435
resolveValue,
3536
registerMessageResolver,
@@ -537,7 +538,7 @@ describe('postTranslation', () => {
537538
expect(getPostTranslationHandler()).toEqual(null)
538539

539540
let key = ''
540-
const handler = (str: VueMessageType, _key: string) => {
541+
const handler = <VueMessageType>(str: VueMessageType, _key: string) => {
541542
key = _key
542543
return shared.isString(str) ? str.trim() : str
543544
}
@@ -548,7 +549,7 @@ describe('postTranslation', () => {
548549
})
549550

550551
test('initialize at composer creating', () => {
551-
const handler = (str: VueMessageType) =>
552+
const handler = <VueMessageType>(str: VueMessageType) =>
552553
shared.isString(str) ? str.trim() : str
553554
const { getPostTranslationHandler, t } = createComposer({
554555
locale: 'en',
@@ -774,7 +775,159 @@ describe('rt', () => {
774775
'no apples',
775776
'one apple',
776777
`${ctx.named('count')} apples`
777-
])
778+
]) as string
779+
}
780+
}
781+
})
782+
783+
expect(rt(messages.value.en.text)).toEqual('hi dio!')
784+
expect(rt(messages.value.en.list, ['dio'])).toEqual('hi dio!')
785+
expect(rt(messages.value.en.named, { name: 'dio' })).toEqual('hi dio!')
786+
expect(rt(messages.value.en.linked)).toEqual('hi DIO !')
787+
expect(rt(messages.value.en.pural, 2)).toEqual('2 apples')
788+
})
789+
790+
test('AST', () => {
791+
registerMessageCompiler(compile)
792+
793+
const { rt, messages } = createComposer({
794+
locale: 'en',
795+
messages: {
796+
en: {
797+
text: {
798+
type: 0,
799+
body: {
800+
type: 2,
801+
items: [
802+
{
803+
type: 3,
804+
value: 'hi dio!'
805+
}
806+
],
807+
static: 'hi dio!'
808+
}
809+
},
810+
list: {
811+
type: 0,
812+
body: {
813+
type: 2,
814+
items: [
815+
{
816+
type: 3,
817+
value: 'hi '
818+
},
819+
{
820+
type: 5,
821+
index: 0
822+
},
823+
{
824+
type: 3,
825+
value: '!'
826+
}
827+
]
828+
}
829+
},
830+
named: {
831+
type: 0,
832+
body: {
833+
type: 2,
834+
items: [
835+
{
836+
type: 3,
837+
value: 'hi '
838+
},
839+
{
840+
type: 4,
841+
key: 'name'
842+
},
843+
{
844+
type: 3,
845+
value: '!'
846+
}
847+
]
848+
}
849+
},
850+
name: {
851+
type: 0,
852+
body: {
853+
type: 2,
854+
items: [
855+
{
856+
type: 3,
857+
value: 'dio'
858+
}
859+
],
860+
static: 'dio'
861+
}
862+
},
863+
linked: {
864+
type: 0,
865+
body: {
866+
type: 2,
867+
items: [
868+
{
869+
type: 3,
870+
value: 'hi '
871+
},
872+
{
873+
type: 6,
874+
modifier: {
875+
type: 8,
876+
value: 'upper'
877+
},
878+
key: {
879+
type: 7,
880+
value: 'name'
881+
}
882+
},
883+
{
884+
type: 3,
885+
value: ' !'
886+
}
887+
]
888+
}
889+
},
890+
pural: {
891+
type: 0,
892+
body: {
893+
type: 1,
894+
cases: [
895+
{
896+
type: 2,
897+
items: [
898+
{
899+
type: 3,
900+
value: 'no apples'
901+
}
902+
],
903+
static: 'no apples'
904+
},
905+
{
906+
type: 2,
907+
items: [
908+
{
909+
type: 3,
910+
value: 'one apple'
911+
}
912+
],
913+
static: 'one apple'
914+
},
915+
{
916+
type: 2,
917+
items: [
918+
{
919+
type: 4,
920+
key: 'count'
921+
},
922+
{
923+
type: 3,
924+
value: ' apples'
925+
}
926+
]
927+
}
928+
]
929+
}
930+
}
778931
}
779932
}
780933
})

packages/vue-i18n-core/test/legacy.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ test('formatFallbackMessages', () => {
141141
test('postTranslation', () => {
142142
const i18n = createVueI18n()
143143
expect(i18n.postTranslation).toEqual(null)
144-
const postTranslation = (str: VueMessageType) =>
144+
const postTranslation = <VueMessageType>(str: VueMessageType) =>
145145
shared.isString(str) ? str.trim() : str
146146
i18n.postTranslation = postTranslation
147147
expect(i18n.postTranslation).toEqual(postTranslation)
@@ -268,7 +268,7 @@ describe('rt', () => {
268268
'no apples',
269269
'one apple',
270270
`${ctx.named('count')} apples`
271-
])
271+
]) as string
272272
}
273273
}
274274
})

0 commit comments

Comments
 (0)