From 0b6f8ad59cf7aff2bccbd9bf7fe6ce8f4e671c51 Mon Sep 17 00:00:00 2001 From: nicolocarpignoli Date: Thu, 16 May 2024 11:11:48 +0200 Subject: [PATCH] feat: translate tags --- src/Controller/Component/TagsComponent.php | 30 ++++++++++++++++++++-- src/Model/ObjectsLoader.php | 10 ++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/Controller/Component/TagsComponent.php b/src/Controller/Component/TagsComponent.php index 4b149c7..732164e 100644 --- a/src/Controller/Component/TagsComponent.php +++ b/src/Controller/Component/TagsComponent.php @@ -5,6 +5,7 @@ use BEdita\Core\Model\Entity\Tag; use BEdita\Core\Model\Table\TagsTable; +use BEdita\I18n\Core\I18nTrait; use Cake\Collection\CollectionInterface; use Cake\Controller\Component; use Cake\Database\Expression\QueryExpression; @@ -19,6 +20,7 @@ */ class TagsComponent extends Component { + use I18nTrait; use LocatorAwareTrait; /** @@ -50,12 +52,16 @@ public function initialize(array $config): void */ public function load(): Query { + $lang ??= $this->getLang(); + return $this->Tags->find() ->where([$this->Tags->aliasField('enabled') => true]) ->order([$this->Tags->aliasField('name')]) - ->formatResults(function (CollectionInterface $results): CollectionInterface { - return $results->map(function (Tag $tag): Tag { + ->formatResults(function (CollectionInterface $results) use ($lang): CollectionInterface { + return $results->map(function (Tag $tag) use ($lang): Tag { $tag->set('slug', Text::slug($tag->name)); + $tag = $this->dangerouslyTranslateLabel($tag, $lang); + $tag->clean(); return $tag; @@ -63,6 +69,26 @@ public function load(): Query }); } + /** + * Dangerous processor to set label to its translation. + * + * **WARNING**: do NOT save entities that have been processed by this processor. + * + * @param \BEdita\Core\Model\Entity\Tag $tag Tag entity to process. + * @param string|null $lang Language code to use. + * @return \BEdita\Core\Model\Entity\Tag + */ + protected function dangerouslyTranslateLabel(Tag $tag, string|null $lang = null): Tag + { + if ($lang === null || !array_key_exists($lang, $tag->get('labels'))) { + return $tag; + } + + $tag->label = $tag->get('labels')[$lang]; + + return $tag; + } + /** * Build tags subquery for filtering. * diff --git a/src/Model/ObjectsLoader.php b/src/Model/ObjectsLoader.php index 4f3800c..baeb8a4 100644 --- a/src/Model/ObjectsLoader.php +++ b/src/Model/ObjectsLoader.php @@ -449,6 +449,16 @@ protected function dangerouslyTranslateFields(ObjectEntity $object, string|null unset($category); } + if (!empty($object->get('tags'))) { + foreach ($object->get('tags') as &$tag) { + /** @type \BEdita\Core\Model\Entity\Tag $tag */ + if (array_key_exists($lang, $tag->get('labels'))) { + $tag->label = $tag->get('labels')[$lang]; + } + } + unset($tag); + } + /** @var \BEdita\Core\Model\Entity\Translation|null $requestedTranslation */ $requestedTranslation = collection($object->translations ?? []) ->filter(fn (Translation $tr): bool => $tr->lang === $lang)