0

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

  • First of all please check your code...You are using magento 2 site and using magento 1 code...then how do you get the correct answer???? – Rita Jose Mar 23 '18 at 10:43
  • I am using Magento 2.1.4. and the code I got it from other post so maybe it doesnt work for my case. any ideahow to fix that ? – Ismail Berlin Mar 23 '18 at 10:49
  • hi. added a new code to get language. please check that – Rita Jose Mar 23 '18 at 11:08
3

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'){

}
| improve this answer | |
0

You'd need to set yourself either an override block, or a helper that allows you access to \Magento\Framework\App\Config\ScopeConfigInterface

Just Use \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, in your constructor argument and set a class property: $this->scopeConfig = $scopeConfig;

To get a specific configuration attribute, just use:

$this->scopeConfig->getValue('general/country/default', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

Assuming you wanted to implement this as a helper, then you'd be looking at something like the following:

app/code/<vendor>/<namespace>/Helper/Config.php

<?php
namespace Vendor\Namespace\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use \Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Helper\Context;

class Config extends AbstractHelper
{
    protected $scopeConfig;
    function __construct()
    {
        ScopeConfigInterface $scopeConfig,
        Context $context
    )
    {
        $this->scopeConfig = $scopeConfig;
        parent::__construct($context);
    }

    function getDefaultCountry()
    {
        return $this->scopeConfig->getValue('general/country/default', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }

}

In your template, you'd call:

$_helper = $this->helper('\Vendor\Namespace\Helper\Config');
echo $_helper->getDefaultCountry();
| improve this answer | |
  • I get : 1 exception(s): Exception #0 (Exception): Notice: Undefined property: Magento\Framework\View\Element\Template::$scopeConfig in /var/www/html/vendor/magento/framework/View/TemplateEngine/Php.php on line 110 – Ismail Berlin Mar 23 '18 at 10:05
  • where do I find the constructor ? – Ismail Berlin Mar 23 '18 at 10:06
  • Have you implemented this directly into the template? – Douglas Radburn Mar 23 '18 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.