discourse/lib/freedom_patches/translate_accelerator.rb

28 lines
636 B
Ruby
Raw Normal View History

2013-04-24 00:40:09 -04:00
module I18n
# this accelerates translation a tiny bit (halves the time it takes)
class << self
alias_method :translate_no_cache, :translate
alias_method :reload_no_cache!, :reload!
LRU_CACHE_SIZE = 2000
def reload!
@cache = nil
reload_no_cache!
end
def translate(*args)
@cache ||= LruRedux::ThreadSafeCache.new(LRU_CACHE_SIZE)
found = true
k = [args,config.locale,config.backend.object_id]
t = @cache.fetch(k){found=false}
unless found
t = @cache[k] = translate_no_cache(*args).freeze
2013-04-24 00:40:09 -04:00
end
t
2013-04-24 00:40:09 -04:00
end
alias_method :t, :translate
end
end