From 18edbeb5bcff9d72a7137e115b450cf21be31f9d Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Tue, 13 Feb 2024 20:25:24 +0100 Subject: [PATCH] chore(TranslationAware): Try to get translation resolved from LocaleSubscriber before --- .../Normalizer/TranslationAwareNormalizer.php | 3 ++ .../TranslationAwareContextBuilder.php | 50 ++++++++++++------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/lib/RoadizCoreBundle/src/Serializer/Normalizer/TranslationAwareNormalizer.php b/lib/RoadizCoreBundle/src/Serializer/Normalizer/TranslationAwareNormalizer.php index c6beaba0..eb1c3df2 100644 --- a/lib/RoadizCoreBundle/src/Serializer/Normalizer/TranslationAwareNormalizer.php +++ b/lib/RoadizCoreBundle/src/Serializer/Normalizer/TranslationAwareNormalizer.php @@ -82,6 +82,9 @@ private function getTranslationFromRequest(): ?TranslationInterface ->findDefault(); } + /* + * Try to get translation resolved from LocaleSubscriber before + */ $requestTranslation = $request->attributes->get('_translation'); if ($requestTranslation instanceof TranslationInterface) { return $requestTranslation; diff --git a/lib/RoadizCoreBundle/src/Serializer/TranslationAwareContextBuilder.php b/lib/RoadizCoreBundle/src/Serializer/TranslationAwareContextBuilder.php index edf63081..2af808c8 100644 --- a/lib/RoadizCoreBundle/src/Serializer/TranslationAwareContextBuilder.php +++ b/lib/RoadizCoreBundle/src/Serializer/TranslationAwareContextBuilder.php @@ -33,26 +33,38 @@ public function createFromRequest(Request $request, bool $normalization, array $ { $context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes); - if (!isset($context['translation']) || !($context['translation'] instanceof TranslationInterface)) { - /** @var TranslationRepository $repository */ - $repository = $this->managerRegistry - ->getRepository(TranslationInterface::class); - $locale = $request->query->get('_locale', $request->getLocale()); - - if (!\is_string($locale)) { - return $context; - } - - if ($this->previewResolver->isPreview()) { - $translation = $repository->findOneByLocaleOrOverrideLocale($locale); - } else { - $translation = $repository->findOneAvailableByLocaleOrOverrideLocale($locale); - } - - if ($translation instanceof TranslationInterface) { - $context['translation'] = $translation; - } + if (isset($context['translation']) && $context['translation'] instanceof TranslationInterface) { + return $context; } + + /* + * Try to get translation resolved from LocaleSubscriber before + */ + $requestTranslation = $request->attributes->get('_translation'); + if ($requestTranslation instanceof TranslationInterface) { + $context['translation'] = $requestTranslation; + return $context; + } + + /** @var TranslationRepository $repository */ + $repository = $this->managerRegistry + ->getRepository(TranslationInterface::class); + $locale = $request->query->get('_locale', $request->getLocale()); + + if (!\is_string($locale)) { + return $context; + } + + if ($this->previewResolver->isPreview()) { + $translation = $repository->findOneByLocaleOrOverrideLocale($locale); + } else { + $translation = $repository->findOneAvailableByLocaleOrOverrideLocale($locale); + } + + if ($translation instanceof TranslationInterface) { + $context['translation'] = $translation; + } + return $context; } }