2015-11-14 04:42:01 +08:00
|
|
|
require 'i18n/backend/pluralization'
|
2017-09-15 13:02:11 +08:00
|
|
|
require_dependency 'locale_site_setting'
|
2015-11-14 04:42:01 +08:00
|
|
|
|
|
|
|
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!
|
|
|
|
@overrides = {}
|
2015-12-24 01:09:18 +08:00
|
|
|
@pluralizers = {}
|
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?
|
|
|
|
filenames.flatten.each { |filename| load_file(filename) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fallbacks(locale)
|
2018-01-22 21:42:12 +08:00
|
|
|
I18n.fallbacks[locale]
|
2015-11-14 04:42:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def exists?(locale, key)
|
|
|
|
fallbacks(locale).each do |fallback|
|
|
|
|
begin
|
|
|
|
return true if super(fallback, key)
|
|
|
|
rescue I18n::InvalidLocale
|
|
|
|
# we do nothing when the locale is invalid, as this is a fallback anyways.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2015-11-24 05:45:05 +08:00
|
|
|
def search(locale, query)
|
2016-02-14 06:01:05 +08:00
|
|
|
results = {}
|
|
|
|
|
|
|
|
fallbacks(locale).each do |fallback|
|
2018-08-31 05:32:24 +08:00
|
|
|
find_results(/#{Regexp.escape(query)}/i, 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
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def find_results(regexp, results, translations, path = nil)
|
|
|
|
return results if translations.blank?
|
|
|
|
|
|
|
|
translations.each do |k_sym, v|
|
|
|
|
k = k_sym.to_s
|
|
|
|
key_path = path ? "#{path}.#{k}" : k
|
|
|
|
if v.is_a?(String)
|
|
|
|
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
|
2018-06-07 13:28:18 +08:00
|
|
|
elsif v.is_a?(Hash)
|
|
|
|
find_results(regexp, results, v, key_path)
|
2015-11-24 05:45:05 +08:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
results
|
|
|
|
end
|
2015-11-13 05:08:19 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
# Support interpolation and pluralization of overrides by first looking up
|
|
|
|
# the original translations before applying our overrides.
|
|
|
|
def lookup(locale, key, scope = [], options = {})
|
|
|
|
existing_translations = super(locale, key, scope, options)
|
|
|
|
return existing_translations if scope.is_a?(Array) && scope.include?(:models)
|
|
|
|
|
|
|
|
overrides = options.dig(:overrides, locale)
|
|
|
|
|
|
|
|
if overrides
|
|
|
|
if existing_translations && options[:count]
|
|
|
|
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
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
result = {}
|
2015-12-29 10:31:23 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
remapped_translations.merge(overrides).each do |k, v|
|
|
|
|
result[k.split('.').last.to_sym] = v if k != key && k.start_with?(key.to_s)
|
2015-11-13 05:08:19 +08:00
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
return result if result.size > 0
|
2015-11-13 05:08:19 +08:00
|
|
|
end
|
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
return overrides[key] if overrides[key]
|
2015-11-13 05:08:19 +08:00
|
|
|
end
|
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
existing_translations
|
|
|
|
end
|
|
|
|
|
2015-11-14 04:42:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|