Magento 2.4 Add custom language switcher in header

Magento 2.4 Add custom language switcher in header

September 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2.4 Add custom language switcher in header

Customizing Magento Header: Adding a Dual-Language Switcher

In this tutorial, we’ll guide you through creating a custom header in Magento 2.4 with a unique language switcher feature. We’re aiming to display only one language option (the second language) in the header of the primary language.

Understanding Your Requirements

You want to:

  1. Create a custom header.
  2. Include a language switcher that doesn’t use the default dropdown view.
  3. Show only the second language option in the primary language’s header (e.g., French in English).
  4. Navigate users directly to the translated page, not the homepage.

Setting Up Your Environment

First, make sure you have Magento 2.4 installed and running on your local or remote server. For this example, we’ll assume you’re using a standard LAMP stack with PHP 7.4+ and MySQL 5.6+.

Building the Custom Header

To start, navigate to app/design/frontend/{vendor}/{theme}/Magento_Theme/templates/html/header.phtml. This is where we’ll customize the header layout.

<!-- app/design/frontend/{vendor}/{theme}/Magento_Theme/templates/html/header.phtml -->
<div class="header">
    <div class="logo">
        <!-- Your logo here -->
    </div>
    <div class="language-switcher">
        <?php if (count($this->getAvailableLanguages()) > 1): ?>
            <a href="<?php echo $this->url->getUrl('multilang'); ?>">Français</a> 
        <?php endif; ?>
    </div>
</div>

In the header.phtml file, we’ve added a new div for the language switcher. Inside this div, we’re checking if there are more than one available languages using $this->getAvailableLanguages().

If true, it means you have at least two languages set up in Magento’s configuration.

Creating the Translation URL

To make sure clicking on “Français” takes users directly to the French version of the page they’re currently viewing, we need a custom route.

Navigate to app/code/{vendor}/{module}/etc/frontend/routes.xml. If this file doesn’t exist, create it. Inside, add the following:

<!-- app/code/{vendor}/{module}/etc/frontend/routes.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="multilang" frontName="multilang">
            <module name="{vendor}_{module}" />
        </route>
    </router>
</config>

This defines a new route called multilang that maps to your custom module.

Implementing the Language Switcher Logic

Create the file app/code/{vendor}/{module}/Controller/Index/Translate.php. This is where we’ll handle the language switching functionality:

<!-- app/code/{vendor}/{module}/Controller/Index/Translate.php -->
namespace {Vendor}\{Module}\Controller\Index;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\ResultFactory;

class Translate extends \Magento\Framework\App\Action\Action
{
    private $request;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        RequestInterface $request
    ) {
        parent::__construct($context);
        $this->request = $request;
    }

    public function execute()
    {
        $currentLanguageCode = $this->request->getParam('language');
        $storeId = $this->request->getParam('store_id');

        // Your logic to switch the language...
        // You might use Magento's store switching API or your own implementation here

        return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setUrl($url);
    }
}

This controller accepts two parameters: language (the code of the target language) and store_id. Your task is to fill in the logic for actually switching the user’s language.

Conclusion

With this setup, you should now have a custom header with a unique language switcher that displays only one option—directly linking users to the translated version of their current page.

Magento 2 Database Themes Configuration

Our network