@@ -103,7 +103,9 @@ public function getDictionary($languageCode)
103
103
foreach ($ languages as $ languageConfig ) {
104
104
$ this ->collectInheritedPacks ($ languageConfig , $ packs );
105
105
}
106
- uasort ($ packs , [$ this , 'sortInherited ' ]);
106
+
107
+ // Get sorted packs
108
+ $ packs = $ this ->getSortedPacks ($ packs );
107
109
108
110
// Merge all packages of translation to one dictionary
109
111
$ result = [];
@@ -118,6 +120,37 @@ public function getDictionary($languageCode)
118
120
return $ result ;
119
121
}
120
122
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
+
121
154
/**
122
155
* Line up (flatten) a tree of inheritance of language packs
123
156
*
@@ -152,28 +185,42 @@ private function collectInheritedPacks($languageConfig, &$result, $level = 0, ar
152
185
}
153
186
154
187
/**
155
- * Sub-routine for custom sorting packs using inheritance level and sort order
188
+ * Add inherited packs to sorted packs
156
189
*
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)
158
209
*
159
210
* @param array $current
160
211
* @param array $next
212
+ *
161
213
* @return int
162
214
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
163
215
*/
164
- private function sortInherited ($ current , $ next )
216
+ private function sortPacks ($ current , $ next )
165
217
{
166
- if ($ current ['inheritance_level ' ] > $ next ['inheritance_level ' ]) {
167
- return -1 ;
168
- } elseif ($ current ['inheritance_level ' ] < $ next ['inheritance_level ' ]) {
169
- return 1 ;
170
- }
171
218
if ($ current ['sort_order ' ] > $ next ['sort_order ' ]) {
172
- return 1 ;
173
- } elseif ($ current ['sort_order ' ] < $ next ['sort_order ' ]) {
174
219
return -1 ;
220
+ } elseif ($ current ['sort_order ' ] < $ next ['sort_order ' ]) {
221
+ return 1 ;
175
222
}
176
- return strcmp ($ current ['key ' ], $ next ['key ' ]);
223
+ return strcmp ($ next ['key ' ], $ current ['key ' ]);
177
224
}
178
225
179
226
/**
0 commit comments