magento - multilingual site + Add store codes to url - want to show flag icons

magento - multilingual site + Add store codes to url - want to show flag icons

September 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
magento - multilingual site + Add store codes to url - want to show flag icons

Multi-Language Magento Sites: Displaying Flag Icons Instead of Language Selectors

In today’s global e-commerce landscape, providing localized experiences for your customers is key to driving sales and engagement. If you’re running a multi-language Magento site with the “Add store codes to URL” option enabled, you might want to replace the default language selector dropdown with flag icons for a cleaner, more professional look. Let me walk you through a refined approach to achieve this.

The Basic Approach: Replacing the Language Selector

As you mentioned, an effective way is to override the app/design/frontend/default/template/page/html/languages.phtml file with custom code. This allows us to iterate over available store views and replace the language selector with flag icons.

The provided code snippet serves as a solid foundation for this purpose:

<?php if(count($this->getStores())>1): ?>
<div class="form-language">
    <div class="langs-wrapper">
        <?php foreach ($this->getStores() as $_lang): ?>
            <?php if ($_lang->getCode() != 'default'): ?>
                <?php
                    $base_url = Mage::getBaseUrl();
                    // remove language in base url
                    $base_url = str_replace('/en/' , "", $base_url);
                    $base_url = str_replace('/fr/' , "", $base_url);

                    $current_url = $this->helper('core/url')->getCurrentUrl();
                    // take out base url and language code
                    $rest_of_url = str_replace($base_url, "", $current_url);
                    $rest_of_url = str_replace('/en/', "", $rest_of_url);
                    $rest_of_url = str_replace('/fr/', "", $rest_of_url);

                    // assemble new url
                    $new_url = $base_url . '/' . $_lang->getCode()  . '/' . $rest_of_url;
                ?>
                <a class="lang-flag" href="<?php echo $new_url; ?>" title="<?php echo $_lang->getName(); ?>">
                    <img src="<?php echo $this->getSkinUrl('images/flags/' . $_lang->getCode() . '.png'); ?>" alt="<?php echo $_lang->getName(); ?>">
                </a>
            <?php endif; ?>
        <?php endforeach; ?>
    </div>
</div>
<?php endif; ?>

Refining the Code for a Better Magento Experience

While this code works, it can be refined to improve performance and maintainability. Let’s make some adjustments:

  1. Use View Helpers: Instead of directly calling the Mage::getBaseUrl() function, use the Mage::helper('core/url')->getBaseUrl() view helper for a more Magento-standard approach.

  2. Avoid Multiple String Replacements: The current code replaces the language code in both the base URL and the remaining path multiple times. We can simplify this by using explode() to break down the current URL into components, then reassemble it with the desired language code.

  3. Add Alt Text for Accessibility: Ensure that each flag image has appropriate alt text describing the corresponding language name for better accessibility.

The Refined Code

Here’s a more polished version of the code:

<?php if(count($this->getStores())>1): ?>
<div class="form-language">
    <div class="langs-wrapper">
        <?php foreach ($this->getStores() as $_lang): ?>
            <?php if ($_lang->getCode() != 'default'): ?>
                <?php
                    $baseUrl = Mage::helper('core/url')->getBaseUrl();
                    $currentUrlParts = explode('/', $this->helper('core/url')->getCurrentUrl());
                    array_shift($currentUrlParts);
                    // Remove language code from remaining path
                    array_shift($currentUrlParts);

                    // Assemble new URL with desired language code
                    $newUrl = rtrim($baseUrl, '/') . '/' . $_lang->getCode() . '/' . implode('/', $currentUrlParts);
                ?>
                <a class="lang-flag" href="<?php echo $new_url; ?>" title="<?php echo $_lang->getName(); ?>">
                    <img src="<?php echo $this->getSkinUrl('images/flags/' . $_lang->getCode() . '.png'); ?>" alt="<?php echo $_lang->getName(); ?>">
                </a>
            <?php endif; ?>
        <?php endforeach; ?>
    </div>
</div>
<?php endif; ?>

Conclusion

Replacing the Magento language selector with flag icons provides a more visually appealing user experience for multi-language sites. By refining the code and adhering to Magento best practices, we can ensure our solution is efficient, maintainable, and accessible.

Remember to keep your theme’s images/flags folder populated with appropriate flag images for each supported locale. If you’re looking for high-quality flag images or need further assistance with Magento localization, feel free to explore the resources available at Laravel Company.

Happy coding! Let me know if you have any other questions about Magento development or e-commerce best practices.

Themes Customers

Our network