Question

I am trying to get language switcher in header.phtml file. I tried this code in header.phtml. it doesnt work in magento 2.2version. Kindly help me to resolve the issue

<?php echo $this->getLayout()->createBlock('page/switch')->setTemplate('page/switch/languages.phtml')->tohtml(); ?>
Was it helpful?

Solution

Language block is already in header block see below code for your reference

<container name="header.panel.wrapper" htmlClass="panel wrapper" htmlTag="div" before="-">
            <container name="header.panel" label="Page Header Panel" htmlTag="div" htmlClass="panel header">
                <block class="Magento\Framework\View\Element\Template" name="skip_to_content" template="Magento_Theme::html/skip.phtml">
                    <arguments>
                        <argument name="target" xsi:type="string">contentarea</argument>
                        <argument name="label" translate="true" xsi:type="string">Skip to Content</argument>
                    </arguments>
                </block>
                <block class="Magento\Store\Block\Switcher" name="store_language" as="store_language" template="Magento_Store::switch/languages.phtml">
                    <arguments>
                        <argument name="view_model" xsi:type="object">Magento\Store\ViewModel\SwitcherUrlProvider</argument>
                    </arguments>
                </block>
                <block class="Magento\Customer\Block\Account\Navigation" name="top.links">
                    <arguments>
                        <argument name="css_class" xsi:type="string">header links</argument>
                    </arguments>
                </block>
            </container>
        </container>

Can you put your code screenshot what you have done for language switcher in your code ?

OTHER TIPS

Add code in your custom layout (.xml) file or local.xml file
    <reference name="header">
        <block type="page/switch" name="custom_store_language" as="custom_store_language" template="page/switch/languages.phtml"/>
    </reference>

You can do this by two way

First add below code in phtml file :

<div class="switcher language switcher-language" data-ui-id="language-switcher" id="switcher-language<?php echo $id?>">
    <strong class="label switcher-label"><span><?php echo __('Language') ?></span></strong>
    <div class="actions dropdown options switcher-options">
        <div class="action toggle switcher-trigger" id="switcher-language-trigger<?php echo $id?>">
            <strong class="view-<?php echo $this->escapeHtml($this->getCurrentStoreCode()) ?>">
                <span><?php echo $this->escapeHtml($this->getStoreName()) ?></span>
            </strong>
        </div>
        <ul class="dropdown switcher-dropdown"
            data-mage-init='{"dropdownDialog":{
                "appendTo":"#switcher-language<?php echo $id ?> > .options",
                "triggerTarget":"#switcher-language-trigger<?php echo $id ?>",
                "closeOnMouseLeave": false,
                "triggerClass":"active",
                "parentClass":"active",
                "buttons":null}}'>
            <?php foreach ($this->getStores() as $_lang): ?>
                <?php if($_lang->getId()!=$this->getCurrentStoreId()): ?>
                    <li class="view-<?php echo $this->escapeHtml($_lang->getCode()); ?> switcher-option">
                        <a href="#" data-post='<?php echo $this->getTargetStorePostData($_lang); ?>'>
                            <?php echo $this->escapeHtml($_lang->getName()) ?></a>
                    </li>
                <?php endif; ?>
            <?php endforeach; ?>
        </ul>
    </div>
</div>

Second way is add below code in xml file like default.xml

<block type="page/switch" name="store_language" as="store_language" template="page/switch/languages.phtml"/>

Note : Clear Cache

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top