From: Vincent Vanwaelscappel Date: Wed, 6 Sep 2023 13:22:06 +0000 (+0200) Subject: wip #6248 @0.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=e9bf374ca3a416f37f746e1af54b158526c6ee8f;p=fluidbook-toolbox.git wip #6248 @0.5 --- diff --git a/app/Elearning/QuizCompiler.php b/app/Elearning/QuizCompiler.php index 7b9f35334..4a229b1d3 100644 --- a/app/Elearning/QuizCompiler.php +++ b/app/Elearning/QuizCompiler.php @@ -3,6 +3,7 @@ namespace App\Elearning; use App\Elearning\QuizCompiler\Animations; +use App\Elearning\QuizCompiler\L10N; use App\Fields\FluidbookFont; use App\Jobs\Base; use App\Models\Quiz; @@ -19,6 +20,7 @@ class QuizCompiler extends Base { use Animations; + use L10N; /** * @var Quiz @@ -60,6 +62,7 @@ class QuizCompiler extends Base // Add data related to the current quiz in the "to compile" files $this->writeStyles(); + $this->writeL10n(); $this->writeData(); // Run the compiler $this->runWebpack(); @@ -69,9 +72,11 @@ class QuizCompiler extends Base $vdir->copyDirectory($this->compilePath . '/dist/js', 'js'); $vdir->copyDirectory($this->compilePath . '/dist/assets', 'assets'); - - $blade = new Blade($this->_resourcesPath() . '/views', $this->_resourcesPath() . '/cache/'.md5(rand(100000,10000000)).'/'); - $html = $blade->make('index', ['data' => $this->data, 'quiz' => $this->quiz, 'theme' => $this->theme])->render(); + $l10n = $this->data->l10n; + $blade = new Blade($this->_resourcesPath() . '/views', $this->_resourcesPath() . '/cache/' . md5(rand(100000, 10000000)) . '/'); + $html = $blade->make('index', ['data' => $this->data, 'quiz' => $this->quiz, 'theme' => $this->theme, '__' => function ($str) use ($l10n) { + return $l10n[$str] ?? $str; + }])->render(); $vdir->file_put_contents('index.html', $html); diff --git a/app/Elearning/QuizCompiler/L10N.php b/app/Elearning/QuizCompiler/L10N.php new file mode 100644 index 000000000..0f78ce061 --- /dev/null +++ b/app/Elearning/QuizCompiler/L10N.php @@ -0,0 +1,19 @@ +data->setRaw('l10n', $this->getTranslations()); + } + + protected function getTranslations() + { + $res= ElearningTranslate::getLocaleTranslations($this->data->translation, true); + return $res; + } +} diff --git a/app/Fields/ContentLocale.php b/app/Fields/ContentLocale.php new file mode 100644 index 000000000..c862abc58 --- /dev/null +++ b/app/Fields/ContentLocale.php @@ -0,0 +1,42 @@ +_limitToLocales = []; + foreach ($list as $item) { + if (preg_match('/' . $this->_name . '.([a-z_A-Z]{2,5}).json/', $item->getFilename(), $matches)) { + $this->_limitToLocales[] = $matches[1]; + } + } + $this->_limitToLocales = array_unique(array_merge( + array_values($this->_limitToLocales), + array_values($this->getAdditionalLocales())) + ); + parent::__construct($attributes); + } + + + public function filterColumn($value) + { + return mb_strtoupper($value); + } + + protected function getAdditionalLocales() + { + return []; + } +} diff --git a/app/Fields/ElearningLocale.php b/app/Fields/ElearningLocale.php new file mode 100644 index 000000000..80f3d5cdf --- /dev/null +++ b/app/Fields/ElearningLocale.php @@ -0,0 +1,16 @@ +_limitToLocales = []; - foreach ($list as $item) { - if (preg_match('/fluidbook.([a-z_A-Z]{2,5}).json/', $item->getFilename(), $matches)) { - $this->_limitToLocales[] = $matches[1]; - } - } - parent::__construct($attributes); - } + protected $_name='fluidbook'; - public function filterColumn($value) - { - return mb_strtoupper($value); - } } diff --git a/app/Models/Base/ToolboxContentTranslate.php b/app/Models/Base/ToolboxContentTranslate.php index 5a0f9e0c6..cc6ac846d 100644 --- a/app/Models/Base/ToolboxContentTranslate.php +++ b/app/Models/Base/ToolboxContentTranslate.php @@ -36,7 +36,6 @@ class ToolboxContentTranslate extends Translate return static::$_name; } - protected function _getLanguageFile($locale) { return static::getLanguageFile($locale); @@ -52,10 +51,15 @@ class ToolboxContentTranslate extends Translate return parent::getExtensions() + ['js']; } + protected static function _getCacheKey() + { + return 'all_' . static::$_name . '_translations'; + } + public static function getAllTranslations($force = true) { if (null === static::$_allTranslations) { - $cacheKey = 'all_' . static::$_name . '_translations'; + $cacheKey = static::_getCacheKey(); if ($force) { Cache::forget($cacheKey); } @@ -116,14 +120,32 @@ class ToolboxContentTranslate extends Translate $t->save(); } + public function onSaved(): bool + { + Cache::forget(static::_getCacheKey()); + return parent::onSaved(); + } + /** * @param string $locale * @return array[]|null */ - public static function getLocaleTranslations($locale) + public static function getLocaleTranslations($locale, $compiled = false) { $all = static::getAllTranslations(false); - return $all[$locale] ?? []; + $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/ElearningTranslate.php b/app/Models/ElearningTranslate.php index 041d997f3..b4285a782 100644 --- a/app/Models/ElearningTranslate.php +++ b/app/Models/ElearningTranslate.php @@ -18,7 +18,7 @@ class ElearningTranslate extends ToolboxContentTranslate protected $table = 'elearning_translate'; protected static string $_name = 'elearning'; - protected static $_basePath='resources/quiz/player'; + protected static $_basePath = 'resources/quiz/player'; protected $_enableRevisions = false; diff --git a/app/Models/FluidbookTranslate.php b/app/Models/FluidbookTranslate.php index 22c153a70..d03eaaa45 100644 --- a/app/Models/FluidbookTranslate.php +++ b/app/Models/FluidbookTranslate.php @@ -19,7 +19,7 @@ class FluidbookTranslate extends ToolboxContentTranslate use FluidbookPlayerBranches; protected $table = 'fluidbook_translate'; - protected static string $_name = 'elearning'; + protected static string $_name = 'fluidbook'; protected static $_basePath = 'resources/fluidbookpublication/player'; protected static $_allTranslations = null; diff --git a/app/Models/Quiz.php b/app/Models/Quiz.php index 57c4aee65..0843de5a5 100644 --- a/app/Models/Quiz.php +++ b/app/Models/Quiz.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Elearning\QuizCompiler; +use App\Fields\ElearningLocale; use App\Fields\QuizDevelopmentVersion; use App\Fields\SCORMVersion; use App\Http\Controllers\Admin\Operations\ChangeownerOperation; @@ -67,14 +68,6 @@ class Quiz extends ToolboxModel ]; } - protected static function _getMessages() - { - return [ - 'defaultMessage' => __('Message affiché à la fin du quiz'), - 'passedMessage' => __('Message affiché lors de la réussite du quiz'), - 'failedMessage' => __('Message affiché lors de l\'échec du quiz'), - ]; - } protected static function _getImages() { @@ -138,22 +131,20 @@ class Quiz extends ToolboxModel 'database_default' => 'quiz', ]); - $this->addField('section_translation', FormSection::class, __('Langue et traductions')); + $this->addField('section_translation', FormSection::class, __('Langue')); $this->addField(['name' => 'translation', 'label' => __('Langue'), - 'type' => 'SelectFromModel', - 'optionsmodel' => QuizTranslation::class, - 'attribute' => 'locale', + 'type' => ElearningLocale::class, 'column' => true, 'column_label' => '']); - foreach (self::_getMessages() as $name => $label) { - $this->addField(['name' => $name, - 'label' => $label, - 'hint' => __('Laisser vide pour utiliser le message par défaut'), - 'type' => 'Text', - ]); - } +// foreach (self::_getMessages() as $name => $label) { +// $this->addField(['name' => $name, +// 'label' => $label, +// 'hint' => __('Laisser vide pour utiliser le message par défaut'), +// 'type' => 'Text', +// ]); +// } $this->addField('section_scorm', FormSection::class, __('SCORM')); @@ -326,23 +317,9 @@ class Quiz extends ToolboxModel } // Create data.js - $d['translations'] = []; - $tid = $data->get('translation', 1) ?? 1; - if ($tid === 'en') { - $tid = 1; - } - $translation = QuizTranslation::find($tid); - foreach (QuizTranslation::getTexts() as $text => $default) { - $d['translations'][$text] = $translation->getAttribute($text); - } - foreach (self::getMessages() as $name => $message) { - if (null === $d[$name] || !$d[$name]) { - $d[$name] = $translation->getAttribute($name); - } - } - + $locale = $data->get('translation', 'en') ?? 'en'; // Countries - $d['countriesList'] = \Cubist\Locale\Country::getList($translation->getAttribute('locale')); + $d['countriesList'] = \Cubist\Locale\Country::getList($locale); // Fix boolean $booleans = ['multiple' => false, 'required' => true, 'count_for_score' => true]; @@ -368,11 +345,6 @@ class Quiz extends ToolboxModel return $d; } - public static function getMessages() - { - return self::_getMessages(); - } - public static function getColors() { return self::_getColors(); @@ -425,4 +397,17 @@ class Quiz extends ToolboxModel { return QuizTheme::withoutGlobalScopes()->find($this->theme ?: 3); } + + public function onRetrieved(): bool + { + $map = ['', 'en', 'fr', 'de', 'es', 'hr', 'it', 'nl', 'pl', 'pt', + 'sv', 'tr', 'cs', 'ja', 'no', 'ru', 'zh', 'ko', 'hi', '', 'ro']; + + $res = parent::onRetrieved(); + if (is_numeric($this->translation) && isset($map[$this->translation])) { + $this->translation = $map[$this->translation]; + $this->setAttribute('translation', $this->translation); + } + return $res; + } }