]> _ Git - fluidbook-toolbox.git/commitdiff
wip #4285 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 19 May 2022 13:11:31 +0000 (15:11 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 19 May 2022 13:11:31 +0000 (15:11 +0200)
app/Fields/FluidbookTranslatedString.php [new file with mode: 0644]
app/Models/FluidbookPublication.php
app/SubForms/FluidbookTranslationOverwrite.php [new file with mode: 0644]

diff --git a/app/Fields/FluidbookTranslatedString.php b/app/Fields/FluidbookTranslatedString.php
new file mode 100644 (file)
index 0000000..1f9e5d2
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+namespace App\Fields;
+
+use App\Models\FluidbookTranslate;
+use Cubist\Backpack\Magic\Fields\SelectFromArray;
+use Cubist\Util\Str;
+
+class FluidbookTranslatedString extends SelectFromArray
+{
+    protected static $__options = null;
+
+    /**
+     * @throws \JsonException
+     */
+    public function getOptions()
+    {
+        if (null === self::$__options) {
+            self::$__options = $this->_getOptions();
+        }
+        return self::$__options;
+    }
+
+    protected function _getOptions()
+    {
+        $locale = 'en';
+
+        $translations = FluidbookTranslate::getFluidbookTranslation($locale);
+        $res = [];
+        foreach ($translations as $k => $translation) {
+            if ($k === 'k' || $k === 'nsis') {
+                continue;
+            }
+            if (!$translation['translation']) {
+                $res[$translation['str']] = $translation['str'];
+            } else if (Str::lower($translation['translation']) == Str::lower($translation['str'])) {
+                $res[$translation['str']] = $translation['translation'];
+            } else {
+                $res[$translation['str']] = $translation['translation'] . ' // ' . $translation['str'];
+            }
+        }
+        return $res;
+    }
+}
index c33167ec2a514697edcdc5378c2172aa3cff3852..60fa304e44d307d8ca0f76a50e0e36d15316415c 100644 (file)
@@ -8,6 +8,8 @@ use App\Fields\FluidbookTitle;
 use App\Fields\User;
 use App\Models\Base\ToolboxModel;
 use App\Models\Traits\PublicationSettings;
+use App\SubForms\FluidbookTranslationOverwrite;
+use Cubist\Backpack\Magic\Fields\BunchOfFieldsMultiple;
 use Cubist\Backpack\Magic\Fields\Code;
 use Cubist\Backpack\Magic\Fields\Datetime;
 use Cubist\Backpack\Magic\Fields\FormBigSection;
@@ -60,8 +62,10 @@ class FluidbookPublication extends ToolboxModel
         $this->addField('theme', \App\Fields\FluidbookTheme::class);
 
         $this->addField('section_locale', FormBigSection::class, __('Langue'));
-        $this->addField('locale', FluidbookLocale::class, 'Langue', ['default' => 'fr', 'allows_null' => false, 'filter' => true, 'filter_label' => 'Langue', 'column' => true, 'column_label' => '<i class="la la-language"></i>']);
-        $this->addField('translations', Code::class, 'Traductions', ['language' => 'javascript']);
+        $this->addField('locale', FluidbookLocale::class, __('Langue principale'), ['default' => 'fr', 'allows_null' => false, 'filter' => true, 'filter_label' => 'Langue', 'column' => true, 'column_label' => '<i class="la la-language"></i>']);
+        $this->addField('translations', Code::class, __('Traductions'));
+        $this->addField('translations_1', KeyValueBunchOfFieldsMultiple::class, __('Traductions personnalisées'), ['bunch' => FluidbookTranslationOverwrite::class, 'add_label' => __('Nouvelle traduction'),]);
+
 
         $this->addField('section_chapters', FormBigSection::class, __('Sommaire'));
         $this->addField('chapters', Textarea::class, __('Sommaire'));
diff --git a/app/SubForms/FluidbookTranslationOverwrite.php b/app/SubForms/FluidbookTranslationOverwrite.php
new file mode 100644 (file)
index 0000000..4a366f9
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+namespace App\SubForms;
+
+use App\Fields\FluidbookTranslatedString;
+use Cubist\Backpack\Magic\Fields\Text;
+use Cubist\Backpack\Magic\SubForm;
+
+class FluidbookTranslationOverwrite extends SubForm
+{
+    /**
+     * @var string
+     */
+    protected $_locale = 'en';
+
+    /**
+     * @return string
+     */
+    public function getLocale(): string
+    {
+        return $this->_locale;
+    }
+
+    /**
+     * @param string $locale
+     */
+    public function setLocale(string $locale): void
+    {
+        $this->_locale = $locale;
+    }
+
+    public function init()
+    {
+        parent::init();
+        $this->addField('key', FluidbookTranslatedString::class, __('Texte original'), ['locale' => $this->getLocale()]);
+        $this->addField('value', Text::class, __('Nouvelle traduction'));
+    }
+}