Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataProvider.php
Go to the documentation of this file.
1 <?php
8 
10 
17 {
23  protected $appState;
24 
30  protected $config;
31 
37  protected $filesUtility;
38 
44  protected $fileReadFactory;
45 
51  protected $translate;
52 
63  public function __construct(
64  \Magento\Framework\App\State $appState,
66  \Magento\Framework\Filesystem\File\ReadFactory $fileReadFactory,
67  \Magento\Framework\Phrase\RendererInterface $translate,
68  \Magento\Framework\Component\ComponentRegistrar $componentRegistrar,
69  \Magento\Framework\Component\DirSearch $dirSearch,
70  \Magento\Framework\View\Design\Theme\ThemePackageList $themePackageList,
71  \Magento\Framework\App\Utility\Files $filesUtility = null
72  ) {
73  $this->appState = $appState;
74  $this->config = $config;
75  $this->fileReadFactory = $fileReadFactory;
76  $this->translate = $translate;
77  $this->filesUtility = (null !== $filesUtility) ?
78  $filesUtility : new \Magento\Framework\App\Utility\Files(
80  $dirSearch,
82  );
83  }
84 
93  public function getData($themePath)
94  {
95  $areaCode = $this->appState->getAreaCode();
96 
97  $files = array_merge(
98  $this->filesUtility->getJsFiles('base', $themePath),
99  $this->filesUtility->getJsFiles($areaCode, $themePath),
100  $this->filesUtility->getStaticHtmlFiles('base', $themePath),
101  $this->filesUtility->getStaticHtmlFiles($areaCode, $themePath)
102  );
103 
104  $dictionary = [];
105  foreach ($files as $filePath) {
106  $read = $this->fileReadFactory->create($filePath[0], \Magento\Framework\Filesystem\DriverPool::FILE);
107  $content = $read->readAll();
108  foreach ($this->getPhrases($content) as $phrase) {
109  try {
110  $translatedPhrase = $this->translate->render([$phrase], []);
111  if ($phrase != $translatedPhrase) {
112  $dictionary[$phrase] = $translatedPhrase;
113  }
114  } catch (\Exception $e) {
115  throw new LocalizedException(
116  __('Error while translating phrase "%s" in file %s.', $phrase, $filePath[0]),
117  $e
118  );
119  }
120  }
121  }
122 
123  return $dictionary;
124  }
125 
133  protected function getPhrases($content)
134  {
135  $phrases = [];
136  foreach ($this->config->getPatterns() as $pattern) {
137  $concatenatedContent = preg_replace('~(["\'])\s*?\+\s*?\1~', '', $content);
138  $result = preg_match_all($pattern, $concatenatedContent, $matches);
139 
140  if ($result) {
141  if (isset($matches[2])) {
142  foreach ($matches[2] as $match) {
143  $phrases[] = str_replace(["\'", '\"'], ["'", '"'], $match);
144  }
145  }
146  }
147  if (false === $result) {
148  throw new LocalizedException(
149  __('Error while generating js translation dictionary: "%s"', error_get_last())
150  );
151  }
152  }
153  return $phrases;
154  }
155 }
$componentRegistrar
Definition: bootstrap.php:23
$pattern
Definition: website.php:22
__()
Definition: __.php:13
$themePackageList
Definition: bootstrap.php:88
$dirSearch
Definition: bootstrap.php:86
__construct(\Magento\Framework\App\State $appState, Config $config, \Magento\Framework\Filesystem\File\ReadFactory $fileReadFactory, \Magento\Framework\Phrase\RendererInterface $translate, \Magento\Framework\Component\ComponentRegistrar $componentRegistrar, \Magento\Framework\Component\DirSearch $dirSearch, \Magento\Framework\View\Design\Theme\ThemePackageList $themePackageList, \Magento\Framework\App\Utility\Files $filesUtility=null)
jquery extjs ext tree mage adminhtml mage translate
Definition: tree.phtml:41
foreach($appDirs as $dir) $files