Can’t Get Current Language in Template File in Magento 2

languagemagento2template

I am trying to set language ico in the frontend.
I try to get the language name using Mage::getStoreConfig('general/country/default'); but I get an error:
This is my setting.phtml

<?php
$id = $block->getIdModifier() ? '-' . $block->getIdModifier() : '';
?>
<div class="custom-group setting-links dropdown" id="setting-links<?php echo $id?>">
    <a class="action toggle switcher-trigger" id="setting-links" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        <i class="fa fa-cog"> </i><?php echo  Mage::getStoreConfig('general/country/default'); ?>
    </a>

    <div class="setting-links-options dropdown-menu" aria-labelledby="setting-links">
        <div class="switcher-currency-wrapper">
            <?php echo $this->getChildHtml('currency_custom') ?>
        </div>
        <div class="switcher-language-wrapper">
            <?php echo $this->getChildHtml('store_language_custom') ?>
        </div>
    </div>
</div>

This generates an error :

PHP Fatal error: Uncaught Error: Class 'Mage' not found in
/var/www/html/app/design/frontend/Venustheme/fasony/Magento_Theme/templates/html/setting.phtml:15

Best Answer

You can get the current using laguage by below code:

With objectManager

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

OR

With Factory Method

protected $_store;

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

Now use getLocale() to get laguage:

$currentStore = $this->_store->getLocale();

if($currentStore == 'en_US'){

}
Related Topic