]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6248 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 6 Sep 2023 13:22:06 +0000 (15:22 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 6 Sep 2023 13:22:06 +0000 (15:22 +0200)
app/Elearning/QuizCompiler.php
app/Elearning/QuizCompiler/L10N.php [new file with mode: 0644]
app/Fields/ContentLocale.php [new file with mode: 0644]
app/Fields/ElearningLocale.php [new file with mode: 0644]
app/Fields/FluidbookLocale.php
app/Models/Base/ToolboxContentTranslate.php
app/Models/ElearningTranslate.php
app/Models/FluidbookTranslate.php
app/Models/Quiz.php

index 7b9f35334837ce3b8e0eec2bd08fff818c5a5ac2..4a229b1d3f84845a01a09ca67178644e9b135f35 100644 (file)
@@ -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 (file)
index 0000000..0f78ce0
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+
+namespace App\Elearning\QuizCompiler;
+
+use App\Models\ElearningTranslate;
+
+trait L10N
+{
+    protected function writeL10n()
+    {
+        $this->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 (file)
index 0000000..c862abc
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+
+namespace App\Fields;
+
+use Cubist\Backpack\CubistBackpackServiceProvider;
+use Cubist\Backpack\Magic\Fields\Locale;
+use Cubist\Util\Files\Files;
+
+class ContentLocale extends Locale
+{
+    protected $_columnType = 'fromfield';
+    protected $_columnViewNamespace = CubistBackpackServiceProvider::NAMESPACE . '::columns';
+
+    protected $_name = 'content';
+
+    public function __construct($attributes)
+    {
+        $list = Files::getDirectoryIterator(resource_path('lang'));
+        $this->_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 (file)
index 0000000..80f3d5c
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+
+
+namespace App\Fields;
+
+class ElearningLocale extends ContentLocale
+{
+
+    protected $_name = 'elearning';
+
+    protected function getAdditionalLocales()
+    {
+        return ['en','fr','de','es','hr','it','nl','pl','pt','sv','tr','cs','ja','no','ru','zh','ko','hi','ro'];
+    }
+
+}
index 69a88ce444f7c3bf50f5adc2ee43fa6bdfe4605f..d8679d2adf18abca49534d5b9dfaab1b6234ce38 100644 (file)
@@ -3,30 +3,9 @@
 
 namespace App\Fields;
 
-
-use Cubist\Backpack\CubistBackpackServiceProvider;
-use Cubist\Backpack\Magic\Fields\Locale;
-use Cubist\Util\Files\Files;
-
-class FluidbookLocale extends Locale
+class FluidbookLocale extends ContentLocale
 {
-    protected $_columnType = 'fromfield';
-    protected $_columnViewNamespace = CubistBackpackServiceProvider::NAMESPACE . '::columns';
 
-    public function __construct($attributes)
-    {
-        $list = Files::getDirectoryIterator(resource_path('lang'));
-        $this->_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);
-    }
 }
index 5a0f9e0c67683eccb5e1d5b5a2bd0e0bebe254cc..cc6ac846d25f13bace99d852b31cc0f8adf18b71 100644 (file)
@@ -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()
index 041d997f369f9e10b50641b1c728ef608dfcd56d..b4285a782b6e2abd11a5eb173ebfa787e411585d 100644 (file)
@@ -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;
 
index 22c153a708d716ef4ac9a81dfa6ebeb837edf414..d03eaaa45dcff9ba85e8517d7f869a878991b5ad 100644 (file)
@@ -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;
index 57c4aee65753cb83baafa34e76ddfb7edfc9c52b..0843de5a59896c0ecc4b76ce9341a8d0a700017b 100644 (file)
@@ -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' => '<i class="fa fa-language"></i>']);
 
-        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;
+    }
 }