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
|
|
|
|
# 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-14 05:34:13 +08:00
|
|
|
def reload!
|
|
|
|
@overrides = {}
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def overrides_for(locale)
|
|
|
|
@overrides ||= {}
|
2015-11-19 05:05:53 +08:00
|
|
|
site_overrides = @overrides[RailsMultisite::ConnectionManagement.current_db] ||= {}
|
2015-11-14 05:34:13 +08:00
|
|
|
|
2015-11-19 05:05:53 +08:00
|
|
|
return site_overrides[locale] if site_overrides[locale]
|
|
|
|
locale_overrides = site_overrides[locale] = {}
|
2015-11-14 05:34:13 +08:00
|
|
|
|
|
|
|
|
2015-11-19 05:05:53 +08:00
|
|
|
locale_overrides
|
2015-11-14 05:34:13 +08:00
|
|
|
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)
|
|
|
|
[locale, SiteSetting.default_locale.to_sym, :en].uniq.compact
|
|
|
|
end
|
|
|
|
|
2015-11-20 05:36:59 +08:00
|
|
|
def lookup(locale, key, scope = [], options = {})
|
|
|
|
|
|
|
|
# Support interpolation and pluralization of overrides
|
|
|
|
if options[:overrides]
|
|
|
|
if options[:count]
|
|
|
|
result = {}
|
|
|
|
options[:overrides].each do |k, v|
|
|
|
|
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
|
|
|
|
|
|
|
|
super(locale, key, scope, options)
|
2015-11-14 05:34:13 +08:00
|
|
|
end
|
|
|
|
|
2015-11-14 04:42:01 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|