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(); ?>

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">
                        <argument name="target" xsi:type="string">contentarea</argument>
                        <argument name="label" translate="true" xsi:type="string">Skip to Content</argument>
                <block class="Magento\Store\Block\Switcher" name="store_language" as="store_language" template="Magento_Store::switch/languages.phtml">
                        <argument name="view_model" xsi:type="object">Magento\Store\ViewModel\SwitcherUrlProvider</argument>
                <block class="Magento\Customer\Block\Account\Navigation" name="top.links">
                        <argument name="css_class" xsi:type="string">header links</argument>

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

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"/>
  • please elaborate what to add in xml.. – SUBBULAKSHMI G Nov 14 '18 at 12:34

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>
        <ul class="dropdown switcher-dropdown"
                "appendTo":"#switcher-language<?php echo $id ?> > .options",
                "triggerTarget":"#switcher-language-trigger<?php echo $id ?>",
                "closeOnMouseLeave": false,
            <?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>
                <?php endif; ?>
            <?php endforeach; ?>

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


create default.xml file in your layout folder. Your file path will be like below. enter image description here

enter image description here

Now store switcher will show on the header part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.