Skip to content

Commit

Permalink
chore(TranslationAware): Try to get translation resolved from LocaleS…
Browse files Browse the repository at this point in the history
…ubscriber before
  • Loading branch information
ambroisemaupate committed Feb 13, 2024
1 parent 9e70e04 commit 18edbeb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 18edbeb

Please sign in to comment.