How to get the current language of the store?
How to get the current language of the store ?

How to get the current language of the store?

You can use both \Magento\Store\Api\Data\StoreInterface or Magento\Framework\Locale\Resolver class to get the current language of the store.

1) By using \Magento\Store\Api\Data\StoreInterface Class

With objectManager:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$store = $objectManager->get('Magento\Store\Api\Data\StoreInterface'); 
echo $store->getLocaleCode();

With Dependency Injection:

protected $_store;

public function __construct(
    ...
    \Magento\Store\Api\Data\StoreInterface $store,
    ...
) {
    ...
    $this->_store = $store;
    ...
}

Now you can use getLocaleCode() function to get the current store’s language:

$currentStore = $this->_store->getLocaleCode();
if($currentStore == 'en_US'){
    //Do your code
}

2) By using Magento\Framework\Locale\Resolver class

With objectManager:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$store = $objectManager->get('Magento\Framework\Locale\Resolver');
echo $store->getLocale();

With Factory Method:

protected $_store;

public function __construct(
    ...
    Magento\Framework\Locale\Resolver $store,
    ...
) {
    ...
    $this->_store = $store;
    ...
}

Now you can use getLocale() function to get the current store’s language:

$currentStore = $this->_store->getLocale();
if($currentStore == 'en_US'){
    //Do your code
}

Leave a Reply

Close Menu