Skip to content

Commit efd89bf

Browse files
committed
improve caching of translate function
1 parent f8fc670 commit efd89bf

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

src/runtime/translate.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ import {
2626
} from '../utils'
2727

2828
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}`
2933

3034
/**
3135
* # translate
@@ -187,7 +191,7 @@ export function translate(
187191
const locale = isString(options.locale) ? options.locale : context.locale
188192
const locales = getLocaleChain(context, fallbackLocale, locale)
189193

190-
// resolve format
194+
// resolve message format
191195
let message: LocaleMessage = {}
192196
let targetLocale: Locale | undefined
193197
let format: PathValue = null
@@ -202,27 +206,35 @@ export function translate(
202206
}
203207
message = messages[targetLocale] || {}
204208
format = resolveValue(message, key)
205-
if (isString(format)) break
209+
if (isString(format) || isFunction(format)) break
206210
handleMissing(context, key, targetLocale, missingWarn, 'translate')
207211
}
208212

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+
}
212221
}
213222

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+
) {
216228
return unresolving ? NOT_REOSLVED : key
217229
}
218230

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)
221234
if (!msg) {
222-
msg = compile(format)
223-
_compileCache.set(format, msg)
235+
msg = isString(format) ? compile(format) : format
236+
_compileCache.set(cacheKey, msg)
224237
}
225-
// console.log('msg', msg.toString())
226238

227239
// evaluate message with context
228240
const ctxOptions = getMessageContextOptions(
@@ -278,19 +290,20 @@ function getMessageContextOptions(
278290
): MessageContextOptions {
279291
const { modifiers, pluralRules, _compileCache } = context
280292

281-
// TODO: need to design resolve message function?
282293
const resolveMessage = (key: string): MessageFunction => {
283-
const fn = _compileCache.get(key)
294+
const cacheKey = generateCacheKey(locale, key)
295+
const fn = _compileCache.get(cacheKey)
284296
if (fn) {
285297
return fn
286298
}
287299
const val = resolveValue(message, key)
288300
if (isString(val)) {
289301
const msg = compile(val)
290-
_compileCache.set(val, msg)
302+
_compileCache.set(cacheKey, msg)
291303
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
294307
} else {
295308
// TODO: should be implemented warning message
296309
return NOOP_MESSAGE_FUNCTION

0 commit comments

Comments
 (0)