_localeResolver = $localeResolver; parent::__construct($data); $this->_storeManager = $storeManager; $this->_viewDesign = $viewDesign; $this->_design = $design; $this->_translate = $translate; $this->_scopeConfig = $scopeConfig; $this->inlineConfig = $inlineConfig; $this->inlineTranslation = $inlineTranslation; $this->logger = $logger; } /** * Start environment emulation of the specified store * * Function returns information about initial store environment and emulates environment of another store * * @param integer $storeId * @param string $area * @param bool $force A true value will ensure that environment is always emulated, regardless of current store * @return void */ public function startEnvironmentEmulation( $storeId, $area = \Magento\Framework\App\Area::AREA_FRONTEND, $force = false ) { // Only allow a single level of emulation if ($this->initialEnvironmentInfo !== null) { $this->logger->error(__('Environment emulation nesting is not allowed.')); return; } if ($storeId == $this->_storeManager->getStore()->getStoreId() && !$force) { return; } $this->storeCurrentEnvironmentInfo(); // emulate inline translations $this->inlineTranslation->suspend($this->inlineConfig->isActive($storeId)); // emulate design $storeTheme = $this->_viewDesign->getConfigurationDesignTheme($area, ['store' => $storeId]); $this->_viewDesign->setDesignTheme($storeTheme, $area); if ($area == \Magento\Framework\App\Area::AREA_FRONTEND) { $designChange = $this->_design->loadChange($storeId); if ($designChange->getData()) { $this->_viewDesign->setDesignTheme($designChange->getDesign(), $area); } } // Current store needs to be changed right before locale change and after design change $this->_storeManager->setCurrentStore($storeId); // emulate locale $newLocaleCode = $this->_scopeConfig->getValue( $this->_localeResolver->getDefaultLocalePath(), \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId ); $this->_localeResolver->setLocale($newLocaleCode); $this->_translate->setLocale($newLocaleCode); $this->_translate->loadData($area); return; } /** * Stop environment emulation * * Function restores initial store environment * * @return \Magento\Store\Model\App\Emulation */ public function stopEnvironmentEmulation() { if ($this->initialEnvironmentInfo === null) { return $this; } $this->_restoreInitialInlineTranslation($this->initialEnvironmentInfo->getInitialTranslateInline()); $initialDesign = $this->initialEnvironmentInfo->getInitialDesign(); $this->_restoreInitialDesign($initialDesign); // Current store needs to be changed right before locale change and after design change $this->_storeManager->setCurrentStore($initialDesign['store']); $this->_restoreInitialLocale($this->initialEnvironmentInfo->getInitialLocaleCode(), $initialDesign['area']); $this->initialEnvironmentInfo = null; return $this; } /** * Stores current environment info * * @return void */ public function storeCurrentEnvironmentInfo() { $this->initialEnvironmentInfo = new \Magento\Framework\DataObject(); $this->initialEnvironmentInfo->setInitialTranslateInline( $this->inlineTranslation->isEnabled() )->setInitialDesign( [ 'area' => $this->_viewDesign->getArea(), 'theme' => $this->_viewDesign->getDesignTheme(), 'store' => $this->_storeManager->getStore()->getStoreId(), ] )->setInitialLocaleCode( $this->_localeResolver->getLocale() ); } /** * Restore initial inline translation state * * @param bool $initialTranslate * @return $this */ protected function _restoreInitialInlineTranslation($initialTranslate) { $this->inlineTranslation->resume($initialTranslate); return $this; } /** * Restore design of the initial store * * @param array $initialDesign * @return $this */ protected function _restoreInitialDesign(array $initialDesign) { $this->_viewDesign->setDesignTheme($initialDesign['theme'], $initialDesign['area']); return $this; } /** * Restore locale of the initial store * * @param string $initialLocaleCode * @param string $initialArea * @return $this */ protected function _restoreInitialLocale( $initialLocaleCode, $initialArea = \Magento\Framework\App\Area::AREA_ADMINHTML ) { $this->_localeResolver->setLocale($initialLocaleCode); $this->_translate->setLocale($initialLocaleCode); $this->_translate->loadData($initialArea); return $this; } }