我在Magento2.4.4中创建了一个自定义标题,我希望包括语言开关,而不是默认的下拉视图。
我只想包含到第二语言的链接.
因此,如果我有两种语言&英语&法语,我只想在标题处显示法语,所以当用户点击它时,它应该带他们到翻译过的页面,而不是主页。
发布于 2022-09-21 07:15:33
Hi @Zee我们可以在设计文件夹中使用phtml文件来创建一个语言转换程序,如下面的app/design/frontend/vendor/Module/Magento_Store/templates/switch/languages.phtml
<?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>发布于 2022-09-21 11:15:28
您可以使用下面的代码来存档您的需求。
在下面的答案中,我假设您知道如何获取存储数据。
<?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>希望这对你有用。
谢谢。
https://stackoverflow.com/questions/73777705
复制相似问题