2015-11-13 15:42:01 -05:00
|
|
|
require 'i18n/backend/pluralization'
|
|
|
|
|
|
|
|
module I18n
|
|
|
|
module Backend
|
|
|
|
class DiscourseI18n < I18n::Backend::Simple
|
2015-11-19 12:35:51 -05:00
|
|
|
include I18n::Backend::Fallbacks
|
2015-11-13 15:42:01 -05:00
|
|
|
include I18n::Backend::Pluralization
|
|
|
|
|
|
|
|
def available_locales
|
|
|
|
# in case you are wondering this is:
|
|
|
|
# Dir.glob( File.join(Rails.root, 'config', 'locales', 'client.*.yml') )
|
|
|
|
# .map {|x| x.split('.')[-2]}.sort
|
|
|
|
LocaleSiteSetting.supported_locales.map(&:to_sym)
|
|
|
|
end
|
|
|
|
|
2015-11-13 16:34:13 -05:00
|
|
|
def reload!
|
|
|
|
@overrides = {}
|
2015-12-23 12:09:18 -05:00
|
|
|
@pluralizers = {}
|
2015-11-13 16:34:13 -05:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2015-11-13 15:42:01 -05:00
|
|
|
# force explicit loading
|
|
|
|
def load_translations(*filenames)
|
|
|
|
unless filenames.empty?
|
|
|
|
filenames.flatten.each { |filename| load_file(filename) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fallbacks(locale)
|
|
|
|
[locale, SiteSetting.default_locale.to_sym, :en].uniq.compact
|
|
|
|
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-23 16:45:05 -05:00
|
|
|
def search(locale, query)
|
|
|
|
find_results(/#{query}/i, {}, translations[locale])
|
|
|
|
end
|
|
|
|
|
2015-11-12 16:08:19 -05:00
|
|
|
protected
|
2015-11-23 16:45:05 -05: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)
|
|
|
|
results[key_path] = v if key_path =~ regexp || v =~ regexp
|
|
|
|
elsif v.is_a?(Hash)
|
|
|
|
find_results(regexp, results, v, key_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
results
|
|
|
|
end
|
2015-11-12 16:08:19 -05:00
|
|
|
|
2015-12-28 21:31:23 -05:00
|
|
|
# Support interpolation and pluralization of overrides by first looking up
|
|
|
|
# the original translations before applying our overrides.
|
2015-11-12 16:08:19 -05:00
|
|
|
def lookup(locale, key, scope = [], options = {})
|
2015-12-28 21:31:23 -05:00
|
|
|
existing_translations = super(locale, key, scope, options)
|
|
|
|
|
|
|
|
if options[:overrides] && existing_translations
|
2015-11-12 16:08:19 -05:00
|
|
|
if options[:count]
|
2015-12-28 21:31:23 -05:00
|
|
|
|
|
|
|
existing_translations =
|
|
|
|
if existing_translations.is_a?(Hash)
|
|
|
|
Hash[existing_translations.map { |k, v| [k.to_s.prepend("#{key}."), v] }]
|
|
|
|
elsif existing_translations.is_a?(String)
|
|
|
|
Hash[[[key, existing_translations]]]
|
|
|
|
end
|
|
|
|
|
2015-11-12 16:08:19 -05:00
|
|
|
result = {}
|
2015-12-28 21:31:23 -05:00
|
|
|
|
|
|
|
existing_translations.merge(options[:overrides]).each do |k, v|
|
2015-11-12 16:08:19 -05:00
|
|
|
result[k.split('.').last.to_sym] = v if k != key && k.start_with?(key.to_s)
|
|
|
|
end
|
|
|
|
return result if result.size > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
return options[:overrides][key] if options[:overrides][key]
|
|
|
|
end
|
|
|
|
|
2015-12-28 21:31:23 -05:00
|
|
|
existing_translations
|
2015-11-12 16:08:19 -05:00
|
|
|
end
|
|
|
|
|
2015-11-13 15:42:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|