2015-02-12 09:08:14 +08: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 12:40:09 +08:00
|
|
|
module I18n
|
2015-02-12 09:08:14 +08:00
|
|
|
|
2013-04-24 12:40:09 +08:00
|
|
|
# this accelerates translation a tiny bit (halves the time it takes)
|
|
|
|
class << self
|
|
|
|
alias_method :translate_no_cache, :translate
|
2016-04-01 05:33:25 +08:00
|
|
|
alias_method :exists_no_cache?, :exists?
|
2013-04-24 12:40:09 +08:00
|
|
|
alias_method :reload_no_cache!, :reload!
|
2015-03-30 13:31:36 +08:00
|
|
|
LRU_CACHE_SIZE = 300
|
2013-04-24 12:40:09 +08:00
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
def init_accelerator!
|
|
|
|
@overrides_enabled = true
|
|
|
|
reload!
|
|
|
|
end
|
|
|
|
|
2013-04-24 12:40:09 +08:00
|
|
|
def reload!
|
2015-02-12 09:08:14 +08:00
|
|
|
@loaded_locales = []
|
2013-04-24 12:40:09 +08:00
|
|
|
@cache = nil
|
2016-04-01 05:33:25 +08:00
|
|
|
@overrides_by_site = {}
|
|
|
|
|
2013-04-24 12:40:09 +08:00
|
|
|
reload_no_cache!
|
2016-04-01 05:33:25 +08:00
|
|
|
ensure_all_loaded!
|
2013-04-24 12:40:09 +08:00
|
|
|
end
|
|
|
|
|
2015-02-12 09:08:14 +08:00
|
|
|
LOAD_MUTEX = Mutex.new
|
|
|
|
def load_locale(locale)
|
|
|
|
LOAD_MUTEX.synchronize do
|
2015-02-12 11:40:07 +08:00
|
|
|
return if @loaded_locales.include?(locale)
|
2015-02-12 09:08:14 +08: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
|
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
def ensure_all_loaded!
|
|
|
|
backend.fallbacks(locale).each {|l| ensure_loaded!(l) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def search(query, opts=nil)
|
|
|
|
locale = opts[:locale] || config.locale
|
|
|
|
|
|
|
|
load_locale(locale) unless @loaded_locales.include?(locale)
|
|
|
|
opts ||= {}
|
|
|
|
|
|
|
|
target = opts[:backend] || backend
|
|
|
|
results = opts[:overridden] ? {} : target.search(config.locale, query)
|
|
|
|
|
|
|
|
regexp = /#{query}/i
|
|
|
|
(overrides_by_locale(locale) || {}).each do |k, v|
|
|
|
|
results.delete(k)
|
|
|
|
results[k] = v if (k =~ regexp || v =~ regexp)
|
|
|
|
end
|
|
|
|
results
|
|
|
|
end
|
|
|
|
|
2015-07-16 00:04:45 +08:00
|
|
|
def ensure_loaded!(locale)
|
|
|
|
@loaded_locales ||= []
|
2016-04-01 05:33:25 +08:00
|
|
|
load_locale(locale) unless @loaded_locales.include?(locale)
|
2015-07-16 00:04:45 +08:00
|
|
|
end
|
|
|
|
|
2016-04-01 05:33:25 +08: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(*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
|
|
|
|
|
2015-03-30 13:31:36 +08:00
|
|
|
|
2013-04-24 12:40:09 +08:00
|
|
|
@cache ||= LruRedux::ThreadSafeCache.new(LRU_CACHE_SIZE)
|
2016-04-01 05:33:25 +08:00
|
|
|
k = "#{key}#{locale}#{config.backend.object_id}"
|
2015-03-30 13:31:36 +08:00
|
|
|
|
|
|
|
@cache.getset(k) do
|
2016-04-01 05:33:25 +08:00
|
|
|
translate_no_cache(key, options).freeze
|
2013-04-24 12:40:09 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-01 05:33:25 +08:00
|
|
|
def overrides_by_locale(locale)
|
|
|
|
return unless @overrides_enabled
|
|
|
|
|
|
|
|
site = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
|
|
|
|
by_site = @overrides_by_site[site]
|
|
|
|
|
|
|
|
if by_site.nil? || !by_site.has_key?(locale)
|
|
|
|
by_site = @overrides_by_site[site] = {}
|
|
|
|
|
|
|
|
# Load overrides
|
|
|
|
translations_overrides = TranslationOverride.where(locale: locale).pluck(:translation_key, :value)
|
|
|
|
|
|
|
|
if translations_overrides.empty?
|
|
|
|
by_site[locale] = {}
|
|
|
|
else
|
|
|
|
translations_overrides.each do |tuple|
|
|
|
|
by_locale = by_site[locale] ||= {}
|
|
|
|
by_locale[tuple[0]] = tuple[1]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
by_site[locale]
|
|
|
|
end
|
|
|
|
|
|
|
|
def client_overrides_json(locale)
|
|
|
|
client_json = (overrides_by_locale(locale) || {}).select {|k, _| k.starts_with?('js.') || k.starts_with?('admin_js.')}
|
|
|
|
MultiJson.dump(client_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
if @overrides_enabled
|
|
|
|
by_locale = overrides_by_locale(locale)
|
|
|
|
if by_locale
|
|
|
|
if options.present?
|
|
|
|
options[:overrides] = by_locale
|
|
|
|
|
|
|
|
# I18n likes to use throw...
|
|
|
|
catch(:exception) do
|
|
|
|
return backend.translate(locale, key, options)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if result = by_locale[key]
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
translate_no_override(key, options)
|
|
|
|
end
|
|
|
|
|
2013-04-24 12:40:09 +08:00
|
|
|
alias_method :t, :translate
|
2016-04-01 05:33:25 +08:00
|
|
|
|
|
|
|
def exists?(key, locale=nil)
|
|
|
|
locale ||= config.locale
|
|
|
|
load_locale(locale) unless @loaded_locales.include?(locale)
|
|
|
|
exists_no_cache?(key, locale)
|
|
|
|
end
|
|
|
|
|
2013-04-24 12:40:09 +08:00
|
|
|
end
|
|
|
|
end
|