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
|
2021-10-27 04:39:28 -04:00
|
|
|
names = YAML.safe_load(File.read(File.join(Rails.root, "config", "locales", "names.yml")))
|
2018-01-25 06:09:18 -05:00
|
|
|
|
|
|
|
DiscoursePluginRegistry.locales.each do |locale, options|
|
|
|
|
if !names.key?(locale) && options[:name] && options[:nativeName]
|
|
|
|
names[locale] = { "name" => options[:name], "nativeName" => options[:nativeName] }
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2018-01-25 06:09:18 -05:00
|
|
|
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
|
2017-11-15 11:20:04 -05:00
|
|
|
.glob(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 { @values = @language_names = @supported_locales = nil }
|
2017-12-20 06:14:39 -05:00
|
|
|
end
|
|
|
|
|
2024-10-15 22:09:07 -04:00
|
|
|
FALLBACKS = { 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
|