appState = $appState; $this->config = $config; $this->fileReadFactory = $fileReadFactory; $this->translate = $translate; $this->filesUtility = (null !== $filesUtility) ? $filesUtility : new \Magento\Framework\App\Utility\Files( $componentRegistrar, $dirSearch, $themePackageList ); } /** * Get translation data * * @param string $themePath * @return array * @throws \Exception * @throws \Magento\Framework\Exception\LocalizedException */ public function getData($themePath) { $areaCode = $this->appState->getAreaCode(); $files = array_merge( $this->filesUtility->getJsFiles('base', $themePath), $this->filesUtility->getJsFiles($areaCode, $themePath), $this->filesUtility->getStaticHtmlFiles('base', $themePath), $this->filesUtility->getStaticHtmlFiles($areaCode, $themePath) ); $dictionary = []; foreach ($files as $filePath) { $read = $this->fileReadFactory->create($filePath[0], \Magento\Framework\Filesystem\DriverPool::FILE); $content = $read->readAll(); foreach ($this->getPhrases($content) as $phrase) { try { $translatedPhrase = $this->translate->render([$phrase], []); if ($phrase != $translatedPhrase) { $dictionary[$phrase] = $translatedPhrase; } } catch (\Exception $e) { throw new LocalizedException( __('Error while translating phrase "%s" in file %s.', $phrase, $filePath[0]), $e ); } } } return $dictionary; } /** * Parse content for entries to be translated * * @param string $content * @return string[] * @throws \Magento\Framework\Exception\LocalizedException */ protected function getPhrases($content) { $phrases = []; foreach ($this->config->getPatterns() as $pattern) { $concatenatedContent = preg_replace('~(["\'])\s*?\+\s*?\1~', '', $content); $result = preg_match_all($pattern, $concatenatedContent, $matches); if ($result) { if (isset($matches[2])) { foreach ($matches[2] as $match) { $phrases[] = str_replace(["\'", '\"'], ["'", '"'], $match); } } } if (false === $result) { throw new LocalizedException( __('Error while generating js translation dictionary: "%s"', error_get_last()) ); } } return $phrases; } }