Skip to content

Commit

Permalink
Merge pull request #2261 from BlackbitDevs/language-switcher
Browse files Browse the repository at this point in the history
Adjusted locale switcher for internal shop pages without translations
  • Loading branch information
dpfaffenbauer committed May 4, 2023
2 parents ca4aa4d + e1ee76a commit 36af629
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ services:
- '@Pimcore\Model\Document\Service'
- '@CoreShop\Component\Core\Context\ShopperContextInterface'
- '@Symfony\Component\HttpFoundation\RequestStack'
- '@router'
tags:
- { name: twig.extension }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
use Pimcore\Model\DataObject\Data\UrlSlug;
use Pimcore\Model\Document;
use Pimcore\Model\Site;
use Pimcore\Model\Staticroute;
use Pimcore\Tool;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

Expand All @@ -35,6 +38,7 @@ public function __construct(
private Document\Service $documentService,
private ShopperContextInterface $shopperContext,
private RequestStack $requestStack,
private RouterInterface $router,
) {
}

Expand Down Expand Up @@ -84,16 +88,31 @@ public function getLocalizedLinks(Document $document): array
continue;
}

if (isset($translations[$language])) {
$localizedDocument = Document::getById($translations[$language]);
$link = '';
if ($this->getMainRequest()->attributes->get('pimcore_request_source') === 'staticroute') {
$route = $this->getMainRequest()->attributes->get('_route');
$staticRoute = Staticroute::getByName($route);
$params = [];
if ( str_contains($staticRoute->getVariables(), '_locale') ) {
$params = ['_locale' => $language];
}
$link = $this->router->generate($route, $params);
} else {
$localizedDocument = Document::getByPath($target);
if ( isset($translations[$language]) ) {
$localizedDocument = Document::getById($translations[$language]);
} else {
$localizedDocument = Document::getByPath($target);
}

if ( $localizedDocument instanceof Document && $localizedDocument->getPublished() ) {
$link = $localizedDocument->getFullPath();
}
}

if ($localizedDocument instanceof Document && $localizedDocument->getPublished()) {
if (!empty($link)) {
$links[] = [
'language' => $language,
'target' => $localizedDocument->getFullPath(),
'target' => $link,
'displayLanguage' => \Locale::getDisplayLanguage($language, $language),
];
}
Expand Down

0 comments on commit 36af629

Please sign in to comment.