9

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

13

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 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 use getLocaleCode() to get laguage:

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

if($currentStore == 'en_US'){

}

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 use getLocale() to get laguage:

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

if($currentStore == 'en_US'){

}
4

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

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

You can use \Magento\Store\Api\Data\StoreInterface to get locale code

/**@var \Magento\Store\Api\Data\StoreInterface **/
protected $_store;

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

you can check like this :

$this->_store->getLocaleCode() == 'en_EN';

Hope this code useful for you.

  • please tell me if have issue .... or if it is useful then accept answer...so other people can use this code. @Rick – Rohan Hapani Aug 17 '17 at 9:50
  • Does the store really have a locale code? As far as I know it is a configuration and not saved directly on the store itself.. – Chuvisco Aug 10 '18 at 7:05

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.