2

I have a very strange problem where the Magento\Framework\Locale\Resolver does not return the correct locale for the current store view.

I have setup a store with two separate store views which have locales set to English and Swedish. I have also configured the stores to append the store code to the current URL.

class Example {
  /** @var \Magento\Store\Model\StoreManagerInterface */
  private $storeManager;

  /** @var \Magento\Framework\Locale\Resolver */
  private $resolver;

  public function __construct(StoreManagerInterface $storeManager,
                              Resolver $resolver)
  {
     $this->storeManager = $storeManager;
     $this->resolver = $resolver;
  }

  /**
   * Get locale using resolver.
  **/
  public function getLocale()
  {
     return $this->resolver->getLocale();
  }

  /**
   * Get locale using storemanager
  **/
  public function getLocaleCode()
  {
     return $this->storeManager->getStore()->getLocaleCode();
  }
}

In the above code the getLocale() and getLocaleCode() always returns the locale of the default store view. I managed to solve this by using the ScopeConfigInterface like this:

class Example {
  /** @var \Magento\Framework\App\Config\ScopeConfigInterface */
  private $scopeConfig;

  public function __construct(ScopeConfigInterface $scopeConfig)
  {
     $this->scopeConfig = $scopeConfig;
  }

  public function getLocale()
  {
     return
     $this->scopeConfig->getValue(
       'general/locale/code', 
       ScopeInterface::SCOPE_STORE,
       $this->storeManager->getStore()->getId()
     );
  }
}

Even though the above works I really would like to know why the resolver returns the incorrect locale for the current store? I am thinking that this might be some kind of weird caching issue, I have tried all the following without any success:

php bin/magento cache:clean
php bin/magento cache:flush
php bin/magento setup:di:compile
php bin/magento setup:upgrade
php bin/magento indexer:reindex
0

2 Answers 2

1

I simply use the following to get the store locale

protected $_store;

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

and seems to work fine

$this->_store->getLocaleCode()
1
  • I have tried the above and it does not seem to work in this case. Are you running version 2.2.4? May 23, 2018 at 8:55
0

You can get current locale using storeManager Interface,

<?php
public function __construct(
    \Magento\Store\Model\StoreManagerInterface $storeManager
) {
    $this->storeManager = $storeManager;
}

public function getLocale() {
    return $this->storeManager->getStore()->getLocaleCode();
}
7
  • I have already tried that and its not working same problem as with the resolver, it reports the default stores locale. May 24, 2018 at 10:48
  • Have you check using storemanager interface? May 24, 2018 at 11:02
  • Yes, I have already tried using the StoreManagerInterface and it also returns the incorrect locale. May 24, 2018 at 11:17
  • Are you really running version 2.2.4? May 24, 2018 at 11:23
  • I have checked its for 2.1.x May 24, 2018 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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