2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
|
|
|
# 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!
|
2018-09-18 06:59:00 -04:00
|
|
|
alias_method :locale_no_cache=, :locale=
|
|
|
|
|
2019-06-05 00:30:25 -04:00
|
|
|
LRU_CACHE_SIZE = 400
|
2013-04-24 00:40:09 -04:00
|
|
|
|
2020-03-12 13:43:27 -04:00
|
|
|
def init_accelerator!(overrides_enabled: true)
|
|
|
|
@overrides_enabled = overrides_enabled
|
2022-05-04 11:35:22 -04:00
|
|
|
reserve_key(:overrides)
|
2018-09-18 06:59:00 -04:00
|
|
|
execute_reload
|
2015-11-23 16:45:05 -05:00
|
|
|
end
|
|
|
|
|
2013-04-24 00:40:09 -04:00
|
|
|
def reload!
|
2018-09-18 06:59:00 -04:00
|
|
|
@requires_reload = true
|
2013-04-24 00:40:09 -04:00
|
|
|
end
|
|
|
|
|
2015-02-11 20:08:14 -05:00
|
|
|
LOAD_MUTEX = Mutex.new
|
2018-09-18 06:59:00 -04:00
|
|
|
|
2015-02-11 20:08:14 -05:00
|
|
|
def load_locale(locale)
|
2021-04-30 05:56:27 -04:00
|
|
|
locale = locale.to_sym
|
2015-02-11 20:08:14 -05:00
|
|
|
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
|
2023-01-20 13:52:49 -05:00
|
|
|
I18n.backend.load_translations(I18n.load_path.grep(/\.rb\z/))
|
2018-01-25 06:09:18 -05:00
|
|
|
|
|
|
|
# load plural rules from plugins
|
2018-09-03 22:16:21 -04:00
|
|
|
DiscoursePluginRegistry.locales.each do |plugin_locale, options|
|
2018-01-25 06:09:18 -05:00
|
|
|
if options[:plural]
|
2018-09-03 22:16:21 -04:00
|
|
|
I18n.backend.store_translations(plugin_locale, i18n: { plural: options[:plural] })
|
2018-01-25 06:09:18 -05:00
|
|
|
end
|
|
|
|
end
|
2015-02-11 20:08:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# load it
|
2023-01-20 13:52:49 -05:00
|
|
|
I18n.backend.load_translations(I18n.load_path.grep(/\.#{Regexp.escape locale}\.yml\z/))
|
2015-02-11 20:08:14 -05:00
|
|
|
|
2023-08-14 13:11:30 -04:00
|
|
|
if Discourse.allow_dev_populate? || Rails.env.test? || Rails.env.development?
|
2021-03-04 12:34:51 -05:00
|
|
|
I18n.backend.load_translations(
|
2023-01-20 13:52:49 -05:00
|
|
|
I18n.load_path.grep(%r{.*faker.*/#{Regexp.escape locale}\.yml\z}),
|
2021-03-04 12:34:51 -05:00
|
|
|
)
|
|
|
|
I18n.backend.load_translations(
|
2023-01-20 13:52:49 -05:00
|
|
|
I18n.load_path.grep(%r{.*faker.*/#{Regexp.escape locale}/.*\.yml\z}),
|
2021-03-04 12:34:51 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
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!
|
2019-05-10 19:43:48 -04:00
|
|
|
I18n.fallbacks[locale].each { |l| ensure_loaded!(l) }
|
2015-11-13 15:42:01 -05:00
|
|
|
end
|
|
|
|
|
2018-09-18 06:59:00 -04:00
|
|
|
def search(query, opts = {})
|
|
|
|
execute_reload if @requires_reload
|
|
|
|
|
2021-04-30 05:56:27 -04:00
|
|
|
locale = (opts[:locale] || config.locale).to_sym
|
2015-12-23 12:09:18 -05:00
|
|
|
load_locale(locale) unless @loaded_locales.include?(locale)
|
2015-11-23 16:45:05 -05:00
|
|
|
|
2021-12-16 10:44:21 -05:00
|
|
|
results = {}
|
2018-11-09 19:17:07 -05:00
|
|
|
regexp = I18n::Backend::DiscourseI18n.create_search_regexp(query)
|
2021-12-16 10:44:21 -05:00
|
|
|
|
|
|
|
if opts[:only_overridden]
|
|
|
|
add_if_matches(overrides_by_locale(locale), results, regexp)
|
|
|
|
else
|
|
|
|
target = opts[:backend] || backend
|
|
|
|
|
|
|
|
I18n.fallbacks[locale].reverse_each do |fallback|
|
|
|
|
add_if_matches(target.search(fallback, query), results, regexp)
|
|
|
|
add_if_matches(overrides_by_locale(fallback), results, regexp)
|
|
|
|
end
|
2015-11-23 16:45:05 -05:00
|
|
|
end
|
2021-12-16 10:44:21 -05:00
|
|
|
|
2015-11-23 16:45:05 -05:00
|
|
|
results
|
|
|
|
end
|
|
|
|
|
2021-12-16 10:44:21 -05:00
|
|
|
def add_if_matches(translations, results, regexp)
|
|
|
|
translations.each { |key, value| results[key] = value if key =~ regexp || value =~ regexp }
|
|
|
|
end
|
|
|
|
|
2015-07-15 12:04:45 -04:00
|
|
|
def ensure_loaded!(locale)
|
2021-04-30 05:56:27 -04:00
|
|
|
locale = locale.to_sym
|
2015-07-15 12:04:45 -04:00
|
|
|
@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
|
|
|
|
|
2019-06-05 00:30:25 -04:00
|
|
|
class MissingTranslation
|
|
|
|
end
|
2015-12-23 12:09:18 -05:00
|
|
|
|
2019-06-05 00:30:25 -04: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
|
|
|
|
|
2019-11-08 01:30:42 -05:00
|
|
|
return translate_no_cache(key, **options) if dup_options.present?
|
2019-06-05 00:30:25 -04:00
|
|
|
|
|
|
|
locale ||= config.locale
|
2021-04-30 05:56:27 -04:00
|
|
|
locale = locale.to_sym
|
2015-12-23 12:09:18 -05:00
|
|
|
|
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
|
|
|
|
2019-06-05 00:30:25 -04:00
|
|
|
val =
|
|
|
|
@cache.getset(k) do
|
|
|
|
begin
|
2019-06-05 08:19:46 -04:00
|
|
|
translate_no_cache(key, locale: locale, raise: true).freeze
|
2019-06-05 00:30:25 -04:00
|
|
|
rescue I18n::MissingTranslationData
|
|
|
|
MissingTranslation
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2019-06-05 00:30:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if val != MissingTranslation
|
|
|
|
val
|
|
|
|
elsif should_raise
|
|
|
|
raise I18n::MissingTranslationData.new(locale, key)
|
|
|
|
else
|
2023-06-16 10:28:03 -04:00
|
|
|
-"Translation missing: #{locale}.#{key}"
|
2013-04-24 00:40:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-23 12:09:18 -05:00
|
|
|
def overrides_by_locale(locale)
|
2021-12-16 10:44:21 -05:00
|
|
|
return {} unless @overrides_enabled
|
2019-06-12 22:58:27 -04:00
|
|
|
return {} if GlobalSetting.skip_db?
|
2021-04-30 05:56:27 -04:00
|
|
|
locale = locale.to_sym
|
2019-06-12 22:58:27 -04:00
|
|
|
|
2019-11-04 20:15:44 -05:00
|
|
|
execute_reload if @requires_reload
|
|
|
|
|
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
|
2021-12-16 10:54:45 -05:00
|
|
|
translations_overrides =
|
|
|
|
TranslationOverride.where(locale: locale).pluck(:translation_key, :value)
|
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] ||= {}
|
2021-12-16 10:54:45 -05:00
|
|
|
by_locale[tuple[0]] = 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
|
2019-09-16 23:38:01 -04:00
|
|
|
rescue ActiveRecord::StatementInvalid => e
|
2020-05-23 00:56:13 -04:00
|
|
|
if PG::UndefinedTable === e.cause || PG::UndefinedColumn === e.cause
|
2019-09-16 23:38:01 -04:00
|
|
|
{}
|
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
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 translate(*args)
|
2018-09-18 06:59:00 -04:00
|
|
|
execute_reload if @requires_reload
|
|
|
|
|
2015-12-23 12:09:18 -05:00
|
|
|
options = args.last.is_a?(Hash) ? args.pop.dup : {}
|
|
|
|
key = args.shift
|
2021-04-30 05:56:27 -04:00
|
|
|
locale = (options[:locale] || config.locale).to_sym
|
2015-12-23 12:09:18 -05:00
|
|
|
|
|
|
|
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 = {}
|
2021-12-16 10:44:21 -05:00
|
|
|
has_count = options.has_key?(:count)
|
2019-06-05 00:30:25 -04:00
|
|
|
|
2019-05-10 19:43:48 -04:00
|
|
|
I18n.fallbacks[locale].each do |l|
|
2021-12-16 10:44:21 -05:00
|
|
|
overrides_for_locale = overrides_by_locale(l)
|
|
|
|
overrides[l] = overrides_for_locale if has_count || overrides_for_locale.key?(key)
|
2017-07-03 01:52:27 -04:00
|
|
|
end
|
|
|
|
|
2021-12-16 10:44:21 -05:00
|
|
|
if overrides.present?
|
|
|
|
no_options = options.empty? || (options.size == 1 && options.has_key?(:locale))
|
2015-12-23 12:09:18 -05:00
|
|
|
|
2021-12-16 10:44:21 -05:00
|
|
|
# Shortcut if the current locale has an override and there are no options.
|
|
|
|
if no_options && (override = overrides.dig(locale, key))
|
|
|
|
return override
|
|
|
|
end
|
|
|
|
|
|
|
|
options[:overrides] = overrides
|
|
|
|
|
|
|
|
# I18n likes to use throw...
|
|
|
|
catch(:exception) { return backend.translate(locale, key, options) }
|
2015-11-19 16:36:59 -05:00
|
|
|
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
|
|
|
|
2015-12-23 12:09:18 -05:00
|
|
|
def exists?(key, locale = nil)
|
2018-09-18 06:59:00 -04:00
|
|
|
execute_reload if @requires_reload
|
|
|
|
|
2015-12-23 12:09:18 -05:00
|
|
|
locale ||= config.locale
|
2021-04-30 05:56:27 -04:00
|
|
|
locale = locale.to_sym
|
2015-12-23 12:09:18 -05:00
|
|
|
load_locale(locale) unless @loaded_locales.include?(locale)
|
|
|
|
exists_no_cache?(key, locale)
|
2015-11-23 16:45:05 -05:00
|
|
|
end
|
|
|
|
|
2018-09-18 06:59:00 -04:00
|
|
|
def locale=(value)
|
2021-04-30 05:56:27 -04:00
|
|
|
value = value.to_sym
|
2018-09-18 06:59:00 -04:00
|
|
|
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 00:40:09 -04:00
|
|
|
end
|
|
|
|
end
|