From: Vincent Vanwaelscappel Date: Mon, 3 Jul 2023 16:32:25 +0000 (+0200) Subject: wip #6050 @0.75 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=8824b251f88c76e5492a38539a301e35641fa77d;p=cubist_cms-back.git wip #6050 @0.75 --- diff --git a/src/app/Magic/Models/Translate.php b/src/app/Magic/Models/Translate.php index 55df37c..f0f8440 100644 --- a/src/app/Magic/Models/Translate.php +++ b/src/app/Magic/Models/Translate.php @@ -3,53 +3,127 @@ namespace Cubist\Backpack\app\Magic\Models; -class Translate extends CubistMagicTranslatableModel -{ +use Cubist\Backpack\app\Magic\Fields\Hidden; +use Cubist\Backpack\app\Magic\Fields\Textarea; +use Cubist\Backpack\Facades\App; + +class Translate extends CubistMagicTranslatableModel { protected $table = 'cubist_translate'; protected $_options = ['name' => 'translate', - 'singular' => 'traduction', - 'plural' => 'traductions', - 'oneinstance' => true]; + 'singular' => 'traduction', + 'plural' => 'traductions', + 'oneinstance' => true]; + + public function getExtensions() { + return ['php', 'phtml']; + } - public function setFields() - { + public function getPaths() { + return ['resources/views', 'app', 'vendor/cubist/cms-back/src/app', 'vendor/cubist/cms-back/src/resources']; + } + + public function setFields() { parent::setFields(); - $paths = ['resources/views','app', 'vendor/cubist/cms-back/src/app/Magic/Menu']; + $paths = $this->getPaths(); $translate = new \Cubist\Locale\Translate(); - foreach ($paths as $path) { - $translate->addPath(base_path() . '/' . $path); + $translate->setExtensions($this->getExtensions()); + foreach ($paths as $sectionLabel => $path) { + $translate->addPath(base_path() . '/' . $path, $sectionLabel); } $translate->parseFiles(); + $this->addField(['name' => 'id', 'type' => Hidden::class, 'label' => '', 'value' => 1, 'database_type' => 'none']); + $this->addField(['name' => 'locale', 'type' => Hidden::class, 'label' => '', 'database_type' => 'none', 'value' => request()->get("locale", App::getLocale())]); + $this->addField(['name' => 'k', - 'label' => '-', - 'type' => 'Hidden', - 'translatable' => true, - 'store_in' => 'content', - 'default' => '-', - 'value' => '-', - 'fake' => true]); - - foreach ($translate->getStringToTranslate() as $string) { - $key = 't_' . base64_encode($string); - - $this->addField(['name' => $key, - 'label' => e($string), - 'type' => 'Textarea', - 'translatable' => true, - 'store_in' => 'content', - 'fake' => true]); + 'label' => '-', + 'type' => 'Hidden', + 'translatable' => true, + 'store_in' => 'content', + 'default' => '-', + 'value' => '-', + 'fake' => true]); + + $seenStrings = []; + + $currentSection = '-'; + $sections = [$currentSection => []]; + + foreach ($translate->getStringToTranslate() as $sectionLabel => $strings) { + $toTranslate = []; + foreach ($strings as $string) { + if (isset($seenStrings[$string])) { + continue; + } + $toTranslate[] = $string; + $seenStrings[$string] = true; + } + if (!count($toTranslate)) { + continue; + } + if (!is_numeric($sectionLabel)) { + if (!$sectionLabel || $sectionLabel == '0') { + $sectionLabel = '-'; + } + $currentSection = $sectionLabel; + if (!isset($sections[$currentSection])) { + $sections[$currentSection] = []; + } + } + + foreach ($toTranslate as $string) { + if (str_starts_with($string, '!!')) { + $currentSection = ltrim($string, '! '); + if (!isset($sections[$currentSection])) { + $sections[$currentSection] = []; + } + continue; + } + $key = self::_encodeKey($string); + + if (is_string($string)) { + $label = e($string); + } else { + $label = '??'; + } + + $sections[$currentSection][] = ['name' => $key, + 'label' => $label, + 'type' => Textarea::class, + 'rows' => 1, + 'translatable' => true, + 'store_in' => 'content', + 'fake' => true]; + } + } + + $i = 0; + foreach ($sections as $section => $fields) { + if (!count($fields)) { + continue; + } + foreach ($fields as $field) { + if (!$field) { + continue; + } + $this->addField($field); + } + $i++; } } + protected static function _encodeKey($string) { + return 't_' . base64_encode($string); + } + + /** * @param \Cubist\Backpack\app\Magic\Controllers\CubistMagicController $controller * @param \Cubist\Backpack\app\Magic\Requests\CubistMagicUpdateRequest $request */ - public function onBeforeUpdate($controller, $request) - { + public function onBeforeUpdate($controller, $request) { $this->saveLanguageFile($controller, $request); parent::onBeforeUpdate($controller, $request); // TODO: Change the autogenerated stub } @@ -58,8 +132,7 @@ class Translate extends CubistMagicTranslatableModel * @param \Cubist\Backpack\app\Magic\Controllers\CubistMagicController $controller * @param \Cubist\Backpack\app\Magic\Requests\CubistMagicUpdateRequest $request */ - public function saveLanguageFile($controller, $request) - { + public function saveLanguageFile($controller, $request) { $exclude = ['_token', '_method', 'http_referrer', 'locale', 'slug', 'id', 'k', 'save_action']; $translations = [];