8000 bug #52005 [Translation] Prevent creating empty keys when key ends wi… · symfony/symfony@4825733 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4825733

Browse files
bug #52005 [Translation] Prevent creating empty keys when key ends with a period (javleds)
This PR was merged into the 5.4 branch. Discussion ---------- [Translation] Prevent creating empty keys when key ends with a period | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #51536 | License | MIT Small fix that prevents creating empty keys on translations when the translation key ends with a period, example: ```php 'This is a validation.' ``` Before the fix, the yaml output was: ```yaml 'This is a validation': '': 'This is a validation.' ``` After the fix we got: ```yaml 'This is a validation.': 'This is a validation.' ``` This can be replicated by: 1. Create a new symfony project using the `--webapp` flag. 2. Execute the command to extract translations as yaml: `php bin/console translation:extract --force --format=yaml --as-tree=3 --prefix='' --clean en` Results: Before the fix: <img width="1027" alt="image" src="https://github.com/symfony/symfony/assets/11241239/70ffbc54-30da-4f7e-88cc-e19bbd7980e1"> After the fix: <img width="1229" alt="image" src="https://github.com/symfony/symfony/assets/11241239/6d26e701-80f9-4ac5-aff9-7e2c78c162f6"> Commits ------- 5b0cf25 [Translation] Prevent creating empty keys when key ends with a period
2 parents 3f4228c + 5b0cf25 commit 4825733

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

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

Lines 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

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 (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