Get Current store locale/language programmatically in Magento 2.

Get Current store locale/language programmatically in Magento 2.

Magento 2 with multi-store functionality, and Different store view contains different locale.

You can set different locales from the admin panel using Stores -> Configuration -> General Tab.
Click on Locale Options,

Set Your locale from the Locale Dropdown box. You must set to this locale at the Store view level.

You can get current store locale programmatically using Resolver class.

Magento\Framework\Locale\Resolver class is used to get the Current store locale code.

<?php
namespace ArmMage\CurrentLocale\Block;

use Magento\Framework\Locale\Resolver;

class Locale
{
    /**
     * @var Resolver
     */
    private $localeResolver;
   
    /**
     * Construct function
     *
     * @param  Resolver $localeResolver
     */
    public function __construct(
        Resolver $localeResolver
    ) {
        $this->localeResolver = $localeResolver;
    }

    /**
     * get current  Locale  function
     *
     * @return string
     */
    public function getCurrentLocale()
    {
        $currentLocaleCode = $this->localeResolver->getLocale(); // fr_CA
        $languageCode = strstr($currentLocaleCode, '_', true);
        return $languageCode;
    }

}

I have given a demo using a simple Class,

The Result will be,
fr for French Store view.
en for English store view.


If this solution isn't working for you, seek answers from ChatGPT


Please note that to ensure accurate and relevant responses, your questions should be related to Magento 2.

Ready to elevate your e-commerce business?

Let's talk about your business goals. Contact us now to see how we can help achieve them.

 
Copyright © 2021-present ArmMage Inc. All rights reserved.