From: Vincent Vanwaelscappel Date: Wed, 6 Sep 2023 07:13:09 +0000 (+0200) Subject: wip #6248 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=a1d66a91fe3c9ba904d63de9945f6f0a6c8ca184;p=fluidbook-toolbox.git wip #6248 --- diff --git a/app/Models/ElearningTranslate.php b/app/Models/ElearningTranslate.php new file mode 100644 index 000000000..42b3a727c --- /dev/null +++ b/app/Models/ElearningTranslate.php @@ -0,0 +1,154 @@ + 'elearning-translate', + 'singular' => 'traduction', + 'plural' => 'traductions', + 'oneinstance' => true]; + + protected $_operations = [ExcelExportOperation::class, ExcelImportOperation::class]; + + + public function __construct(array $attributes = []) + { + PHP::neverStop(); + + $this->_availableLocales = \Cubist\Locale\Locale::getList(App::getLocale()); + $this->addEditAction('fluidbook_translate.excel_export'); + $this->addEditAction('fluidbook_translate.excel_import'); + + parent::__construct($attributes); + } + + public function setFields() + { + //$this->addField(['name' => 'nsis', 'type' => NSISLocale::class, 'label' => __('Langue de l\'installeur') . ' (' . __('Version offline windows') . ')', 'translatable' => true, 'default' => 'English']); + parent::setFields(); + } + + public function getPaths() + { + $res = []; + foreach (self::getActiveBranches() as $branch) { + $res['Player - git:' . $branch] = 'resources/fluidbookpublication/player/branches/' . $branch . '/js'; + $res['Player - local:' . $branch] = 'resources/fluidbookpublication/player/local/' . $branch . '/js'; + } + //$res[__('Compilateur')]='resources/compiler'; + //$res[__('Application')]=''resources/html5app'; + return $res; + } + + public function getExtensions() + { + return array_merge(parent::getExtensions(), ['js']); + } + + protected function _getLanguageFile($locale) + { + return self::getLanguageFile($locale); + } + + public static function getLanguageFile($locale) + { + return resource_path('lang/elearning.' . $locale . '.json'); + } + + public static function getAllElearningTranslations($force = false) + { + if (null === self::$_allTranslations) { + $cacheKey = 'all_elearning_translations'; + if ($force) { + Cache::forget($cacheKey); + } + self::$_allTranslations = Cache::remember($cacheKey, 3600, function () { + start_measure('Get all elearning translations !'); + $t = ElearningTranslate::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' => self::keyToStr($k), 'translation' => $v]; + } + } + stop_measure('Get all fluidbook translations !'); + return $res; + + }); + } + return self::$_allTranslations; + } + + public static function getCompiledTranslations() + { + $raw = self::getAllElearningTranslations(); + $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 updateElearningTranslation($locale, $translations) + { + /** @var FluidbookTranslate $t */ + $t = self::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(); + } + + /** + * @param string $locale + * @return array[]|null + * @throws \JsonException + */ + public static function getElearningTranslation($locale) + { + $all = self::getAllFluidbookTranslations(); + $res = $all[$locale] ?? null; + return $res; + } + + +} diff --git a/app/Models/FluidbookTranslate.php b/app/Models/FluidbookTranslate.php index 0f1ed68f9..737029323 100644 --- a/app/Models/FluidbookTranslate.php +++ b/app/Models/FluidbookTranslate.php @@ -4,7 +4,6 @@ namespace App\Models; use App\Fields\NSISLocale; -use App\Http\Controllers\Admin\Base\FluidbookTranslateController; use App\Http\Controllers\Admin\Operations\FluidbookTranslate\ExcelExportOperation; use App\Http\Controllers\Admin\Operations\FluidbookTranslate\ExcelImportOperation; use App\Models\Traits\FluidbookPlayerBranches; @@ -157,27 +156,4 @@ class FluidbookTranslate extends Translate return $res; } - /** - * @param $key string - * @return string - */ - public static function keyToStr($key) - { - if (self::isKey($key)) { - return base64_decode(substr($key, 2)); - } - return $key; - } - - /** - * @param $key string - * @return bool - */ - public static function isKey($key) - { - if (!$key) { - return false; - } - return (bool)preg_match("/^t_[a-zA-Z0-9\/\+]*={0,2}$/", $key); - } }