10000 [Translator] Dump translation constants as tree instead of simple list · symfony/symfony@98f6407 · GitHub
[go: up one dir, main page]

Skip to content

Commit 98f6407

Browse files
committed
[Translator] Dump translation constants as tree instead of simple list
Dump translations constants as tree based on '.' character as a delimeter in path. Correctly process cases like this ['foo.bar' => 'test1', 'foo' => 'test2'].
1 parent c16930f commit 98f6407

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

src/Symfony/Component/Translation/Dumper/YamlFileDumper.php

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class YamlFileDumper extends FileDumper
2626
*/
2727
protected function format(MessageCatalogue $messages, $domain)
2828
{
29-
return Yaml::dump($messages->all($domain));
29+
return Yaml::dump($this->expandToTree($messages->all($domain)), 10);
3030
}
3131

3232
/**
@@ -36,4 +36,68 @@ protected function getExtension()
3636
{
3737
return 'yml';
3838
}
39+
40+
protected function expandToTree(array $messages)
41+
{
42+
$tree = array();
43+
44+
foreach ($messages as $id => $value) {
45+
$this->expandElement($tree, explode('.', $id), $value);
46+
}
47+
48+
return $tree;
49+
}
50+
51+
protected function expandElement(array & $tree, array $parts, $value)
52+
{
53+
$referenceToElement = & $this->getElementByPath($tree, $parts);
54+
55+
$referenceToElement = $value;
56+
57+
unset($referenceToElement);
58+
}
59+
60+
protected function & getElementByPath(array & $tree, array $parts)
61+
{
62+
$elem = & $tree;
63+
$parentOfElem = null;
64+
65+
foreach ($parts as $i => $part) {
66+
if (! isset($elem[$part])) {
67+
$elem[$part] = [];
68+
} elseif (is_string($elem[$part])) {
69+
$elem = & $elem[ implode('.', array_slice($parts, $i)) ];
70+
break;
71+
}
72+
73+
$parentOfElem = & $elem;
74+
$elem = & $elem[$part];
75+
}
76+
77+
if (is_array($elem) && count($elem) > 0 && $parentOfElem) {
78+
/* Process next case:
79+
* 'foo.bar': 'test1'
80+
* 'foo': 'test2'
81+
*
82+
* `foo` was array = {bar: 'te 10000 st1'} before we found string constant `foo`.
83+
* Cancel treating foo.bar as array and make it string constant too.
84+
*/
85+
$this->cancelExpand($parentOfElem, $part, $elem);
86+
}
87+
88+
return $elem;
89+
}
90+
91+
protected function cancelExpand(array & $tree, $prefix, array $node)
92+
{
93+
$prefix .= '.';
94+
95+
foreach ($node as $id => $value) {
96+
if (is_string($value)) {
97+
$tree[$prefix.$id] = $value;
98+
} else {
99+
$this->cancelExpand($tree, $prefix.$id, $value);
100+
}
101+
}
102+
}
39103
}

src/Symfony/Component/Translation/Tests/Dumper/YamlFileDumperTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,23 @@ class YamlFileDumperTest extends \PHPUnit_Framework_TestCase
1919
public function testDump()
2020
{
2121
$catalogue = new MessageCatalogue('en');
22-
$catalogue->add(array('foo' => 'bar'));
22+
$catalogue->add(
23+
array(
24+
'foo1' => 'bar',
25+
'foo.bar' => 'test1',
26+
'foo2.bar' => 'test2',
27+
'foo2.bar.test' => 'test3',
28+
'foo3.level2.level3.level4' => 'test4',
29+
'foo3.level2.level3' => 'test5',
30+
'foo3.level2' => 'test6',
31+
'foo3.bar' => 'test7',
32+
));
2333

2434
$tempDir = sys_get_temp_dir();
2535
$dumper = new YamlFileDumper();
2636
$dumper->dump($catalogue, array('path' => $tempDir));
2737

28-
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.yml'), file_get_contents($tempDir.'/messages.en.yml'));
38+
$this->assertEquals(file_get_contents(__DIR__.'/../fixtures/messages.yml'), file_get_contents($tempDir.'/messages.en.yml'));
2939

3040
unlink($tempDir.'/messages.en.yml');
3141
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
foo1: bar
2+
foo:
3+
bar: test1
4+
foo2:
5+
bar: test2
6+
bar.test: test3< 4F28 /span>
7+
foo3:
8+
level2: test6
9+
level2.level3: test5
10+
level2.level3.level4: test4
11+
bar: test7

0 commit comments

Comments
 (0)
0