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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\App\Language;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Filesystem\Directory\ReadFactory;
/**
* A service for reading language package dictionaries
*
* @api
* @since 100.0.2
*/
class Dictionary
{
/**
* Paths of all language packages
*
* @var string[]
*/
private $paths;
/**
* Creates directory read objects
*
* @var ReadFactory
*/
private $directoryReadFactory;
/**
* Component Registrar
*
* @var ReadFactory
*/
private $componentRegistrar;
/**
* @var ConfigFactory
*/
private $configFactory;
/**
* @var array
*/
private $packList = [];
/**
* @param ReadFactory $directoryReadFactory
* @param ComponentRegistrar $componentRegistrar
* @param ConfigFactory $configFactory
*/
public function __construct(
ReadFactory $directoryReadFactory,
ComponentRegistrar $componentRegistrar,
ConfigFactory $configFactory
) {
$this->directoryReadFactory = $directoryReadFactory;
$this->componentRegistrar = $componentRegistrar;
$this->configFactory = $configFactory;
}
/**
* Load and merge all phrases from language packs by specified code
*
* Takes into account inheritance between language packs
* Returns associative array where key is phrase in the source code and value is its translation
*
* @param string $languageCode
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getDictionary($languageCode)
{
$languages = [];
$this->paths = $this->componentRegistrar->getPaths(ComponentRegistrar::LANGUAGE);
foreach ($this->paths as $path) {
$directoryRead = $this->directoryReadFactory->create($path);
if ($directoryRead->isExist('language.xml')) {
$xmlSource = $directoryRead->readFile('language.xml');
try {
$languageConfig = $this->configFactory->create(['source' => $xmlSource]);
} catch (\Magento\Framework\Config\Dom\ValidationException $e) {
throw new \Magento\Framework\Exception\LocalizedException(
new \Magento\Framework\Phrase(
'The XML in file "%1" is invalid:' . "\n%2\nVerify the XML and try again.",
[$path . '/language.xml', $e->getMessage()]
),
$e
);
}
$this->packList[$languageConfig->getVendor()][$languageConfig->getPackage()] = $languageConfig;
if ($languageConfig->getCode() === $languageCode) {
$languages[] = $languageConfig;
}
}
}
// Collect the inherited packages with meta-information of sorting
$packs = [];
foreach ($languages as $languageConfig) {
$this->collectInheritedPacks($languageConfig, $packs);
}
uasort($packs, [$this, 'sortInherited']);
// Merge all packages of translation to one dictionary
$result = [];
foreach ($packs as $packInfo) {
/** @var Config $languageConfig */
$languageConfig = $packInfo['language'];
$dictionary = $this->readPackCsv($languageConfig->getVendor(), $languageConfig->getPackage());
foreach ($dictionary as $key => $value) {
$result[$key] = $value;
}
}
return $result;
}
/**
* Line up (flatten) a tree of inheritance of language packs
*
* Record level of recursion (level of inheritance) for further use in sorting
*
* @param Config $languageConfig
* @param array $result
* @param int $level
* @param array $visitedPacks
* @return void
*/
private function collectInheritedPacks($languageConfig, &$result, $level = 0, array &$visitedPacks = [])
{
$packKey = implode('|', [$languageConfig->getVendor(), $languageConfig->getPackage()]);
if (!isset($visitedPacks[$packKey]) &&
(!isset($result[$packKey]) || $result[$packKey]['inheritance_level'] < $level)
) {
$visitedPacks[$packKey] = true;
$result[$packKey] = [
'inheritance_level' => $level,
'sort_order' => $languageConfig->getSortOrder(),
'language' => $languageConfig,
'key' => $packKey,
];
foreach ($languageConfig->getUses() as $reuse) {
if (isset($this->packList[$reuse['vendor']][$reuse['package']])) {
$parentLanguageConfig = $this->packList[$reuse['vendor']][$reuse['package']];
$this->collectInheritedPacks($parentLanguageConfig, $result, $level + 1, $visitedPacks);
}
}
}
}
/**
* Sub-routine for custom sorting packs using inheritance level and sort order
*
* First sort by inheritance level descending, then by sort order ascending
*
* @param array $current
* @param array $next
* @return int
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
*/
private function sortInherited($current, $next)
{
if ($current['inheritance_level'] > $next['inheritance_level']) {
return -1;
} elseif ($current['inheritance_level'] < $next['inheritance_level']) {
return 1;
}
if ($current['sort_order'] > $next['sort_order']) {
return 1;
} elseif ($current['sort_order'] < $next['sort_order']) {
return -1;
}
return strcmp($current['key'], $next['key']);
}
/**
* Read the CSV-files in a language package
*
* The files are sorted alphabetically, then each of them is read, and results are recorded into key => value array
*
* @param string $vendor
* @param string $package
* @return array
*/
private function readPackCsv($vendor, $package)
{
$path = $this->componentRegistrar->getPath(ComponentRegistrar::LANGUAGE, strtolower($vendor . '_' . $package));
$result = [];
if (isset($path)) {
$directoryRead = $this->directoryReadFactory->create($path);
$foundCsvFiles = $directoryRead->search("*.csv");
foreach ($foundCsvFiles as $foundCsvFile) {
$file = $directoryRead->openFile($foundCsvFile);
while (($row = $file->readCsv()) !== false) {
if (is_array($row) && count($row) > 1) {
$result[$row[0]] = $row[1];
}
}
}
}
return $result;
}
}