@@ -26,6 +26,10 @@ import {
26
26
} from '../utils'
27
27
28
28
const NOOP_MESSAGE_FUNCTION = ( ) => ''
29
+ const isMessageFunction = ( val : unknown ) : val is MessageFunction =>
30
+ isFunction ( val )
31
+ const generateCacheKey = ( locale : Locale , key : string ) : string =>
32
+ `${ locale } __${ key } `
29
33
30
34
/**
31
35
* # translate
@@ -187,7 +191,7 @@ export function translate(
187
191
const locale = isString ( options . locale ) ? options . locale : context . locale
188
192
const locales = getLocaleChain ( context , fallbackLocale , locale )
189
193
190
- // resolve format
194
+ // resolve message format
191
195
let message : LocaleMessage = { }
192
196
let targetLocale : Locale | undefined
193
197
let format : PathValue = null
@@ -202,27 +206,35 @@ export function translate(
202
206
}
203
207
message = messages [ targetLocale ] || { }
204
208
format = resolveValue ( message , key )
205
- if ( isString ( format ) ) break
209
+ if ( isString ( format ) || isFunction ( format ) ) break
206
210
handleMissing ( context , key , targetLocale , missingWarn , 'translate' )
207
211
}
208
212
209
- // if you use default message, set it as format!
210
- if ( ! isString ( format ) && enableDefaultMsg ) {
211
- format = defaultMsgOrKey
213
+ let cacheBaseKey = key
214
+
215
+ // if you use default message, set it as message format!
216
+ if ( ! ( isString ( format ) || isMessageFunction ( format ) ) ) {
217
+ if ( enableDefaultMsg ) {
218
+ format = defaultMsgOrKey
219
+ cacheBaseKey = format
220
+ }
212
221
}
213
222
214
- // checking format and target locale
215
- if ( ! isString ( format ) || ! isString ( targetLocale ) ) {
223
+ // checking message format and target locale
224
+ if (
225
+ ! ( isString ( format ) || isMessageFunction ( format ) ) ||
226
+ ! isString ( targetLocale )
227
+ ) {
216
228
return unresolving ? NOT_REOSLVED : key
217
229
}
218
230
219
- // compile format
220
- let msg = _compileCache . get ( format )
231
+ // compile message format
232
+ const cacheKey = generateCacheKey ( targetLocale , cacheBaseKey )
233
+ let msg = _compileCache . get ( cacheKey )
221
234
if ( ! msg ) {
222
- msg = compile ( format )
223
- _compileCache . set ( format , msg )
235
+ msg = isString ( format ) ? compile ( format ) : format
236
+ _compileCache . set ( cacheKey , msg )
224
237
}
225
- // console.log('msg', msg.toString())
226
238
227
239
// evaluate message with context
228
240
const ctxOptions = getMessageContextOptions (
@@ -278,19 +290,20 @@ function getMessageContextOptions(
278
290
) : MessageContextOptions {
279
291
const { modifiers, pluralRules, _compileCache } = context
280
292
281
- // TODO: need to design resolve message function?
282
293
const resolveMessage = ( key : string ) : MessageFunction => {
283
- const fn = _compileCache . get ( key )
294
+ const cacheKey = generateCacheKey ( locale , key )
295
+ const fn = _compileCache . get ( cacheKey )
284
296
if ( fn ) {
285
297
return fn
286
298
}
287
299
const val = resolveValue ( message , key )
288
300
if ( isString ( val ) ) {
289
301
const msg = compile ( val )
290
- _compileCache . set ( val , msg )
302
+ _compileCache . set ( cacheKey , msg )
291
303
return msg
292
- } else if ( isFunction ( val ) ) {
293
- return val as MessageFunction
304
+ } else if ( isMessageFunction ( val ) ) {
305
+ _compileCache . set ( cacheKey , val )
306
+ return val
294
307
} else {
295
308
// TODO: should be implemented warning message
296
309
return NOOP_MESSAGE_FUNCTION
0 commit comments