Magento 2.4在页眉中添加自定义语言切换器



我正在Magento 2.4.4中创建一个自定义标头,我想包括语言切换器,但不包括默认的下拉视图。

我只想包含到第二语言的链接

所以,如果我有两种语言英语&法语,在英语中,我只想在标题中显示法语,这样当用户点击它时;它应该把他们带到翻译的页面,而不是主页。

我想要的屏幕截图

Hi@Zee我们可以在设计文件夹中使用phtml文件创建一个语言切换器,如下所示app/design/frontend/vendor/Module/Magento_Store/templates/switch/languages.html

<?php if (count($block->getStores())>1) : ?>
<?php $id = $block->getIdModifier() ? '-' . $block->getIdModifier() : '' ?>
<div class="switcher language switcher-language" data-ui-id="language-switcher" id="switcher-language<?= $block->escapeHtmlAttr($id) ?>">
<strong class="label switcher-label"><span><?= $block->escapeHtml(__('Language')) ?></span></strong>
<?php
$currentWebsite = $helper->getCurrentWebsiteName();
?>
<?php if($currentWebsite == 'Qatar'){ ?>
<div class="actions dropdown options switcher-options">
<div class="action toggle switcher-trigger"
id="switcher-language-trigger<?= $block->escapeHtmlAttr($id) ?>">
<strong class="view-<?= $block->escapeHtml($block->getCurrentStoreCode()) ?>">
<?php foreach ($block->getStores() as $_lang) : ?>
<?php if ($_lang->getId() != $block->getCurrentStoreId()) : ?>
<strong class="view-<?= $block->escapeHtml($block->getCurrentStoreCode()) ?>">
<span>
<a class="language-switcher-link" 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 class="actions dropdown options switcher-options">
<div class="action toggle switcher-trigger"
id="switcher-language-trigger<?= $block->escapeHtmlAttr($id) ?>"
data-mage-init='{"dropdown":{}}'
data-toggle="dropdown"
data-trigger-keypress-button="true">
<strong class="view-<?= $block->escapeHtml($block->getCurrentStoreCode()) ?>">
<span><?= $block->escapeHtml($block->getStoreName()) ?></span>
</strong>
</div>
<ul class="dropdown switcher-dropdown"
data-target="dropdown">
<?php foreach ($block->getStores() as $_lang) : ?>
<?php if ($_lang->getId() != $block->getCurrentStoreId()) : ?>
<li class="view-<?= $block->escapeHtml($_lang->getCode()) ?> switcher-option">
<a href="<?= $block->escapeUrl($block->getViewModel()->getTargetStoreRedirectUrl($_lang)) ?>">
<?= $block->escapeHtml($_lang->getName()) ?>
</a>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div>
<?php } ?>
</div>

您可以使用以下代码来归档您的需求

在下面的回答中,我假设您知道如何获取存储数据。

<?php
$currentStore = "English"; // THIS SHOULD BE DYNAMIC
$switchStore = "French"; // THIS SHOULD BE DYNAMIC
$URL = 'https://demo.com/product.html?___store='. $currentStore .'&amp;___from_store='.$switchStore
?>
<a href="#" onclick="window.location.href='<?= $URL ?>'"> <?= __('STORE_NAME') ?></a>

希望这对你有用。

谢谢。

最新更新