2013-08-24 05:35:01 +08:00
|
|
|
require_dependency 'enum_site_setting'
|
|
|
|
|
|
|
|
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
|
2013-08-24 05:35:01 +08:00
|
|
|
supported_locales.map do |l|
|
2016-09-08 06:04:01 +08:00
|
|
|
lang = language_names[l] || language_names[l[0..1]]
|
2017-07-28 09:20:09 +08:00
|
|
|
{ name: lang ? lang['nativeName'] : l, value: l }
|
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
|
|
|
|
@language_names ||= YAML.load(File.read(File.join(Rails.root, 'config', 'locales', 'names.yml')))
|
|
|
|
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
|
|
|
|
app_client_files = Dir.glob(
|
|
|
|
File.join(Rails.root, 'config', 'locales', 'client.*.yml')
|
|
|
|
)
|
|
|
|
|
2017-12-20 19:14:39 +08:00
|
|
|
unless ignore_plugins?
|
2017-11-16 01:50:20 +08:00
|
|
|
app_client_files += Dir.glob(
|
|
|
|
File.join(Rails.root, 'plugins', '*', 'config', 'locales', 'client.*.yml')
|
|
|
|
)
|
|
|
|
end
|
2017-11-16 00:20:04 +08:00
|
|
|
|
2017-12-20 19:14:39 +08:00
|
|
|
app_client_files.map { |x| x.split('.')[-2] }
|
|
|
|
.uniq
|
|
|
|
.select { |locale| valid_locale?(locale) }
|
|
|
|
.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
|
|
|
|
2017-12-20 19:14:39 +08:00
|
|
|
def self.valid_locale?(locale)
|
|
|
|
assets = Rails.configuration.assets
|
|
|
|
|
|
|
|
assets.precompile.grep(/locales\/#{locale}(?:\.js)?/).present? &&
|
|
|
|
(Dir.glob(File.join(Rails.root, 'app', 'assets', 'javascripts', 'locales', "#{locale}.js.erb")).present? ||
|
|
|
|
Dir.glob(File.join(Rails.root, 'plugins', '*', 'assets', 'locales', "#{locale}.js.erb")).present?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.ignore_plugins?
|
|
|
|
Rails.env.test? && ENV['LOAD_PLUGINS'] != "1"
|
|
|
|
end
|
|
|
|
|
|
|
|
private_class_method :valid_locale?
|
|
|
|
private_class_method :ignore_plugins?
|
2013-06-11 23:39:55 +08:00
|
|
|
end
|