From: Vincent Vanwaelscappel Date: Mon, 11 Dec 2023 14:14:58 +0000 (+0100) Subject: wip #6568 @1 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=b93554d7601b25fe75ea1a2907361b7ba2cbdbce;p=fluidbook-toolbox.git wip #6568 @1 --- diff --git a/app/Http/Controllers/Admin/Operations/ContentTranslate/ExcelExportOperation.php b/app/Http/Controllers/Admin/Operations/ContentTranslate/ExcelExportOperation.php index 01f147c02..2fa581285 100644 --- a/app/Http/Controllers/Admin/Operations/ContentTranslate/ExcelExportOperation.php +++ b/app/Http/Controllers/Admin/Operations/ContentTranslate/ExcelExportOperation.php @@ -14,7 +14,6 @@ trait ExcelExportOperation { protected function setupExcelExportRoutes($segment, $routeName, $controller) { - Route::match(['get'], $segment . '/excel/export/{locale}', $controller . '@excelExport'); } diff --git a/app/Models/Base/ToolboxBaseTranslate.php b/app/Models/Base/ToolboxBaseTranslate.php new file mode 100644 index 000000000..cfc6f5c39 --- /dev/null +++ b/app/Models/Base/ToolboxBaseTranslate.php @@ -0,0 +1,153 @@ +_availableLocales = \Cubist\Locale\Locale::getList(App::getLocale()); + $this->addEditAction('content_translate.excel_export'); + $this->addEditAction('content_translate.excel_import'); + + parent::__construct($attributes); + } + + public static function getName() + { + return static::$_name; + } + + protected function _getLanguageFile($locale) + { + return static::getLanguageFile($locale); + } + + public static function getLanguageFile($locale) + { + $n = ''; + if (static::$_name) { + $n = static::$_name . '.'; + } + return resource_path('lang/' . $n . $locale . '.json'); + } + + public function getExtensions() + { + $res = parent::getExtensions(); + $res[] = 'js'; + return $res; + } + + protected static function _getCacheKey() + { + return 'all_' . Files::hashFileAttributes(__FILE__) . '_' . static::$_name . '_translations'; + } + + public static function getAllTranslations($force = true) + { + if (null === static::$_allTranslations) { + + + $cacheKey = static::_getCacheKey(); + if ($force) { + Cache::forget($cacheKey); + } + static::$_allTranslations = Cache::remember($cacheKey, 3600, function () { + start_measure('Get all ' . static::$_name . ' translations !'); + $t = static::find(1); + try { + $json = json_decode($t->getRawOriginal('content_translatable'), true, 512, JSON_THROW_ON_ERROR); + } catch (\Exception $e) { + $json = []; + } + + $res = []; + foreach ($json as $code => $tr) { + $res[$code] = []; + foreach ($tr as $k => $v) { + $res[$code][$k] = ['str' => static::keyToStr($k), 'translation' => $v]; + } + } + stop_measure('Get all ' . static::$_name . ' translations !'); + return $res; + + }); + } + return static::$_allTranslations; + } + + public static function getCompiledTranslations() + { + $raw = static::getAllTranslations(); + $res = []; + foreach ($raw as $code => $data) { + $res[$code] = []; + foreach ($data as $k => $v) { + if (is_string($v)) { + $res[$code][$k] = $v; + } else { + $res[$code][$v['str']] = $v['translation']; + } + } + } + return $res; + } + + /** + * @throws \JsonException + */ + public static function updateTranslation($locale, $translations) + { + /** @var static $t */ + $t = static::find(1); + $json = json_decode($t->getRawOriginal('content_translatable'), true, 512, JSON_THROW_ON_ERROR); + foreach ($translations as $k => $v) { + $json[$locale][$k] = $v; + } + $t->setRawAttributes(['content_translatable' => json_encode($json, JSON_THROW_ON_ERROR)]); + $t->save(); + } + + public function onSaved(): bool + { + Cache::forget(static::_getCacheKey()); + return parent::onSaved(); + } + + /** + * @param string $locale + * @return array[]|null + */ + public static function getLocaleTranslations($locale, $compiled = false) + { + $all = static::getAllTranslations(false); + $res = $all[$locale] ?? []; + if (!$compiled) { + return $res; + } + $comp = []; + foreach ($res as $k => $v) { + if (is_string($v)) { + $comp[$k] = $v; + } else { + $comp[$v['str']] = $v['translation']; + } + } + return $comp; + } + +} diff --git a/app/Models/Base/ToolboxContentTranslate.php b/app/Models/Base/ToolboxContentTranslate.php index f28d0835e..8d0b2b3b3 100644 --- a/app/Models/Base/ToolboxContentTranslate.php +++ b/app/Models/Base/ToolboxContentTranslate.php @@ -10,145 +10,12 @@ use Cubist\Util\Files\Files; use Cubist\Util\PHP; use Illuminate\Support\Facades\Cache; -class ToolboxContentTranslate extends Translate +class ToolboxContentTranslate extends ToolboxBaseTranslate { - protected static $_allTranslations = null; protected static $_basePath = ''; - protected $_operations = [ExcelExportOperation::class, ExcelImportOperation::class]; - - public function __construct(array $attributes = []) - { - PHP::neverStop(); - - $this->_availableLocales = \Cubist\Locale\Locale::getList(App::getLocale()); - $this->addEditAction('content_translate.excel_export'); - $this->addEditAction('content_translate.excel_import'); - - parent::__construct($attributes); - } - - public static function getName() - { - return static::$_name; - } - - protected function _getLanguageFile($locale) - { - return static::getLanguageFile($locale); - } - - public static function getLanguageFile($locale) - { - return resource_path('lang/' . static::$_name . '.' . $locale . '.json'); - } - - public function getExtensions() - { - $res = parent::getExtensions(); - $res[] = 'js'; - return $res; - } - - protected static function _getCacheKey() - { - return 'all_' . Files::hashFileAttributes(__FILE__) . '_' . static::$_name . '_translations'; - } - - public static function getAllTranslations($force = true) - { - if (null === static::$_allTranslations) { - - - $cacheKey = static::_getCacheKey(); - if ($force) { - Cache::forget($cacheKey); - } - static::$_allTranslations = Cache::remember($cacheKey, 3600, function () { - start_measure('Get all ' . static::$_name . ' translations !'); - $t = static::find(1); - try { - $json = json_decode($t->getRawOriginal('content_translatable'), true, 512, JSON_THROW_ON_ERROR); - } catch (\Exception $e) { - $json = []; - } - - $res = []; - foreach ($json as $code => $tr) { - $res[$code] = []; - foreach ($tr as $k => $v) { - $res[$code][$k] = ['str' => static::keyToStr($k), 'translation' => $v]; - } - } - stop_measure('Get all ' . static::$_name . ' translations !'); - return $res; - - }); - } - return static::$_allTranslations; - } - - public static function getCompiledTranslations() - { - $raw = static::getAllTranslations(); - $res = []; - foreach ($raw as $code => $data) { - $res[$code] = []; - foreach ($data as $k => $v) { - if (is_string($v)) { - $res[$code][$k] = $v; - } else { - $res[$code][$v['str']] = $v['translation']; - } - } - } - return $res; - } - - /** - * @throws \JsonException - */ - public static function updateTranslation($locale, $translations) - { - /** @var static $t */ - $t = static::find(1); - $json = json_decode($t->getRawOriginal('content_translatable'), true, 512, JSON_THROW_ON_ERROR); - foreach ($translations as $k => $v) { - $json[$locale][$k] = $v; - } - $t->setRawAttributes(['content_translatable' => json_encode($json, JSON_THROW_ON_ERROR)]); - $t->save(); - } - - public function onSaved(): bool - { - Cache::forget(static::_getCacheKey()); - return parent::onSaved(); - } - - /** - * @param string $locale - * @return array[]|null - */ - public static function getLocaleTranslations($locale, $compiled = false) - { - $all = static::getAllTranslations(false); - $res = $all[$locale] ?? []; - if (!$compiled) { - return $res; - } - $comp = []; - foreach ($res as $k => $v) { - if (is_string($v)) { - $comp[$k] = $v; - } else { - $comp[$v['str']] = $v['translation']; - } - } - return $comp; - } public function getPaths() { diff --git a/app/Models/ToolboxTranslate.php b/app/Models/ToolboxTranslate.php index 6052edde1..55eba677a 100644 --- a/app/Models/ToolboxTranslate.php +++ b/app/Models/ToolboxTranslate.php @@ -3,10 +3,9 @@ namespace App\Models; -use App\Models\Traits\FluidbookPlayerBranches; -use Cubist\Backpack\Magic\Models\Translate; +use App\Models\Base\ToolboxBaseTranslate; -class ToolboxTranslate extends Translate +class ToolboxTranslate extends ToolboxBaseTranslate { protected $table = 'toolbox_translate'; @@ -15,6 +14,7 @@ class ToolboxTranslate extends Translate 'plural' => 'traductions', 'oneinstance' => true]; + protected static $_name=''; protected $_enableTrackEmptyValues = true;