Skip to content

Commit

Permalink
#8691: improved language pack inheritance order
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaha committed Jan 16, 2020
1 parent 6b4a82b commit 8ce0822
Showing 1 changed file with 59 additions and 12 deletions.
71 changes: 59 additions & 12 deletions lib/internal/Magento/Framework/App/Language/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public function getDictionary($languageCode)
foreach ($languages as $languageConfig) {
$this->collectInheritedPacks($languageConfig, $packs);
}
uasort($packs, [$this, 'sortInherited']);

// Get sorted packs
$packs = $this->getSortedPacks($packs);

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

/**
* Get sorted packs
*
* First level packs (inheritance_level eq 0) sort by 'sort order' (ascending)
* Inherited packs has the same order as declared in parent config (language.xml)
*
* @param array $allPacks
*
* @return array
*/
private function getSortedPacks($allPacks)
{
// Get first level (inheritance_level) packs and sort by provided sort order (descending)
$firstLevelPacks = array_filter(
$allPacks,
function ($pack) {
return $pack['inheritance_level'] === 0;
}
);
uasort($firstLevelPacks, [$this, 'sortPacks']);

// Add inherited packs
$sortedPacks = [];
foreach ($firstLevelPacks as $pack) {
$this->addInheritedPacks($allPacks, $pack, $sortedPacks);
}

// Reverse array: the first element has the lowest priority, the last one - the highest
return array_reverse($sortedPacks, true);
}

/**
* Line up (flatten) a tree of inheritance of language packs
*
Expand Down Expand Up @@ -152,28 +185,42 @@ private function collectInheritedPacks($languageConfig, &$result, $level = 0, ar
}

/**
* Sub-routine for custom sorting packs using inheritance level and sort order
* Add inherited packs to sorted packs
*
* First sort by inheritance level descending, then by sort order ascending
* @param array $packs
* @param array $pack
* @param array $sortedPacks
*
* @return void
*/
private function addInheritedPacks($packs, $pack, &$sortedPacks)
{
$sortedPacks[$pack['key']] = $pack;
foreach ($pack['language']->getUses() as $reuse) {
$packKey = implode('|', [$reuse['vendor'], $reuse['package']]);
if (isset($packs[$packKey])) {
$this->addInheritedPacks($packs, $packs[$packKey], $sortedPacks);
}
}
}

/**
* Sub-routine for custom sorting packs using sort order (descending)
*
* @param array $current
* @param array $next
*
* @return int
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
*/
private function sortInherited($current, $next)
private function sortPacks($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;
} elseif ($current['sort_order'] < $next['sort_order']) {
return 1;
}
return strcmp($current['key'], $next['key']);
return strcmp($next['key'], $current['key']);
}

/**
Expand Down

0 comments on commit 8ce0822

Please sign in to comment.