Make subsequent i18n.t calls faster
closes: CORE-2773 Testing locally, rendering the /accounts/1 spent 171ms in scoped i18n.t, With this it spent 11ms Change-Id: I67c84716e398bcb21f1b8e988a757e57936d0045 Reviewed-on: https://gerrit.instructure.com/189179 Tested-by: Jenkins QA-Review: Ryan Shaw <ryan@instructure.com> Product-Review: Ryan Shaw <ryan@instructure.com> Reviewed-by: Brent Burgoyne <bburgoyne@instructure.com>
This commit is contained in:
parent
c4f9ca9253
commit
ebe9e20a58
|
@ -317,16 +317,39 @@ I18n.scoped = (scope, callback) => {
|
|||
class Scope {
|
||||
constructor(scope) {
|
||||
this.scope = scope
|
||||
this.cache = new Map()
|
||||
}
|
||||
|
||||
translate() {
|
||||
const args = [].slice.call(arguments)
|
||||
let options = args[args.length - 1]
|
||||
if (!(options instanceof Object)) {
|
||||
options = {}
|
||||
args.push(options)
|
||||
translate(...args) {
|
||||
let cacheKey
|
||||
try {
|
||||
cacheKey = I18n.locale + JSON.stringify(args)
|
||||
} catch (e) {
|
||||
// if there is something in the arguments we can't stringify, just do it without cache
|
||||
}
|
||||
if (cacheKey) {
|
||||
const cached = this.cache.get(cacheKey)
|
||||
if (cached) {
|
||||
return cached
|
||||
} else {
|
||||
const valToCache = this.translateWithoutCache(...args)
|
||||
this.cache.set(cacheKey, valToCache)
|
||||
return valToCache
|
||||
}
|
||||
}
|
||||
else {
|
||||
return this.translateWithoutCache(...args)
|
||||
}
|
||||
}
|
||||
|
||||
translateWithoutCache() {
|
||||
let args = arguments
|
||||
const options = args[args.length - 1]
|
||||
if (options instanceof Object) {
|
||||
options.scope = this.scope
|
||||
} else {
|
||||
args = [...args, {scope: this.scope}]
|
||||
}
|
||||
options.scope = this.scope
|
||||
return I18n.translate(...args)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue