@@ -91,6 +91,92 @@ describe('test/index.test.ts', () => {
9191 await close ( app ) ;
9292 } ) ;
9393
94+ it ( 'should test missingKeyHandler with default behavior' , async ( ) => {
95+ const app = await createLightApp ( join ( __dirname , './fixtures/base-app' ) , {
96+ globalConfig : {
97+ i18n : {
98+ defaultLocale : 'en_US' ,
99+ localeTable : {
100+ en_US : {
101+ 'hello' : 'hello world'
102+ }
103+ }
104+ }
105+ }
106+ } ) ;
107+
108+ const i18nService = await app . getApplicationContext ( ) . getAsync ( MidwayI18nService ) ;
109+
110+ // Test existing key
111+ expect ( i18nService . translate ( 'hello' ) ) . toEqual ( 'hello world' ) ;
112+
113+ // Test missing key with default missingKeyHandler (should return the key itself)
114+ expect ( i18nService . translate ( 'nonexistent_key' ) ) . toEqual ( 'nonexistent_key' ) ;
115+
116+ await close ( app ) ;
117+ } ) ;
118+
119+ it ( 'should test custom missingKeyHandler' , async ( ) => {
120+ const app = await createLightApp ( join ( __dirname , './fixtures/base-app' ) , {
121+ globalConfig : {
122+ i18n : {
123+ defaultLocale : 'en_US' ,
124+ localeTable : {
125+ en_US : {
126+ 'hello' : 'hello world'
127+ }
128+ } ,
129+ missingKeyHandler : ( message , options ) => {
130+ return `[Missing: ${ message } ${ options ?. locale ? ` (${ options . locale } )` : '' } ]` ;
131+ }
132+ }
133+ }
134+ } ) ;
135+
136+ const i18nService = await app . getApplicationContext ( ) . getAsync ( MidwayI18nService ) ;
137+
138+ // Test existing key
139+ expect ( i18nService . translate ( 'hello' ) ) . toEqual ( 'hello world' ) ;
140+
141+ // Test missing key with custom missingKeyHandler
142+ expect ( i18nService . translate ( 'nonexistent_key' ) ) . toEqual ( '[Missing: nonexistent_key]' ) ;
143+ expect ( i18nService . translate ( 'nonexistent_key' , { locale : 'zh_CN' } ) ) . toEqual ( '[Missing: nonexistent_key (zh_CN)]' ) ;
144+
145+ await close ( app ) ;
146+ } ) ;
147+
148+ it ( 'should test missingKeyHandler with groups' , async ( ) => {
149+ const app = await createLightApp ( join ( __dirname , './fixtures/base-app' ) , {
150+ globalConfig : {
151+ i18n : {
152+ defaultLocale : 'en_US' ,
153+ localeTable : {
154+ en_US : {
155+ 'user' : {
156+ 'hello' : 'Hello user'
157+ }
158+ }
159+ } ,
160+ missingKeyHandler : ( message , options ) => {
161+ const group = options ?. group || 'default' ;
162+ return `[${ group } .${ message } ]` ;
163+ }
164+ }
165+ }
166+ } ) ;
167+
168+ const i18nService = await app . getApplicationContext ( ) . getAsync ( MidwayI18nService ) ;
169+
170+ // Test existing key in group
171+ expect ( i18nService . translate ( 'hello' , { group : 'user' } ) ) . toEqual ( 'Hello user' ) ;
172+
173+ // Test missing key in group
174+ expect ( i18nService . translate ( 'goodbye' , { group : 'user' } ) ) . toEqual ( '[user.goodbye]' ) ;
175+ expect ( i18nService . translate ( 'goodbye' ) ) . toEqual ( '[default.goodbye]' ) ;
176+
177+ await close ( app ) ;
178+ } ) ;
179+
94180 it ( 'should test fallbacks' , async ( ) => {
95181 const app = await createLightApp ( join ( __dirname , './fixtures/base-app' ) , {
96182 globalConfig : {
0 commit comments