discourse/lib/freedom_patches/translate_accelerator.rb

66 lines
1.7 KiB
Ruby
Raw Normal View History

# This patch performs 2 functions
#
# 1. It caches all translations which drastically improves
# translation performance in an LRU cache
#
# 2. It patches I18n so it only loads the translations it needs
# on demand
#
# This patch depends on the convention that locale yml files must be named [locale_name].yml
2013-04-24 00:40:09 -04:00
module I18n
2013-04-24 00:40:09 -04:00
# 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 = 300
2013-04-24 00:40:09 -04:00
def reload!
@loaded_locales = []
2013-04-24 00:40:09 -04:00
@cache = nil
reload_no_cache!
end
LOAD_MUTEX = Mutex.new
def load_locale(locale)
LOAD_MUTEX.synchronize do
2015-02-11 22:40:07 -05:00
return if @loaded_locales.include?(locale)
if @loaded_locales.empty?
# load all rb files
I18n.backend.load_translations(I18n.load_path.grep(/\.rb$/))
end
# load it
I18n.backend.load_translations(I18n.load_path.grep Regexp.new("\\.#{locale}\\.yml$"))
@loaded_locales << locale
end
end
def ensure_all_loaded!
backend.fallbacks(locale).each {|l| ensure_loaded!(l) }
end
def ensure_loaded!(locale)
@loaded_locales ||= []
load_locale(locale) unless @loaded_locales.include?(locale)
end
def translate(key, *args)
load_locale(config.locale) unless @loaded_locales.include?(config.locale)
return translate_no_cache(key, *args) if args.length > 0
2013-04-24 00:40:09 -04:00
@cache ||= LruRedux::ThreadSafeCache.new(LRU_CACHE_SIZE)
k = "#{key}#{config.locale}#{config.backend.object_id}"
@cache.getset(k) do
translate_no_cache(key).freeze
2013-04-24 00:40:09 -04:00
end
end
alias_method :t, :translate
end
end