Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5b0cf25

Browse filesBrowse 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 [Translation] Simplify test cases by removing blank spaces
1 parent 7aaa92a commit 5b0cf25
Copy full SHA for 5b0cf25

File tree

Expand file treeCollapse file tree

2 files changed

+71
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+71
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/Util/ArrayConverterTest.php
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,34 @@ public static function messagesData()
6969
],
7070
],
7171
],
72+
[
73+
// input
74+
[
75+
'foo.' => 'foo.',
76+
'.bar' => '.bar',
77+
'abc.abc' => 'value',
78+
'bcd.bcd.' => 'value',
79+
'.cde.cde.' => 'value',
80+
'.def.def' => 'value',
81+
],
82+
// expected output
83+
[
84+
'foo.' => 'foo.',
85+
'.bar' => '.bar',
86+
'abc' => [
87+
'abc' => 'value',
88+
],
89+
'bcd' => [
90+
'bcd.' => 'value',
91+
],
92+
'.cde' => [
93+
'cde.' => 'value',
94+
],
95+
'.def' => [
96+
'def' => 'value',
97+
],
98+
],
99+
],
72100
];
73101
}
74102
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Util/ArrayConverter.php
+43-1Lines 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 (0 === $index && '' === $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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.