minor #31661 [Translation] refactor ArrayLoader::flatten (azjezz)
This PR was squashed before being merged into the 4.4 branch (closes #31661).
Discussion
----------
[Translation] refactor ArrayLoader::flatten
| Q | A
| ------------- | ---
| Branch? | 4.4
| Bug fix? | no
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| License | MIT
Commits
-------
5b0544691e
[Translation] refactor ArrayLoader::flatten
This commit is contained in:
commit
05d6d31794
@ -25,7 +25,7 @@ class ArrayLoader implements LoaderInterface
|
||||
*/
|
||||
public function load($resource, $locale, $domain = 'messages')
|
||||
{
|
||||
$this->flatten($resource);
|
||||
$resource = $this->flatten($resource);
|
||||
$catalogue = new MessageCatalogue($locale);
|
||||
$catalogue->add($resource, $domain);
|
||||
|
||||
@ -39,28 +39,20 @@ class ArrayLoader implements LoaderInterface
|
||||
* 'key' => ['key2' => ['key3' => 'value']]
|
||||
* Becomes:
|
||||
* 'key.key2.key3' => 'value'
|
||||
*
|
||||
* This function takes an array by reference and will modify it
|
||||
*
|
||||
* @param array &$messages The array that will be flattened
|
||||
* @param array $subnode Current subnode being parsed, used internally for recursive calls
|
||||
* @param string $path Current path being parsed, used internally for recursive calls
|
||||
*/
|
||||
private function flatten(array &$messages, array $subnode = null, $path = null)
|
||||
private function flatten(array $messages): array
|
||||
{
|
||||
if (null === $subnode) {
|
||||
$subnode = &$messages;
|
||||
}
|
||||
foreach ($subnode as $key => $value) {
|
||||
$result = [];
|
||||
foreach ($messages as $key => $value) {
|
||||
if (\is_array($value)) {
|
||||
$nodePath = $path ? $path.'.'.$key : $key;
|
||||
$this->flatten($messages, $value, $nodePath);
|
||||
if (null === $path) {
|
||||
unset($messages[$key]);
|
||||
foreach ($this->flatten($value) as $k => $v) {
|
||||
$result[$key.'.'.$k] = $v;
|
||||
}
|
||||
} elseif (null !== $path) {
|
||||
$messages[$path.'.'.$key] = $value;
|
||||
} else {
|
||||
$result[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user