mirror of
https://github.com/discourse/discourse.git
synced 2025-09-08 12:06:51 +08:00
PERF: only load locale files we are using
We used to load up all translations in all languages
This commit is contained in:
parent
dde24c4ddb
commit
a5f46c98c1
2 changed files with 50 additions and 1 deletions
|
@ -20,5 +20,4 @@ class LocaleSiteSetting < EnumSiteSetting
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private_class_method :supported_locales
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,35 @@
|
||||||
|
# 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
|
||||||
|
|
||||||
module I18n
|
module I18n
|
||||||
|
module Backend
|
||||||
|
|
||||||
|
class Simple
|
||||||
|
def available_locales
|
||||||
|
# in case you are wondering this is:
|
||||||
|
# Dir.glob( File.join(Rails.root, 'config', 'locales', 'client.*.yml') )
|
||||||
|
# .map {|x| x.split('.')[-2]}.sort
|
||||||
|
LocaleSiteSetting.supported_locales.map(&:to_sym)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module Base
|
||||||
|
# force explicit loading
|
||||||
|
def load_translations(*filenames)
|
||||||
|
unless filenames.empty?
|
||||||
|
filenames.flatten.each { |filename| load_file(filename) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
# this accelerates translation a tiny bit (halves the time it takes)
|
# this accelerates translation a tiny bit (halves the time it takes)
|
||||||
class << self
|
class << self
|
||||||
alias_method :translate_no_cache, :translate
|
alias_method :translate_no_cache, :translate
|
||||||
|
@ -6,16 +37,35 @@ module I18n
|
||||||
LRU_CACHE_SIZE = 2000
|
LRU_CACHE_SIZE = 2000
|
||||||
|
|
||||||
def reload!
|
def reload!
|
||||||
|
@loaded_locales = []
|
||||||
@cache = nil
|
@cache = nil
|
||||||
reload_no_cache!
|
reload_no_cache!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
LOAD_MUTEX = Mutex.new
|
||||||
|
def load_locale(locale)
|
||||||
|
LOAD_MUTEX.synchronize do
|
||||||
|
return if @loaded_locales.include?(config.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 translate(*args)
|
def translate(*args)
|
||||||
@cache ||= LruRedux::ThreadSafeCache.new(LRU_CACHE_SIZE)
|
@cache ||= LruRedux::ThreadSafeCache.new(LRU_CACHE_SIZE)
|
||||||
found = true
|
found = true
|
||||||
k = [args, config.locale, config.backend.object_id]
|
k = [args, config.locale, config.backend.object_id]
|
||||||
t = @cache.fetch(k) { found = false }
|
t = @cache.fetch(k) { found = false }
|
||||||
unless found
|
unless found
|
||||||
|
load_locale(config.locale) unless @loaded_locales.include?(config.locale)
|
||||||
begin
|
begin
|
||||||
t = translate_no_cache(*args)
|
t = translate_no_cache(*args)
|
||||||
rescue MissingInterpolationArgument
|
rescue MissingInterpolationArgument
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue