extensions can have own translations

This commit is contained in:
maxpozdeev 2022-09-16 12:08:55 +03:00
parent 09225acfde
commit 81d32e0a1b
4 changed files with 62 additions and 9 deletions

View file

@ -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 =
<<<EOD
<h3 class="page-title"><a class="mtt-back-button"></a> $title </h3>
<h3 class="page-title"><a class="mtt-back-button"></a> $name </h3>
<div id="settings_msg" style="display:none"></div>
<form id="ext_settings_form" data-ext="$escapedExt">
<div class="mtt-settings-table">

View file

@ -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);
}
}

View file

@ -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;

View file

@ -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 .= "<a href='#' data-settings-link='ext-deactivate' data-ext='". htmlspecialchars($ext). "'>". __('set_deactivate', true). '</a>';
$instance = MTTExtensionLoader::extensionInstance($ext);