2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
2015-11-24 05:45:05 +08:00
|
|
|
alias_method :exists_no_cache?, :exists?
|
2013-04-24 12:40:09 +08:00
|
|
|
alias_method :reload_no_cache!, :reload!
|
2018-09-18 18:59:00 +08:00
|
|
|
alias_method :locale_no_cache=, :locale=
|
|
|
|
|
2019-06-05 12:30:25 +08:00
|
|
|
LRU_CACHE_SIZE = 400
|
2013-04-24 12:40:09 +08:00
|
|
|
|
2015-11-24 05:45:05 +08:00
|
|
|
def init_accelerator!
|
|
|
|
@overrides_enabled = true
|
2018-09-18 18:59:00 +08:00
|
|
|
execute_reload
|
2015-11-24 05:45:05 +08:00
|
|
|
end
|
|
|
|
|
2013-04-24 12:40:09 +08:00
|
|
|
def reload!
|
2018-09-18 18:59:00 +08:00
|
|
|
@requires_reload = true
|
2013-04-24 12:40:09 +08:00
|
|
|
end
|
|
|
|
|
2015-02-12 09:08:14 +08:00
|
|
|
LOAD_MUTEX = Mutex.new
|
2018-09-18 18:59:00 +08:00
|
|
|
|
2015-02-12 09:08:14 +08:00
|
|
|
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$/))
|
2018-01-25 19:09:18 +08:00
|
|
|
|
|
|
|
# load plural rules from plugins
|
2018-09-04 10:16:21 +08:00
|
|
|
DiscoursePluginRegistry.locales.each do |plugin_locale, options|
|
2018-01-25 19:09:18 +08:00
|
|
|
if options[:plural]
|
2019-06-05 12:30:25 +08:00
|
|
|
I18n.backend.store_translations(
|
|
|
|
plugin_locale,
|
2018-09-04 10:16:21 +08:00
|
|
|
i18n: { plural: options[:plural] }
|
|
|
|
)
|
2018-01-25 19:09:18 +08:00
|
|
|
end
|
|
|
|
end
|
2015-02-12 09:08:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# load it
|
2017-02-24 18:31:21 +08:00
|
|
|
I18n.backend.load_translations(I18n.load_path.grep(/\.#{Regexp.escape locale}\.yml$/))
|
2015-02-12 09:08:14 +08:00
|
|
|
|
|
|
|
@loaded_locales << locale
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-14 04:42:01 +08:00
|
|
|
def ensure_all_loaded!
|
2019-05-11 07:43:48 +08:00
|
|
|
I18n.fallbacks[locale].each { |l| ensure_loaded!(l) }
|
2015-11-14 04:42:01 +08:00
|
|
|
end
|
|
|
|
|
2018-09-18 18:59:00 +08:00
|
|
|
def search(query, opts = {})
|
|
|
|
execute_reload if @requires_reload
|
|
|
|
|
2015-12-24 01:09:18 +08:00
|
|
|
locale = opts[:locale] || config.locale
|
|
|
|
|
|
|
|
load_locale(locale) unless @loaded_locales.include?(locale)
|
2015-11-24 05:45:05 +08:00
|
|
|
opts ||= {}
|
|
|
|
|
|
|
|
target = opts[:backend] || backend
|
2015-12-01 04:22:58 +08:00
|
|
|
results = opts[:overridden] ? {} : target.search(config.locale, query)
|
2015-11-24 05:45:05 +08:00
|
|
|
|
2018-11-10 08:17:07 +08:00
|
|
|
regexp = I18n::Backend::DiscourseI18n.create_search_regexp(query)
|
2015-12-24 01:09:18 +08:00
|
|
|
(overrides_by_locale(locale) || {}).each do |k, v|
|
2015-11-24 05:45:05 +08:00
|
|
|
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 ||= []
|
2015-11-14 04:42:01 +08:00
|
|
|
load_locale(locale) unless @loaded_locales.include?(locale)
|
2015-07-16 00:04:45 +08:00
|
|
|
end
|
|
|
|
|
2015-11-20 05:36:59 +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
|
|
|
|
|
2019-06-05 12:30:25 +08:00
|
|
|
class MissingTranslation; end
|
2015-12-24 01:09:18 +08:00
|
|
|
|
2019-06-05 12:30:25 +08:00
|
|
|
def translate_no_override(key, options)
|
|
|
|
# note we skip cache for :format and :count
|
|
|
|
should_raise = false
|
|
|
|
locale = nil
|
|
|
|
|
|
|
|
dup_options = nil
|
|
|
|
if options
|
|
|
|
dup_options = options.dup
|
|
|
|
should_raise = dup_options.delete(:raise)
|
|
|
|
locale = dup_options.delete(:locale)
|
|
|
|
end
|
|
|
|
|
|
|
|
if dup_options.present?
|
2019-11-08 14:30:42 +08:00
|
|
|
return translate_no_cache(key, **options)
|
2019-06-05 12:30:25 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
locale ||= config.locale
|
2015-12-24 01:09:18 +08:00
|
|
|
|
2013-04-24 12:40:09 +08:00
|
|
|
@cache ||= LruRedux::ThreadSafeCache.new(LRU_CACHE_SIZE)
|
2015-12-24 01:09:18 +08:00
|
|
|
k = "#{key}#{locale}#{config.backend.object_id}"
|
2015-03-30 13:31:36 +08:00
|
|
|
|
2019-06-05 12:30:25 +08:00
|
|
|
val = @cache.getset(k) do
|
|
|
|
begin
|
2019-06-05 20:19:46 +08:00
|
|
|
translate_no_cache(key, locale: locale, raise: true).freeze
|
2019-06-05 12:30:25 +08:00
|
|
|
rescue I18n::MissingTranslationData
|
|
|
|
MissingTranslation
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if val != MissingTranslation
|
|
|
|
val
|
|
|
|
elsif should_raise
|
|
|
|
raise I18n::MissingTranslationData.new(locale, key)
|
|
|
|
else
|
|
|
|
-"translation missing: #{locale}.#{key}"
|
2013-04-24 12:40:09 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-24 01:09:18 +08:00
|
|
|
def overrides_by_locale(locale)
|
2015-11-21 06:13:37 +08:00
|
|
|
return unless @overrides_enabled
|
2019-06-13 10:58:27 +08:00
|
|
|
return {} if GlobalSetting.skip_db?
|
|
|
|
|
2019-11-05 09:15:44 +08:00
|
|
|
execute_reload if @requires_reload
|
|
|
|
|
2015-11-21 06:13:37 +08:00
|
|
|
site = RailsMultisite::ConnectionManagement.current_db
|
2015-11-20 05:36:59 +08:00
|
|
|
|
2015-11-21 06:13:37 +08:00
|
|
|
by_site = @overrides_by_site[site]
|
2017-07-07 11:28:00 +08:00
|
|
|
by_site ||= {}
|
2015-11-20 05:36:59 +08:00
|
|
|
|
2017-07-07 11:28:00 +08:00
|
|
|
if !by_site.has_key?(locale)
|
2015-11-21 06:13:37 +08:00
|
|
|
# Load overrides
|
2016-04-09 02:49:50 +08:00
|
|
|
translations_overrides = TranslationOverride.where(locale: locale).pluck(:translation_key, :value, :compiled_js)
|
2016-02-23 16:00:39 +08:00
|
|
|
|
|
|
|
if translations_overrides.empty?
|
|
|
|
by_site[locale] = {}
|
|
|
|
else
|
|
|
|
translations_overrides.each do |tuple|
|
|
|
|
by_locale = by_site[locale] ||= {}
|
2016-04-09 02:49:50 +08:00
|
|
|
by_locale[tuple[0]] = tuple[2] || tuple[1]
|
2016-02-23 16:00:39 +08:00
|
|
|
end
|
2015-11-20 05:36:59 +08:00
|
|
|
end
|
2017-07-07 11:28:00 +08:00
|
|
|
|
|
|
|
@overrides_by_site[site] = by_site
|
2015-11-21 06:13:37 +08:00
|
|
|
end
|
|
|
|
|
2016-08-24 17:53:03 +08:00
|
|
|
by_site[locale].with_indifferent_access
|
2019-09-17 11:38:01 +08:00
|
|
|
rescue ActiveRecord::StatementInvalid => e
|
|
|
|
if PG::UndefinedTable === e.cause
|
|
|
|
{}
|
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
2015-11-21 06:13:37 +08:00
|
|
|
end
|
2015-11-20 05:36:59 +08:00
|
|
|
|
2015-12-24 01:09:18 +08:00
|
|
|
def translate(*args)
|
2018-09-18 18:59:00 +08:00
|
|
|
execute_reload if @requires_reload
|
|
|
|
|
2015-12-24 01:09:18 +08:00
|
|
|
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-21 06:13:37 +08:00
|
|
|
|
|
|
|
if @overrides_enabled
|
2017-07-03 13:52:27 +08:00
|
|
|
overrides = {}
|
|
|
|
|
2019-06-05 12:30:25 +08:00
|
|
|
# for now lets do all the expensive work for keys with count
|
|
|
|
# no choice really
|
|
|
|
has_override = !!options[:count]
|
|
|
|
|
2019-05-11 07:43:48 +08:00
|
|
|
I18n.fallbacks[locale].each do |l|
|
2019-06-05 12:30:25 +08:00
|
|
|
override = overrides[l] = overrides_by_locale(l)
|
|
|
|
has_override ||= override.key?(key)
|
2017-07-03 13:52:27 +08:00
|
|
|
end
|
|
|
|
|
2019-06-05 12:30:25 +08:00
|
|
|
if has_override && overrides.present?
|
2015-12-24 01:09:18 +08:00
|
|
|
if options.present?
|
2017-07-03 13:52:27 +08:00
|
|
|
options[:overrides] = overrides
|
2015-12-24 01:09:18 +08:00
|
|
|
|
2015-12-09 14:10:51 +08:00
|
|
|
# I18n likes to use throw...
|
|
|
|
catch(:exception) do
|
2015-12-24 01:09:18 +08:00
|
|
|
return backend.translate(locale, key, options)
|
2015-12-09 14:10:51 +08:00
|
|
|
end
|
|
|
|
else
|
2017-07-03 13:52:27 +08:00
|
|
|
overrides.each do |_k, v|
|
|
|
|
if result = v[key]
|
|
|
|
return result
|
|
|
|
end
|
2015-12-09 14:10:51 +08:00
|
|
|
end
|
2015-11-20 05:36:59 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-24 17:53:03 +08:00
|
|
|
|
2015-12-24 01:09:18 +08:00
|
|
|
translate_no_override(key, options)
|
2015-11-20 05:36:59 +08:00
|
|
|
end
|
|
|
|
|
2013-04-24 12:40:09 +08:00
|
|
|
alias_method :t, :translate
|
2015-11-24 05:45:05 +08:00
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def exists?(key, locale = nil)
|
2018-09-18 18:59:00 +08:00
|
|
|
execute_reload if @requires_reload
|
|
|
|
|
2015-12-24 01:09:18 +08:00
|
|
|
locale ||= config.locale
|
|
|
|
load_locale(locale) unless @loaded_locales.include?(locale)
|
|
|
|
exists_no_cache?(key, locale)
|
2015-11-24 05:45:05 +08:00
|
|
|
end
|
|
|
|
|
2018-09-18 18:59:00 +08:00
|
|
|
def locale=(value)
|
|
|
|
execute_reload if @requires_reload
|
|
|
|
self.locale_no_cache = value
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
RELOAD_MUTEX = Mutex.new
|
|
|
|
|
|
|
|
def execute_reload
|
|
|
|
RELOAD_MUTEX.synchronize do
|
|
|
|
return unless @requires_reload
|
|
|
|
|
|
|
|
@loaded_locales = []
|
|
|
|
@cache = nil
|
|
|
|
@overrides_by_site = {}
|
|
|
|
|
|
|
|
reload_no_cache!
|
|
|
|
ensure_all_loaded!
|
|
|
|
|
|
|
|
@requires_reload = false
|
|
|
|
end
|
|
|
|
end
|
2013-04-24 12:40:09 +08:00
|
|
|
end
|
|
|
|
end
|