Skip to content

Commit fb42f23

Browse files
committed
[Translation] Prevent creating empty keys when key ends with a period
[Translation] create getKeyParts() to keep periods at start or end of the key
1 parent 7aaa92a commit fb42f23

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

src/Symfony/Component/Translation/Tests/Util/ArrayConverterTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,38 @@ public static function messagesData()
6969
],
7070
],
7171
],
72+
[
73+
// input
74+
[
75+
'foo.' => 'foo.',
76+
'bar.' => 'bar.',
77+
'foo bar.' => 'foo bar.',
78+
'.foo bar' => '.foo bar',
79+
'abc 1.abc 2' => 'value',
80+
'bcd 1.bcd 2.' => 'value',
81+
'.cde 1.cde 2.' => 'value',
82+
'.def 1.def 2' => 'value',
83+
],
84+
// expected output
85+
[
86+
'foo.' => 'foo.',
87+
'bar.' => 'bar.',
88+
'foo bar.' => 'foo bar.',
89+
'.foo bar' => '.foo bar',
90+
'abc 1' => [
91+
'abc 2' => 'value',
92+
],
93+
'bcd 1' => [
94+
'bcd 2.' => 'value',
95+
],
96+
'.cde 1' => [
97+
'cde 2.' => 'value',
98+
],
99+
'.def 1' => [
100+
'def 2' => 'value',
101+
],
102+
],
103+
],
72104
];
73105
}
74106
}

src/Symfony/Component/Translation/Util/ArrayConverter.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static function expandToTree(array $messages)
3838
$tree = [];
3939

4040
foreach ($messages as $id => $value) {
41-
$referenceToElement = &self::getElementByPath($tree, explode('.', $id));
41+
$referenceToElement = &self::getElementByPath($tree, self::getKeyParts($id));
4242

4343
$referenceToElement = $value;
4444

@@ -65,6 +65,7 @@ private static function &getElementByPath(array &$tree, array $parts)
6565
$elem = &$elem[implode('.', \array_slice($parts, $i))];
6666
break;
6767
}
68+
6869
$parentOfElem = &$elem;
6970
$elem = &$elem[$part];
7071
}
@@ -96,4 +97,45 @@ private static function cancelExpand(array &$tree, string $prefix, array $node)
9697
}
9798
}
9899
}
100+
101+
private static function getKeyParts(string $key)
102+
{
103+
$parts = explode('.', $key);
104+
$partsCount = count($parts);
105+
106+
$result = [];
107+
$buffer = '';
108+
109+
foreach ($parts as $index => $part) {
110+
if ($index === 0 && $part === '') {
111+
$buffer = '.';
112+
113+
continue;
114+
}
115+
116+
if ($index === $partsCount - 1 && $part === '') {
117+
$buffer .= '.';
118+
$result[] = $buffer;
119+
120+
continue;
121+
}
122+
123+
if (isset($parts[$index + 1]) && $parts[$index + 1] === '') {
124+
$buffer .= $part;
125+
126+
continue;
127+
}
128+
129+
if ($buffer) {
130+
$result[] = $buffer . $part;
131+
$buffer = '';
132+
133+
continue;
134+
}
135+
136+
$result[] = $part;
137+
}
138+
139+
return $result;
140+
}
99141
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy