#StackBounty: #magento2 #multistore #locale #magento2.2.4 Resolver does not report correct locale

Bounty: 50

I have a very strange problem where the MagentoFrameworkLocaleResolver 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 MagentoStoreModelStoreManagerInterface */
  private $storeManager;

  /** @var MagentoFrameworkLocaleResolver */
  private $resolver;

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

  public function getLocale()
  {
     return $this->resolver->getLocale();
  }
}

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

$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


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.