diff --git a/src/includes/api/ExtSettingsController.php b/src/includes/api/ExtSettingsController.php index 831a1b4..404b18e 100644 --- a/src/includes/api/ExtSettingsController.php +++ b/src/includes/api/ExtSettingsController.php @@ -19,12 +19,19 @@ class ExtSettingsController extends ApiController { $data = $instance->settingsPage(); - $title = htmlspecialchars($instance::title); + $lang = Lang::instance(); + $nameKey = 'ext.'. $ext. '.name'; + if ($lang->hasKey($nameKey)) { + $name = htmlspecialchars($lang->get($nameKey)); + } + else { + $name = htmlspecialchars($instance::name); + } $escapedExt = htmlspecialchars($ext); - $e = function($s) { return __($s, true); }; + $e = function($s) use($lang) { return htmlspecialchars($lang->get($s)); }; $data = << $title +

$name

diff --git a/src/includes/class.lang.php b/src/includes/class.lang.php index 4125d0c..df00818 100644 --- a/src/includes/class.lang.php +++ b/src/includes/class.lang.php @@ -94,6 +94,11 @@ class Lang return $key; } + function hasKey(string $key): bool + { + return isset($this->strings[$key]); + } + function rtl() { if ( isset($this->strings['_rtl']) ) { @@ -157,5 +162,32 @@ class Lang return $this->code; } + public function loadExtensionLang(string $ext) + { + $langDir = MTT_EXT. $ext. '/lang/'; + if (!is_dir($langDir)) { + return; + } + if (!file_exists($langDir. 'en.json')) { + return; + } + if (file_exists($langDir. $this->code. '.json')) { + $langStr = file_get_contents($langDir. $this->code. '.json'); + $lang = json_decode($langStr, true) ?? []; + } + $defStr = file_get_contents($langDir. 'en.json'); + $def = json_decode($defStr, true) ?? []; + + $lang = array_replace($def, $lang); + if (isset($lang['_header'])) { + unset($lang['_header']); + } + if (isset($lang['_ltr'])) { + unset($lang['_ltr']); + } + + $this->strings = array_replace($this->strings, $lang); + } + } diff --git a/src/includes/classes.php b/src/includes/classes.php index 551901f..68160a4 100644 --- a/src/includes/classes.php +++ b/src/includes/classes.php @@ -74,7 +74,7 @@ abstract class ApiController abstract class MTTExtension { const bundleId = ''; - const title = ''; + const name = ''; abstract function init(); } @@ -120,13 +120,15 @@ class MTTExtensionLoader } $className = get_class($instance); - if (!defined("$className::bundleId") || !defined("$className::title")) { - throw new Exception("Failed to register extension '$ext': require class constants (bundleId, title)"); + if (!defined("$className::bundleId") || !defined("$className::name")) { + throw new Exception("Failed to register extension '$ext': require class constants (bundleId, name)"); } if ($instance::bundleId != $ext) { throw new Exception("Extension '$ext' bundleId does not conforms to extension dir"); } + Lang::instance()->loadExtensionLang($ext); + $instance->init(); self::$exts[$ext] = $instance; } @@ -148,6 +150,7 @@ class MTTExtensionLoader */ public static function bundles(): array { + $lang = Lang::instance(); $a = []; $files = array_diff(scandir(MTT_EXT) ?? [], ['.', '..']); foreach ($files as $ext) { @@ -161,12 +164,23 @@ class MTTExtensionLoader continue; } $meta = json_decode($jsonData, true); - if (!is_array($meta) || !isset($meta['bundleId']) || !isset($meta['title']) || !isset($meta['description'])) { + if (!is_array($meta) || !isset($meta['bundleId']) || !isset($meta['name']) || !isset($meta['description'])) { continue; } - if (!is_string($meta['bundleId']) || !is_string($meta['title']) || !is_string($meta['description'])) { + if (!is_string($meta['bundleId']) || !is_string($meta['name']) || !is_string($meta['description'])) { continue; } + if ( $lang->langCode() != 'en' && is_dir(MTT_EXT. $ext. '/lang') ) { + $lf = MTT_EXT. $ext. '/lang/'. $lang->langCode(). '.json'; + if (file_exists($lf)) { + $jsonText = file_get_contents($lf) ?? ''; + $json = json_decode($jsonText, true) ?? []; + $lt = $json['ext.'.$ext.'.name'] ?? null; + if ($lt !== null) { + $meta['name'] = $lt; + } + } + } $a[$ext] = $meta; } return $a; diff --git a/src/settings.php b/src/settings.php index 486b3c9..902ba8b 100644 --- a/src/settings.php +++ b/src/settings.php @@ -194,7 +194,7 @@ function listExtensions() $activatedExts = Config::get('extensions'); if (!is_array($activatedExts)) $activatedExts = []; foreach ($extBundles as $ext => $meta) { - $out = htmlspecialchars($meta['title']). ' '; + $out = htmlspecialchars($meta['name']). ' '; if (in_array($ext, $activatedExts)) { $out .= "". __('set_deactivate', true). ''; $instance = MTTExtensionLoader::extensionInstance($ext);