]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6248 @0.25
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 6 Sep 2023 07:28:25 +0000 (09:28 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 6 Sep 2023 07:28:25 +0000 (09:28 +0200)
app/Models/Base/ToolboxContentTranslate.php [new file with mode: 0644]
app/Models/ElearningTranslate.php
app/Models/FluidbookTranslate.php

diff --git a/app/Models/Base/ToolboxContentTranslate.php b/app/Models/Base/ToolboxContentTranslate.php
new file mode 100644 (file)
index 0000000..70495f7
--- /dev/null
@@ -0,0 +1,109 @@
+<?php
+
+namespace App\Models\Base;
+
+use App\Models\ElearningTranslate;
+use App\Models\FluidbookTranslate;
+use Cubist\Backpack\Magic\Models\Translate;
+use Illuminate\Support\Facades\Cache;
+
+class ToolboxContentTranslate extends Translate
+{
+
+    protected static $_allTranslations = null;
+
+    protected static $_name = 'content';
+
+
+    protected function _getLanguageFile($locale)
+    {
+        return self::getLanguageFile($locale);
+    }
+
+    public static function getLanguageFile($locale)
+    {
+        return resource_path('lang/' . static::$_name . '.' . $locale . '.json');
+    }
+
+    public function getExtensions()
+    {
+        return array_merge(parent::getExtensions(), ['js']);
+    }
+
+    public static function getAllTranslations($force = false)
+    {
+        if (null === static::$_allTranslations) {
+            $cacheKey = 'all_' . static::$_name . '_translations';
+            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 self::$_allTranslations;
+    }
+
+    public static function getCompiledTranslations()
+    {
+        $raw = self::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();
+    }
+
+    /**
+     * @param string $locale
+     * @return array[]|null
+     * @throws \JsonException
+     */
+    public static function getTranslation($locale)
+    {
+        $all = static::getAllTranslations();
+        $res = $all[$locale] ?? null;
+        return $res;
+    }
+
+}
index 42b3a727cfedf0cbf7746e19f2ed1912a3634e12..6a5655aabd6ef860a0fbb2ada00a58870d57cbf4 100644 (file)
@@ -3,22 +3,21 @@
 
 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;
+use App\Models\Base\ToolboxContentTranslate;
+use App\Models\Traits\QuizBranches;
 use Cubist\Backpack\Facades\App;
-use Cubist\Backpack\Magic\Models\Translate;
 use Cubist\Util\PHP;
-use Illuminate\Support\Facades\Cache;
 
 // __('!!Traduction des contenus e-learning')
-class ElearningTranslate extends Translate{
+class ElearningTranslate extends ToolboxContentTranslate
+{
 
-    protected $table = 'elearning_translate';
+    use QuizBranches;
 
-    protected static $_allTranslations = null;
+    protected $table = 'elearning_translate';
+    protected static $_name='elearning';
 
     protected $_enableRevisions = false;
 
@@ -35,120 +34,22 @@ class ElearningTranslate extends Translate{
         PHP::neverStop();
 
         $this->_availableLocales = \Cubist\Locale\Locale::getList(App::getLocale());
-        $this->addEditAction('fluidbook_translate.excel_export');
-        $this->addEditAction('fluidbook_translate.excel_import');
+        $this->addEditAction('elearning_translate.excel_export');
+        $this->addEditAction('elearning_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['Player - git:' . $branch] = 'resources/quiz/player/branches/' . $branch . '/js';
+            $res['Player - local:' . $branch] = 'resources/quiz/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;
-    }
-
-
 }
index 7370293234df469ea501e9e5002dc87a921f0b8c..ea8bde0f7505ba804294af0e9705a7d74abeb131 100644 (file)
@@ -6,6 +6,7 @@ namespace App\Models;
 use App\Fields\NSISLocale;
 use App\Http\Controllers\Admin\Operations\FluidbookTranslate\ExcelExportOperation;
 use App\Http\Controllers\Admin\Operations\FluidbookTranslate\ExcelImportOperation;
+use App\Models\Base\ToolboxContentTranslate;
 use App\Models\Traits\FluidbookPlayerBranches;
 use Cubist\Backpack\Facades\App;
 use Cubist\Backpack\Magic\Models\Translate;
@@ -13,11 +14,12 @@ use Cubist\Util\PHP;
 use Illuminate\Support\Facades\Cache;
 
 // __('!!Traduction des fluidbooks')
-class FluidbookTranslate extends Translate
+class FluidbookTranslate extends ToolboxContentTranslate
 {
     use FluidbookPlayerBranches;
 
     protected $table = 'fluidbook_translate';
+    protected static $_name = 'elearning';
 
     protected static $_allTranslations = null;
 
@@ -60,90 +62,12 @@ class FluidbookTranslate extends Translate
         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/fluidbook.' . $locale . '.json');
-    }
 
     public static function getAllFluidbookTranslations($force = false)
     {
-        if (null === self::$_allTranslations) {
-            $cacheKey = 'all_fluidbook_translations';
-            if ($force) {
-                Cache::forget($cacheKey);
-            }
-            self::$_allTranslations = Cache::remember($cacheKey, 3600, function () {
-                start_measure('Get all fluidbook translations !');
-                $t = FluidbookTranslate::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];
-                    }
-                }
-
-                $nsis = json_decode($t->getRawOriginal('nsis'), true, 512, JSON_THROW_ON_ERROR);
-                foreach ($nsis as $code => $v) {
-                    $res[$code]['nsis'] = $v;
-                }
-                stop_measure('Get all fluidbook translations !');
-                return $res;
-
-            });
-        }
-        return self::$_allTranslations;
-    }
-
-    public static function getCompiledTranslations()
-    {
-        $raw = self::getAllFluidbookTranslations();
-        $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 updateFluidbookTranslation($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();
+        return static::getAllTranslations($force);
     }
-
+    
     /**
      * @param string $locale
      * @return array[]|null
@@ -151,9 +75,7 @@ class FluidbookTranslate extends Translate
      */
     public static function getFluidbookTranslation($locale)
     {
-        $all = self::getAllFluidbookTranslations();
-        $res = $all[$locale] ?? null;
-        return $res;
+        return static::getTranslation($locale);
     }
 
 }