Skip to content

Commit 8ce0822

Browse files
author
Vaha
committed
#8691: improved language pack inheritance order
1 parent 6b4a82b commit 8ce0822

File tree

1 file changed

+59
-12
lines changed

1 file changed

+59
-12
lines changed

lib/internal/Magento/Framework/App/Language/Dictionary.php

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ public function getDictionary($languageCode)
103103
foreach ($languages as $languageConfig) {
104104
$this->collectInheritedPacks($languageConfig, $packs);
105105
}
106-
uasort($packs, [$this, 'sortInherited']);
106+
107+
// Get sorted packs
108+
$packs = $this->getSortedPacks($packs);
107109

108110
// Merge all packages of translation to one dictionary
109111
$result = [];
@@ -118,6 +120,37 @@ public function getDictionary($languageCode)
118120
return $result;
119121
}
120122

123+
/**
124+
* Get sorted packs
125+
*
126+
* First level packs (inheritance_level eq 0) sort by 'sort order' (ascending)
127+
* Inherited packs has the same order as declared in parent config (language.xml)
128+
*
129+
* @param array $allPacks
130+
*
131+
* @return array
132+
*/
133+
private function getSortedPacks($allPacks)
134+
{
135+
// Get first level (inheritance_level) packs and sort by provided sort order (descending)
136+
$firstLevelPacks = array_filter(
137+
$allPacks,
138+
function ($pack) {
139+
return $pack['inheritance_level'] === 0;
140+
}
141+
);
142+
uasort($firstLevelPacks, [$this, 'sortPacks']);
143+
144+
// Add inherited packs
145+
$sortedPacks = [];
146+
foreach ($firstLevelPacks as $pack) {
147+
$this->addInheritedPacks($allPacks, $pack, $sortedPacks);
148+
}
149+
150+
// Reverse array: the first element has the lowest priority, the last one - the highest
151+
return array_reverse($sortedPacks, true);
152+
}
153+
121154
/**
122155
* Line up (flatten) a tree of inheritance of language packs
123156
*
@@ -152,28 +185,42 @@ private function collectInheritedPacks($languageConfig, &$result, $level = 0, ar
152185
}
153186

154187
/**
155-
* Sub-routine for custom sorting packs using inheritance level and sort order
188+
* Add inherited packs to sorted packs
156189
*
157-
* First sort by inheritance level descending, then by sort order ascending
190+
* @param array $packs
191+
* @param array $pack
192+
* @param array $sortedPacks
193+
*
194+
* @return void
195+
*/
196+
private function addInheritedPacks($packs, $pack, &$sortedPacks)
197+
{
198+
$sortedPacks[$pack['key']] = $pack;
199+
foreach ($pack['language']->getUses() as $reuse) {
200+
$packKey = implode('|', [$reuse['vendor'], $reuse['package']]);
201+
if (isset($packs[$packKey])) {
202+
$this->addInheritedPacks($packs, $packs[$packKey], $sortedPacks);
203+
}
204+
}
205+
}
206+
207+
/**
208+
* Sub-routine for custom sorting packs using sort order (descending)
158209
*
159210
* @param array $current
160211
* @param array $next
212+
*
161213
* @return int
162214
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
163215
*/
164-
private function sortInherited($current, $next)
216+
private function sortPacks($current, $next)
165217
{
166-
if ($current['inheritance_level'] > $next['inheritance_level']) {
167-
return -1;
168-
} elseif ($current['inheritance_level'] < $next['inheritance_level']) {
169-
return 1;
170-
}
171218
if ($current['sort_order'] > $next['sort_order']) {
172-
return 1;
173-
} elseif ($current['sort_order'] < $next['sort_order']) {
174219
return -1;
220+
} elseif ($current['sort_order'] < $next['sort_order']) {
221+
return 1;
175222
}
176-
return strcmp($current['key'], $next['key']);
223+
return strcmp($next['key'], $current['key']);
177224
}
178225

179226
/**

0 commit comments

Comments
 (0)