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