2013-08-23 16:35:01 -05:00
|
|
|
require_dependency 'enum_site_setting'
|
|
|
|
|
|
|
|
class LocaleSiteSetting < EnumSiteSetting
|
2013-06-11 10:39:55 -05:00
|
|
|
|
|
|
|
def self.valid_value?(val)
|
|
|
|
supported_locales.include?(val)
|
|
|
|
end
|
|
|
|
|
2013-06-23 00:07:25 -05:00
|
|
|
def self.values
|
2018-01-25 05:09:18 -06: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 16:35:01 -05:00
|
|
|
end
|
2013-06-11 10:39:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@lock = Mutex.new
|
|
|
|
|
2016-09-07 17:04:01 -05:00
|
|
|
def self.language_names
|
|
|
|
return @language_names if @language_names
|
|
|
|
|
|
|
|
@lock.synchronize do
|
2018-01-25 05:09:18 -06: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 17:04:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-11 10:39:55 -05:00
|
|
|
def self.supported_locales
|
|
|
|
@lock.synchronize do
|
2017-11-15 10:20:04 -06:00
|
|
|
@supported_locales ||= begin
|
2018-01-25 05:09:18 -06:00
|
|
|
locales = Dir.glob(
|
2017-11-15 10:20:04 -06:00
|
|
|
File.join(Rails.root, 'config', 'locales', 'client.*.yml')
|
2018-01-25 05:09:18 -06:00
|
|
|
).map { |x| x.split('.')[-2] }
|
2017-11-15 10:20:04 -06:00
|
|
|
|
2018-01-25 05:09:18 -06:00
|
|
|
locales += DiscoursePluginRegistry.locales.keys
|
|
|
|
locales.uniq.sort
|
2017-11-15 10:20:04 -06:00
|
|
|
end
|
2013-06-11 10:39:55 -05:00
|
|
|
end
|
|
|
|
end
|
2014-10-03 22:07:20 -05:00
|
|
|
|
2018-01-25 05:09:18 -06:00
|
|
|
def self.reset!
|
|
|
|
@lock.synchronize do
|
|
|
|
@values = @language_names = @supported_locales = nil
|
|
|
|
end
|
2017-12-20 05:14:39 -06:00
|
|
|
end
|
|
|
|
|
2018-01-25 05:09:18 -06:00
|
|
|
def self.fallback_locale(locale)
|
|
|
|
plugin_locale = DiscoursePluginRegistry.locales[locale.to_s]
|
|
|
|
plugin_locale ? plugin_locale[:fallbackLocale]&.to_sym : nil
|
2017-12-20 05:14:39 -06:00
|
|
|
end
|
2013-06-11 10:39:55 -05:00
|
|
|
end
|