10

I'm trying to show a custom block for each store view / language. Therefore I want to create switch statement like:

$lang = // Get language code or store view code here;
switch ($lang) {

    case 'en':
        // English block
        break;

    case 'nl':
        // Dutch block
        break;

    default:
        // Dutch block
        break;
}

How can I get this? I need it in this file \app\design\frontend\Venustheme\floristy\Ves_Themesettings\templates\header\default.phtml

14

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

  1. BY USING \Magento\Store\Api\Data\StoreInterface CLASS

With Dependency Injection

protected $_store;

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

Now use getLocaleCode() to get language:

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

if($currentStore == 'en_US'){
    
}

With objectManager

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

echo $store->getLocaleCode();
  1. BY USING Magento\Framework\Locale\Resolver CLASS

With Dependency Injection

protected $_store;

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

Now use getLocale() to get language:

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

if($currentStore == 'en_US'){
    
}

With objectManager

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

echo $store->getLocale();

NOTE: You should never use \Magento\Framework\App\ObjectManager::getInstance() It defeats the purpose of dependency injection.

| improve this answer | |
5

You can get current locale by using below way,

Use of Directly Objectmanager in phtml file is not perfect way for magento 2 standard,

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$getLocale = $objectManager->get('Magento\Framework\Locale\Resolver');
$haystack  = $getLocale->getLocale(); 
$lang = strstr($haystack, '_', true); 
switch ($lang) {

    case 'en':
        // English block
        break;

    case 'nl':
        // Dutch block
        break;

    default:
        // Dutch block
        break;
}

You can call Block file and set one function for your requirement and call those function inside phtml file.

public function __construct(
        \Magento\Framework\Locale\Resolver $locale
    ) {
        $this->locale = $locale;
    }

call inside phtml file,

$currentCode = $this->locale->getLocale();
$langCode = strstr($currentCode, '_', true);
if($langCode == 'en_US'){

}
| improve this answer | |
  • +1 for strstr($haystack, '_', true); , good trick – Milan Simek Jun 4 '18 at 14:27

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.