2015-02-11 19:08:14 -06:00
|
|
|
# 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-23 23:40:09 -05:00
|
|
|
module I18n
|
2015-02-11 19:08:14 -06:00
|
|
|
|
2013-04-23 23:40:09 -05: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!
|
2015-03-30 00:31:36 -05:00
|
|
|
LRU_CACHE_SIZE = 300
|
2013-04-23 23:40:09 -05:00
|
|
|
|
|
|
|
def reload!
|
2015-02-11 19:08:14 -06:00
|
|
|
@loaded_locales = []
|
2013-04-23 23:40:09 -05:00
|
|
|
@cache = nil
|
2015-11-19 15:36:59 -06:00
|
|
|
|
|
|
|
@overrides_enabled = true
|
|
|
|
@overrides_by_site = {}
|
|
|
|
|
2013-04-23 23:40:09 -05:00
|
|
|
reload_no_cache!
|
|
|
|
end
|
|
|
|
|
2015-02-11 19:08:14 -06:00
|
|
|
LOAD_MUTEX = Mutex.new
|
|
|
|
def load_locale(locale)
|
|
|
|
LOAD_MUTEX.synchronize do
|
2015-02-11 21:40:07 -06:00
|
|
|
return if @loaded_locales.include?(locale)
|
2015-02-11 19:08:14 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-11-13 14:42:01 -06:00
|
|
|
def ensure_all_loaded!
|
|
|
|
backend.fallbacks(locale).each {|l| ensure_loaded!(l) }
|
|
|
|
end
|
|
|
|
|
2015-07-15 11:04:45 -05:00
|
|
|
def ensure_loaded!(locale)
|
|
|
|
@loaded_locales ||= []
|
2015-11-13 14:42:01 -06:00
|
|
|
load_locale(locale) unless @loaded_locales.include?(locale)
|
2015-07-15 11:04:45 -05:00
|
|
|
end
|
|
|
|
|
2015-11-19 15:36:59 -06:00
|
|
|
# In some environments such as migrations we don't want to use overrides.
|
|
|
|
# Use this to disable them over a block of ruby code
|
|
|
|
def overrides_disabled
|
|
|
|
@overrides_enabled = false
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
@overrides_enabled = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def translate_no_override(key, *args)
|
2015-03-30 00:31:36 -05:00
|
|
|
return translate_no_cache(key, *args) if args.length > 0
|
|
|
|
|
2013-04-23 23:40:09 -05:00
|
|
|
@cache ||= LruRedux::ThreadSafeCache.new(LRU_CACHE_SIZE)
|
2015-11-19 15:36:59 -06:00
|
|
|
k = "#{key}#{config.locale}#{config.backend.object_id}"
|
2015-03-30 00:31:36 -05:00
|
|
|
|
|
|
|
@cache.getset(k) do
|
|
|
|
translate_no_cache(key).freeze
|
2013-04-23 23:40:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-19 15:36:59 -06:00
|
|
|
def translate(key, *args)
|
|
|
|
load_locale(config.locale) unless @loaded_locales.include?(config.locale)
|
|
|
|
|
|
|
|
if @overrides_enabled
|
|
|
|
site = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
|
|
|
|
by_site = @overrides_by_site[site]
|
|
|
|
|
|
|
|
by_locale = nil
|
|
|
|
unless by_site
|
|
|
|
by_site = @overrides_by_site[site] = {}
|
|
|
|
|
|
|
|
# Load overrides
|
|
|
|
TranslationOverride.where(locale: locale).pluck(:translation_key, :value).each do |tuple|
|
|
|
|
by_locale = by_site[locale] ||= {}
|
|
|
|
by_locale[tuple[0]] = tuple[1]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
by_locale = by_site[config.locale]
|
|
|
|
if by_locale
|
|
|
|
if args.size > 0 && args[0].is_a?(Hash)
|
|
|
|
args[0][:overrides] = by_locale
|
|
|
|
return backend.translate(config.locale, key, args[0])
|
|
|
|
end
|
|
|
|
|
|
|
|
if result = by_locale[key]
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
translate_no_override(key, *args)
|
|
|
|
end
|
|
|
|
|
2013-04-23 23:40:09 -05:00
|
|
|
alias_method :t, :translate
|
|
|
|
end
|
|
|
|
end
|