-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
DataProvider.php
155 lines (140 loc) · 4.84 KB
/
DataProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Translation\Model\Js;
use Magento\Framework\Exception\LocalizedException;
/**
* DataProvider for js translation
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class DataProvider implements DataProviderInterface
{
/**
* Application state
*
* @var \Magento\Framework\App\State
*/
protected $appState;
/**
* Js translation configuration
*
* @var Config
*/
protected $config;
/**
* @var \Magento\Framework\App\Utility\Files
*/
protected $filesUtility;
/**
* Filesystem
*
* @var \Magento\Framework\Filesystem\File\ReadFactory
*/
protected $fileReadFactory;
/**
* Basic translate renderer
*
* @var \Magento\Framework\Phrase\RendererInterface
*/
protected $translate;
/**
* @param \Magento\Framework\App\State $appState
* @param Config $config
* @param \Magento\Framework\Filesystem\File\ReadFactory $fileReadFactory
* @param \Magento\Framework\Phrase\RendererInterface $translate
* @param \Magento\Framework\Component\ComponentRegistrar $componentRegistrar
* @param \Magento\Framework\Component\DirSearch $dirSearch
* @param \Magento\Framework\View\Design\Theme\ThemePackageList $themePackageList
* @param \Magento\Framework\App\Utility\Files|null $filesUtility
*/
public function __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
) {
$this->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
);
}
}
}
ksort($dictionary);
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[] = $match !== null ? str_replace(["\'", '\"'], ["'", '"'], $match) : '';
}
}
}
if (false === $result) {
throw new LocalizedException(
__('Error while generating js translation dictionary: "%s"', error_get_last())
);
}
}
return $phrases;
}
}