Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Dictionary.php
Go to the documentation of this file.
1 <?php
8 
11 
19 {
25  private $paths;
26 
32  private $directoryReadFactory;
33 
39  private $componentRegistrar;
40 
44  private $configFactory;
45 
49  private $packList = [];
50 
56  public function __construct(
57  ReadFactory $directoryReadFactory,
58  ComponentRegistrar $componentRegistrar,
59  ConfigFactory $configFactory
60  ) {
61  $this->directoryReadFactory = $directoryReadFactory;
62  $this->componentRegistrar = $componentRegistrar;
63  $this->configFactory = $configFactory;
64  }
65 
76  public function getDictionary($languageCode)
77  {
78  $languages = [];
79  $this->paths = $this->componentRegistrar->getPaths(ComponentRegistrar::LANGUAGE);
80  foreach ($this->paths as $path) {
81  $directoryRead = $this->directoryReadFactory->create($path);
82  if ($directoryRead->isExist('language.xml')) {
83  $xmlSource = $directoryRead->readFile('language.xml');
84  try {
85  $languageConfig = $this->configFactory->create(['source' => $xmlSource]);
86  } catch (\Magento\Framework\Config\Dom\ValidationException $e) {
87  throw new \Magento\Framework\Exception\LocalizedException(
88  new \Magento\Framework\Phrase(
89  'The XML in file "%1" is invalid:' . "\n%2\nVerify the XML and try again.",
90  [$path . '/language.xml', $e->getMessage()]
91  ),
92  $e
93  );
94  }
95  $this->packList[$languageConfig->getVendor()][$languageConfig->getPackage()] = $languageConfig;
96  if ($languageConfig->getCode() === $languageCode) {
97  $languages[] = $languageConfig;
98  }
99  }
100  }
101 
102  // Collect the inherited packages with meta-information of sorting
103  $packs = [];
104  foreach ($languages as $languageConfig) {
105  $this->collectInheritedPacks($languageConfig, $packs);
106  }
107  uasort($packs, [$this, 'sortInherited']);
108 
109  // Merge all packages of translation to one dictionary
110  $result = [];
111  foreach ($packs as $packInfo) {
113  $languageConfig = $packInfo['language'];
114  $dictionary = $this->readPackCsv($languageConfig->getVendor(), $languageConfig->getPackage());
115  $result = array_merge($result, $dictionary);
116  }
117  return $result;
118  }
119 
131  private function collectInheritedPacks($languageConfig, &$result, $level = 0, array &$visitedPacks = [])
132  {
133  $packKey = implode('|', [$languageConfig->getVendor(), $languageConfig->getPackage()]);
134  if (!isset($visitedPacks[$packKey]) &&
135  (!isset($result[$packKey]) || $result[$packKey]['inheritance_level'] < $level)
136  ) {
137  $visitedPacks[$packKey] = true;
138  $result[$packKey] = [
139  'inheritance_level' => $level,
140  'sort_order' => $languageConfig->getSortOrder(),
141  'language' => $languageConfig,
142  'key' => $packKey,
143  ];
144  foreach ($languageConfig->getUses() as $reuse) {
145  if (isset($this->packList[$reuse['vendor']][$reuse['package']])) {
146  $parentLanguageConfig = $this->packList[$reuse['vendor']][$reuse['package']];
147  $this->collectInheritedPacks($parentLanguageConfig, $result, $level + 1, $visitedPacks);
148  }
149  }
150  }
151  }
152 
163  private function sortInherited($current, $next)
164  {
165  if ($current['inheritance_level'] > $next['inheritance_level']) {
166  return -1;
167  } elseif ($current['inheritance_level'] < $next['inheritance_level']) {
168  return 1;
169  }
170  if ($current['sort_order'] > $next['sort_order']) {
171  return 1;
172  } elseif ($current['sort_order'] < $next['sort_order']) {
173  return -1;
174  }
175  return strcmp($current['key'], $next['key']);
176  }
177 
187  private function readPackCsv($vendor, $package)
188  {
189  $path = $this->componentRegistrar->getPath(ComponentRegistrar::LANGUAGE, strtolower($vendor . '_' . $package));
190  $result = [];
191  if (isset($path)) {
192  $directoryRead = $this->directoryReadFactory->create($path);
193  $foundCsvFiles = $directoryRead->search("*.csv");
194  foreach ($foundCsvFiles as $foundCsvFile) {
195  $file = $directoryRead->openFile($foundCsvFile);
196  while (($row = $file->readCsv()) !== false) {
197  if (is_array($row) && count($row) > 1) {
198  $result[$row[0]] = $row[1];
199  }
200  }
201  }
202  }
203  return $result;
204  }
205 }
$componentRegistrar
Definition: bootstrap.php:23
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$configFactory
Definition: config_data.php:43
__construct(ReadFactory $directoryReadFactory, ComponentRegistrar $componentRegistrar, ConfigFactory $configFactory)
Definition: Dictionary.php:56