2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-08-23 17:35:01 -04:00
|
|
|
class LocaleSiteSetting < EnumSiteSetting
|
2013-06-11 11:39:55 -04:00
|
|
|
|
|
|
|
def self.valid_value?(val)
|
|
|
|
supported_locales.include?(val)
|
|
|
|
end
|
|
|
|
|
2013-06-23 01:07:25 -04:00
|
|
|
def self.values
|
2018-01-25 06:09:18 -05:00
|
|
|
@values ||= supported_locales.map do |locale|
|
|
|
|
lang = language_names[locale] || language_names[locale.split("_")[0]]
|
|
|
|
{
|
|
|
|
name: lang ? lang['nativeName'] : locale,
|
|
|
|
value: locale
|
|
|
|
}
|
2013-08-23 17:35:01 -04:00
|
|
|
end
|
2013-06-11 11:39:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@lock = Mutex.new
|
|
|
|
|
2016-09-07 18:04:01 -04:00
|
|
|
def self.language_names
|
|
|
|
return @language_names if @language_names
|
|
|
|
|
|
|
|
@lock.synchronize do
|
2018-01-25 06:09:18 -05:00
|
|
|
@language_names ||= begin
|
|
|
|
names = YAML.load(File.read(File.join(Rails.root, 'config', 'locales', 'names.yml')))
|
|
|
|
|
|
|
|
DiscoursePluginRegistry.locales.each do |locale, options|
|
|
|
|
if !names.key?(locale) && options[:name] && options[:nativeName]
|
|
|
|
names[locale] = { "name" => options[:name], "nativeName" => options[:nativeName] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
names
|
|
|
|
end
|
2016-09-07 18:04:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-11 11:39:55 -04:00
|
|
|
def self.supported_locales
|
|
|
|
@lock.synchronize do
|
2017-11-15 11:20:04 -05:00
|
|
|
@supported_locales ||= begin
|
2018-01-25 06:09:18 -05:00
|
|
|
locales = Dir.glob(
|
2017-11-15 11:20:04 -05:00
|
|
|
File.join(Rails.root, 'config', 'locales', 'client.*.yml')
|
2018-01-25 06:09:18 -05:00
|
|
|
).map { |x| x.split('.')[-2] }
|
2017-11-15 11:20:04 -05:00
|
|
|
|
2018-01-25 06:09:18 -05:00
|
|
|
locales += DiscoursePluginRegistry.locales.keys
|
|
|
|
locales.uniq.sort
|
2017-11-15 11:20:04 -05:00
|
|
|
end
|
2013-06-11 11:39:55 -04:00
|
|
|
end
|
|
|
|
end
|
2014-10-03 23:07:20 -04:00
|
|
|
|
2018-01-25 06:09:18 -05:00
|
|
|
def self.reset!
|
|
|
|
@lock.synchronize do
|
|
|
|
@values = @language_names = @supported_locales = nil
|
|
|
|
end
|
2017-12-20 06:14:39 -05:00
|
|
|
end
|
|
|
|
|
2019-05-15 17:43:00 -04:00
|
|
|
FALLBACKS ||= {
|
2021-01-20 15:32:22 -05:00
|
|
|
en_GB: :en
|
2019-05-15 17:43:00 -04:00
|
|
|
}
|
|
|
|
|
2018-01-25 06:09:18 -05:00
|
|
|
def self.fallback_locale(locale)
|
2019-05-15 17:43:00 -04:00
|
|
|
fallback_locale = FALLBACKS[locale.to_sym]
|
|
|
|
return fallback_locale if fallback_locale
|
|
|
|
|
2018-01-25 06:09:18 -05:00
|
|
|
plugin_locale = DiscoursePluginRegistry.locales[locale.to_s]
|
|
|
|
plugin_locale ? plugin_locale[:fallbackLocale]&.to_sym : nil
|
2017-12-20 06:14:39 -05:00
|
|
|
end
|
2013-06-11 11:39:55 -04:00
|
|
|
end
|