I'm creating a custom header in Magento 2.4.4, I want to include language switcher but not the default dropdown view.
I want to include link to the second language only.
So if I have 2 languages English & French, in English I want to show only French at the header so when users click on it; it should takes them to the translated page not the home page.
CodePudding user response:
Hi @Zee we can create a language switcher by using phtml file in design folder like this app/design/frontend/vendor/Module/Magento_Store/templates/switch/languages.phtml
<?php if (count($block->getStores())>1) : ?>
<?php $id = $block->getIdModifier() ? '-' . $block->getIdModifier() : '' ?>
<div data-ui-id="language-switcher" id="switcher-language<?= $block->escapeHtmlAttr($id) ?>">
<strong ><span><?= $block->escapeHtml(__('Language')) ?></span></strong>
<?php
$currentWebsite = $helper->getCurrentWebsiteName();
?>
<?php if($currentWebsite == 'Qatar'){ ?>
<div >
<div
id="switcher-language-trigger<?= $block->escapeHtmlAttr($id) ?>">
<strong >
<?php foreach ($block->getStores() as $_lang) : ?>
<?php if ($_lang->getId() != $block->getCurrentStoreId()) : ?>
<strong >
<span>
<a href="<?= $block->escapeUrl($block->getViewModel()->getTargetStoreRedirectUrl($_lang)) ?>">
<?php if($_lang->getName() == "Arabic"){ ?>
<?= $block->escapeHtml("عربي") ?>
<?php }else{ ?>
<?= $block->escapeHtml($_lang->getName()) ?>
<?php } ?>
</a>
</span>
</strong>
<?php endif; ?>
<?php endforeach; ?>
</strong>
</div>
</div>
<?php }else{ ?>
<div >
<div
id="switcher-language-trigger<?= $block->escapeHtmlAttr($id) ?>"
data-mage-init='{"dropdown":{}}'
data-toggle="dropdown"
data-trigger-keypress-button="true">
<strong >
<span><?= $block->escapeHtml($block->getStoreName()) ?></span>
</strong>
</div>
<ul
data-target="dropdown">
<?php foreach ($block->getStores() as $_lang) : ?>
<?php if ($_lang->getId() != $block->getCurrentStoreId()) : ?>
<li >
<a href="<?= $block->escapeUrl($block->getViewModel()->getTargetStoreRedirectUrl($_lang)) ?>">
<?= $block->escapeHtml($_lang->getName()) ?>
</a>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div>
<?php } ?>
</div>
CodePudding user response:
You can use below code for archive your requirement
In the below answer I am assuming that you know how to get store data.
<?php
$currentStore = "English"; // THIS SHOULD BE DYNAMIC
$switchStore = "French"; // THIS SHOULD BE DYNAMIC
$URL = 'https://demo.com/product.html?___store='. $currentStore .'&___from_store='.$switchStore
?>
<a href="#" onclick="window.location.href='<?= $URL ?>'"> <?= __('STORE_NAME') ?></a>
Hope this will useful for you.
Thank you.