4

Is there a way to get the list of all Locale for set up stores?

As you would get Locale for the current store with

\Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\Locale\Resolver')->getLocale();
10

From my understanding, we will get the locale values from core_config_data table. To get these values, we need to have the path, scope type and scope code:

We will try to get all stores and use scope config object to get the locale values.

/** @var \Magento\Framework\App\ObjectManager $obj */
$obj = \Magento\Framework\App\ObjectManager::getInstance();

/** @var \Magento\Store\Model\StoreManagerInterface|\Magento\Store\Model\StoreManager $storeManager */
$storeManager = $obj->get('Magento\Store\Model\StoreManagerInterface');
$stores = $storeManager->getStores($withDefault = false);

//Get scope config
/** @var \Magento\Framework\App\Config\ScopeConfigInterface|\Magento\Framework\App\Config $scopeConfig */
$scopeConfig = $obj->get('Magento\Framework\App\Config\ScopeConfigInterface');

//Locale code
$locale = [];

//Try to get list of locale for all stores;
foreach($stores as $store) {
    $locale[] = $scopeConfig->getValue('general/locale/code', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store->getStoreId());
}

There's a good example how to get locale: vendor/magento/module-store/Block/Switcher.php

EDIT: Avoiding using Object Manager directly.

Need to inject:

  • Magento\Store\Model\StoreManagerInterface
  • Magento\Framework\App\Config\ScopeConfigInterface

into the constructor.

<?php

namespace Vendor\Stores\Model;

class Locale
{
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    protected $scopeConfig;

    /**
     * Locale constructor.
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     */
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    )
    {
        $this->storeManager = $storeManager;
        $this->scopeConfig = $scopeConfig;
    }

    /**
     * Get list of Locale for all stores
     * @return array
     */
    public function getListLocaleForAllStores()
    {
        //Locale code
        $locale = [];
        $stores = $this->storeManager->getStores($withDefault = false);
        //Try to get list of locale for all stores;
        foreach($stores as $store) {
            $locale[] = $this->scopeConfig->getValue('general/locale/code', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store->getStoreId());
        }
        return $locale;
    }
}
|improve this answer|||||
  • 1
    Thanks, that is what I was trying to do. Just fix $storeConfig inside of foreach with $scopeConfig. – Stasa Stanisic Jul 13 '16 at 8:16

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.