2019-05-03 06:17:27 +08:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2015-11-14 04:42:01 +08:00
|
|
|
|
require 'i18n/backend/pluralization'
|
|
|
|
|
|
|
|
|
|
module I18n
|
|
|
|
|
module Backend
|
|
|
|
|
class DiscourseI18n < I18n::Backend::Simple
|
2015-11-20 01:35:51 +08:00
|
|
|
|
include I18n::Backend::Fallbacks
|
2015-11-14 04:42:01 +08:00
|
|
|
|
include I18n::Backend::Pluralization
|
|
|
|
|
|
|
|
|
|
def available_locales
|
|
|
|
|
LocaleSiteSetting.supported_locales.map(&:to_sym)
|
|
|
|
|
end
|
|
|
|
|
|
2015-11-14 05:34:13 +08:00
|
|
|
|
def reload!
|
2015-12-24 01:09:18 +08:00
|
|
|
|
@pluralizers = {}
|
2021-04-21 17:36:32 +08:00
|
|
|
|
# this calls `reload!` in our patch lib/freedom_patches/translate_accelerator.rb
|
|
|
|
|
I18n.reload!
|
2015-11-14 05:34:13 +08:00
|
|
|
|
super
|
|
|
|
|
end
|
|
|
|
|
|
2015-11-14 04:42:01 +08:00
|
|
|
|
# force explicit loading
|
|
|
|
|
def load_translations(*filenames)
|
|
|
|
|
unless filenames.empty?
|
2020-11-11 23:44:01 +08:00
|
|
|
|
self.class.sort_locale_files(filenames.flatten).each do |filename|
|
|
|
|
|
load_file(filename)
|
|
|
|
|
end
|
2015-11-14 04:42:01 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-05-11 07:50:17 +08:00
|
|
|
|
def pluralize(locale, entry, count)
|
|
|
|
|
begin
|
|
|
|
|
super
|
|
|
|
|
rescue I18n::InvalidPluralizationData => e
|
|
|
|
|
raise e if I18n.fallbacks[locale] == [locale]
|
|
|
|
|
throw(:exception, e)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-11-11 23:44:01 +08:00
|
|
|
|
def self.sort_locale_files(files)
|
2021-06-16 23:04:21 +08:00
|
|
|
|
files.sort.sort_by do |filename|
|
2020-11-11 23:44:01 +08:00
|
|
|
|
matches = /(?:client|server)-([1-9]|[1-9][0-9]|100)\..+\.yml/.match(filename)
|
|
|
|
|
matches&.[](1)&.to_i || 0
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-10 08:17:07 +08:00
|
|
|
|
def self.create_search_regexp(query, as_string: false)
|
|
|
|
|
regexp = Regexp.escape(query)
|
|
|
|
|
|
|
|
|
|
regexp.gsub!(/['‘’‚‹›]/, "['‘’‚‹›]")
|
|
|
|
|
regexp.gsub!(/["“”„«»]/, '["“”„«»]')
|
|
|
|
|
regexp.gsub!(/(?:\\\.\\\.\\\.|…)/, '(?:\.\.\.|…)')
|
|
|
|
|
|
|
|
|
|
as_string ? regexp : /#{regexp}/i
|
|
|
|
|
end
|
|
|
|
|
|
2015-11-24 05:45:05 +08:00
|
|
|
|
def search(locale, query)
|
2016-02-14 06:01:05 +08:00
|
|
|
|
results = {}
|
2018-11-10 08:17:07 +08:00
|
|
|
|
regexp = self.class.create_search_regexp(query)
|
2016-02-14 06:01:05 +08:00
|
|
|
|
|
2019-05-11 07:43:48 +08:00
|
|
|
|
I18n.fallbacks[locale].each do |fallback|
|
2018-11-10 08:17:07 +08:00
|
|
|
|
find_results(regexp, results, translations[fallback])
|
2016-02-14 06:01:05 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
results
|
2015-11-24 05:45:05 +08:00
|
|
|
|
end
|
|
|
|
|
|
2015-11-13 05:08:19 +08:00
|
|
|
|
protected
|
2017-02-24 18:31:21 +08:00
|
|
|
|
|
2015-11-24 05:45:05 +08:00
|
|
|
|
def find_results(regexp, results, translations, path = nil)
|
|
|
|
|
return results if translations.blank?
|
2018-06-07 13:28:18 +08:00
|
|
|
|
|
2015-11-24 05:45:05 +08:00
|
|
|
|
translations.each do |k_sym, v|
|
|
|
|
|
k = k_sym.to_s
|
|
|
|
|
key_path = path ? "#{path}.#{k}" : k
|
|
|
|
|
if v.is_a?(String)
|
2016-02-14 06:01:05 +08:00
|
|
|
|
unless results.has_key?(key_path)
|
|
|
|
|
results[key_path] = v if key_path =~ regexp || v =~ regexp
|
2015-11-24 05:45:05 +08:00
|
|
|
|
end
|
|
|
|
|
elsif v.is_a?(Hash)
|
|
|
|
|
find_results(regexp, results, v, key_path)
|
|
|
|
|
end
|
|
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
|
results
|
|
|
|
|
end
|
2015-11-13 05:08:19 +08:00
|
|
|
|
|
2015-12-29 10:31:23 +08:00
|
|
|
|
# Support interpolation and pluralization of overrides by first looking up
|
|
|
|
|
# the original translations before applying our overrides.
|
2015-11-13 05:08:19 +08:00
|
|
|
|
def lookup(locale, key, scope = [], options = {})
|
2015-12-29 10:31:23 +08:00
|
|
|
|
existing_translations = super(locale, key, scope, options)
|
2017-07-11 12:16:48 +08:00
|
|
|
|
return existing_translations if scope.is_a?(Array) && scope.include?(:models)
|
2018-06-07 13:28:18 +08:00
|
|
|
|
|
2017-07-03 13:52:27 +08:00
|
|
|
|
overrides = options.dig(:overrides, locale)
|
2021-04-30 19:54:45 +08:00
|
|
|
|
key = key.to_s
|
2018-06-07 13:28:18 +08:00
|
|
|
|
|
2017-07-11 12:16:48 +08:00
|
|
|
|
if overrides
|
2020-08-29 06:11:45 +08:00
|
|
|
|
if options[:count]
|
|
|
|
|
if !existing_translations
|
|
|
|
|
I18n.fallbacks[locale].drop(1).each do |fallback|
|
|
|
|
|
existing_translations = super(fallback, key, scope, options)
|
|
|
|
|
break if existing_translations.present?
|
2015-12-29 10:31:23 +08:00
|
|
|
|
end
|
2020-08-29 06:11:45 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if existing_translations
|
|
|
|
|
remapped_translations =
|
|
|
|
|
if existing_translations.is_a?(Hash)
|
|
|
|
|
Hash[existing_translations.map { |k, v| ["#{key}.#{k}", v] }]
|
|
|
|
|
elsif existing_translations.is_a?(String)
|
|
|
|
|
Hash[[[key, existing_translations]]]
|
|
|
|
|
end
|
2015-12-29 10:31:23 +08:00
|
|
|
|
|
2020-08-29 06:11:45 +08:00
|
|
|
|
result = {}
|
2015-12-29 10:31:23 +08:00
|
|
|
|
|
2020-08-29 06:11:45 +08:00
|
|
|
|
remapped_translations.merge(overrides).each do |k, v|
|
2021-04-30 19:54:45 +08:00
|
|
|
|
result[k.split('.').last.to_sym] = v if k != key && k.start_with?(key)
|
2020-08-29 06:11:45 +08:00
|
|
|
|
end
|
|
|
|
|
return result if result.size > 0
|
2015-11-13 05:08:19 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-07-03 13:52:27 +08:00
|
|
|
|
return overrides[key] if overrides[key]
|
2015-11-13 05:08:19 +08:00
|
|
|
|
end
|
|
|
|
|
|
2015-12-29 10:31:23 +08:00
|
|
|
|
existing_translations
|
2018-06-07 13:28:18 +08:00
|
|
|
|
end
|
2015-11-14 04:42:01 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|