2015-02-11 20:08:14 -05: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-24 00:40:09 -04:00
|
|
|
module I18n
|
2015-02-11 20:08:14 -05:00
|
|
|
|
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
|
2015-11-23 16:45:05 -05:00
|
|
|
alias_method :exists_no_cache?, :exists?
|
2013-04-24 00:40:09 -04:00
|
|
|
alias_method :reload_no_cache!, :reload!
|
2015-03-30 01:31:36 -04:00
|
|
|
LRU_CACHE_SIZE = 300
|
2013-04-24 00:40:09 -04:00
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
def init_accelerator!
|
|
|
|
@overrides_enabled = true
|
|
|
|
reload!
|
|
|
|
end
|
|
|
|
|
2013-04-24 00:40:09 -04:00
|
|
|
def reload!
|
2015-02-11 20:08:14 -05:00
|
|
|
@loaded_locales = []
|
2013-04-24 00:40:09 -04:00
|
|
|
@cache = nil
|
2015-11-19 16:36:59 -05:00
|
|
|
@overrides_by_site = {}
|
|
|
|
|
2013-04-24 00:40:09 -04:00
|
|
|
reload_no_cache!
|
2016-02-13 17:21:17 -05:00
|
|
|
ensure_all_loaded!
|
2013-04-24 00:40:09 -04:00
|
|
|
end
|
|
|
|
|
2015-02-11 20:08:14 -05:00
|
|
|
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)
|
2015-02-11 20:08:14 -05:00
|
|
|
|
|
|
|
if @loaded_locales.empty?
|
|
|
|
# load all rb files
|
|
|
|
I18n.backend.load_translations(I18n.load_path.grep(/\.rb$/))
|
|
|
|
end
|
|
|
|
|
|
|
|
# load it
|
2017-02-24 05:31:21 -05:00
|
|
|
I18n.backend.load_translations(I18n.load_path.grep(/\.#{Regexp.escape locale}\.yml$/))
|
2015-02-11 20:08:14 -05:00
|
|
|
|
|
|
|
@loaded_locales << locale
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-13 15:42:01 -05:00
|
|
|
def ensure_all_loaded!
|
2017-07-27 21:20:09 -04:00
|
|
|
backend.fallbacks(locale).each { |l| ensure_loaded!(l) }
|
2015-11-13 15:42:01 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def search(query, opts = nil)
|
2015-12-23 12:09:18 -05:00
|
|
|
locale = opts[:locale] || config.locale
|
|
|
|
|
|
|
|
load_locale(locale) unless @loaded_locales.include?(locale)
|
2015-11-23 16:45:05 -05:00
|
|
|
opts ||= {}
|
|
|
|
|
|
|
|
target = opts[:backend] || backend
|
2015-11-30 15:22:58 -05:00
|
|
|
results = opts[:overridden] ? {} : target.search(config.locale, query)
|
2015-11-23 16:45:05 -05:00
|
|
|
|
|
|
|
regexp = /#{query}/i
|
2015-12-23 12:09:18 -05:00
|
|
|
(overrides_by_locale(locale) || {}).each do |k, v|
|
2015-11-23 16:45:05 -05:00
|
|
|
results.delete(k)
|
|
|
|
results[k] = v if (k =~ regexp || v =~ regexp)
|
|
|
|
end
|
|
|
|
results
|
|
|
|
end
|
|
|
|
|
2015-07-15 12:04:45 -04:00
|
|
|
def ensure_loaded!(locale)
|
|
|
|
@loaded_locales ||= []
|
2015-11-13 15:42:01 -05:00
|
|
|
load_locale(locale) unless @loaded_locales.include?(locale)
|
2015-07-15 12:04:45 -04:00
|
|
|
end
|
|
|
|
|
2015-11-19 16:36:59 -05: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
|
|
|
|
|
2015-12-23 12:09:18 -05:00
|
|
|
def translate_no_override(*args)
|
|
|
|
return translate_no_cache(*args) if args.length > 1 && args[1].present?
|
|
|
|
|
|
|
|
options = args.last.is_a?(Hash) ? args.pop.dup : {}
|
|
|
|
key = args.shift
|
|
|
|
locale = options[:locale] || config.locale
|
|
|
|
|
2013-04-24 00:40:09 -04:00
|
|
|
@cache ||= LruRedux::ThreadSafeCache.new(LRU_CACHE_SIZE)
|
2015-12-23 12:09:18 -05:00
|
|
|
k = "#{key}#{locale}#{config.backend.object_id}"
|
2015-03-30 01:31:36 -04:00
|
|
|
|
|
|
|
@cache.getset(k) do
|
2015-12-23 12:09:18 -05:00
|
|
|
translate_no_cache(key, options).freeze
|
2013-04-24 00:40:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-23 12:09:18 -05:00
|
|
|
def overrides_by_locale(locale)
|
2015-11-20 17:13:37 -05:00
|
|
|
return unless @overrides_enabled
|
2015-11-19 16:36:59 -05:00
|
|
|
|
2015-11-20 17:13:37 -05:00
|
|
|
site = RailsMultisite::ConnectionManagement.current_db
|
2015-11-19 16:36:59 -05:00
|
|
|
|
2015-11-20 17:13:37 -05:00
|
|
|
by_site = @overrides_by_site[site]
|
2017-07-06 23:28:00 -04:00
|
|
|
by_site ||= {}
|
2015-11-19 16:36:59 -05:00
|
|
|
|
2017-07-06 23:28:00 -04:00
|
|
|
if !by_site.has_key?(locale)
|
2015-11-20 17:13:37 -05:00
|
|
|
# Load overrides
|
2016-04-08 14:49:50 -04:00
|
|
|
translations_overrides = TranslationOverride.where(locale: locale).pluck(:translation_key, :value, :compiled_js)
|
2016-02-23 03:00:39 -05:00
|
|
|
|
|
|
|
if translations_overrides.empty?
|
|
|
|
by_site[locale] = {}
|
|
|
|
else
|
|
|
|
translations_overrides.each do |tuple|
|
|
|
|
by_locale = by_site[locale] ||= {}
|
2016-04-08 14:49:50 -04:00
|
|
|
by_locale[tuple[0]] = tuple[2] || tuple[1]
|
2016-02-23 03:00:39 -05:00
|
|
|
end
|
2015-11-19 16:36:59 -05:00
|
|
|
end
|
2017-07-06 23:28:00 -04:00
|
|
|
|
|
|
|
@overrides_by_site[site] = by_site
|
2015-11-20 17:13:37 -05:00
|
|
|
end
|
|
|
|
|
2016-08-24 05:53:03 -04:00
|
|
|
by_site[locale].with_indifferent_access
|
2015-11-20 17:13:37 -05:00
|
|
|
end
|
2015-11-19 16:36:59 -05:00
|
|
|
|
2015-12-23 12:09:18 -05:00
|
|
|
def client_overrides_json(locale)
|
2017-02-24 05:31:21 -05:00
|
|
|
client_json = (overrides_by_locale(locale) || {}).select { |k, _| k[/^(admin_js|js)\./] }
|
2015-11-20 17:13:37 -05:00
|
|
|
MultiJson.dump(client_json)
|
|
|
|
end
|
|
|
|
|
2015-12-23 12:09:18 -05:00
|
|
|
def translate(*args)
|
|
|
|
options = args.last.is_a?(Hash) ? args.pop.dup : {}
|
|
|
|
key = args.shift
|
|
|
|
locale = options[:locale] || config.locale
|
|
|
|
|
|
|
|
load_locale(locale) unless @loaded_locales.include?(locale)
|
2015-11-20 17:13:37 -05:00
|
|
|
|
|
|
|
if @overrides_enabled
|
2017-07-03 01:52:27 -04:00
|
|
|
overrides = {}
|
|
|
|
|
|
|
|
backend.fallbacks(locale).each do |l|
|
|
|
|
overrides[l] = overrides_by_locale(l)
|
|
|
|
end
|
|
|
|
|
|
|
|
if overrides.present?
|
2015-12-23 12:09:18 -05:00
|
|
|
if options.present?
|
2017-07-03 01:52:27 -04:00
|
|
|
options[:overrides] = overrides
|
2015-12-23 12:09:18 -05:00
|
|
|
|
2015-12-09 01:10:51 -05:00
|
|
|
# I18n likes to use throw...
|
|
|
|
catch(:exception) do
|
2015-12-23 12:09:18 -05:00
|
|
|
return backend.translate(locale, key, options)
|
2015-12-09 01:10:51 -05:00
|
|
|
end
|
|
|
|
else
|
2017-07-03 01:52:27 -04:00
|
|
|
overrides.each do |_k, v|
|
|
|
|
if result = v[key]
|
|
|
|
return result
|
|
|
|
end
|
2015-12-09 01:10:51 -05:00
|
|
|
end
|
2015-11-19 16:36:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-24 05:53:03 -04:00
|
|
|
|
2015-12-23 12:09:18 -05:00
|
|
|
translate_no_override(key, options)
|
2015-11-19 16:36:59 -05:00
|
|
|
end
|
|
|
|
|
2013-04-24 00:40:09 -04:00
|
|
|
alias_method :t, :translate
|
2015-11-23 16:45:05 -05:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def exists?(key, locale = nil)
|
2015-12-23 12:09:18 -05:00
|
|
|
locale ||= config.locale
|
|
|
|
load_locale(locale) unless @loaded_locales.include?(locale)
|
|
|
|
exists_no_cache?(key, locale)
|
2015-11-23 16:45:05 -05:00
|
|
|
end
|
|
|
|
|
2013-04-24 00:40:09 -04:00
|
|
|
end
|
|
|
|
end
|