skip to Main Content

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.

Screenshot of what i want

2

Answers


  1. 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 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>
    
    Login or Signup to reply.
  2. 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 .'&amp;___from_store='.$switchStore
    ?>
    <a href="#" onclick="window.location.href='<?= $URL ?>'"> <?= __('STORE_NAME') ?></a>
    

    Hope this will useful for you.

    Thank you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search