Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #14630 [Translator] Dump translation constants as tree instea…
…d of simple list (gepo) This PR was merged into the 2.8 branch. Discussion ---------- [Translator] Dump translation constants as tree instead of simple list | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | no | Fixed tickets | | License | MIT | Doc PR | PR #14434 for 2.8 branch Commits ------- 29ec5ca [Translation] add options 'as_tree', 'inline' to YamlFileDumper to dump messages as tree instead of simple list.
- Loading branch information
Showing
8 changed files
with
241 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/Symfony/Component/Translation/Tests/Util/ArrayConverterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation\Tests\Util; | ||
|
||
use Symfony\Component\Translation\Util\ArrayConverter; | ||
|
||
class ArrayConverterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider messsagesData | ||
*/ | ||
public function testDump($input, $expectedOutput) | ||
{ | ||
$this->assertEquals($expectedOutput, ArrayConverter::expandToTree($input)); | ||
} | ||
|
||
public function messsagesData() | ||
{ | ||
return array( | ||
array( | ||
// input | ||
array( | ||
'foo1' => 'bar', | ||
'foo.bar' => 'value', | ||
), | ||
// expected output | ||
array( | ||
'foo1' => 'bar', | ||
'foo' => array('bar' => 'value'), | ||
), | ||
), | ||
array( | ||
// input | ||
array( | ||
'foo.bar' => 'value1', | ||
'foo.bar.test' => 'value2', | ||
), | ||
// expected output | ||
array( | ||
'foo' => array( | ||
'bar' => 'value1', | ||
'bar.test' => 'value2', | ||
), | ||
), | ||
), | ||
array( | ||
// input | ||
array( | ||
'foo.level2.level3.level4' => 'value1', | ||
'foo.level2' => 'value2', | ||
'foo.bar' => 'value3', | ||
), | ||
// expected output | ||
array( | ||
'foo' => array( | ||
'level2' => 'value2', | ||
'level2.level3.level4' => 'value1', | ||
'bar' => 'value3', | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/Symfony/Component/Translation/Tests/fixtures/messages.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
foo: | ||
bar1: value1 | ||
bar2: value2 |
2 changes: 2 additions & 0 deletions
2
src/Symfony/Component/Translation/Tests/fixtures/messages_linear.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
foo.bar1: value1 | ||
foo.bar2: value2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation\Util; | ||
|
||
/** | ||
* ArrayConverter generates tree like structure from a message catalogue. | ||
* e.g. this | ||
* 'foo.bar1' => 'test1', | ||
* 'foo.bar2' => 'test2' | ||
* converts to follows: | ||
* foo: | ||
* bar1: test1 | ||
* bar2: test2. | ||
* | ||
* @author Gennady Telegin <gtelegin@gmail.com> | ||
*/ | ||
class ArrayConverter | ||
{ | ||
/** | ||
* Converts linear messages array to tree-like array. | ||
* For example this rray('foo.bar' => 'value') will be converted to array('foo' => array('bar' => 'value')). | ||
* | ||
* @param array $messages Linear messages array | ||
* | ||
* @return array Tree-like messages array | ||
*/ | ||
public static function expandToTree(array $messages) | ||
{ | ||
$tree = array(); | ||
|
||
foreach ($messages as $id => $value) { | ||
$referenceToElement = &self::getElementByPath($tree, explode('.', $id)); | ||
|
||
$referenceToElement = $value; | ||
|
||
unset($referenceToElement); | ||
} | ||
|
||
return $tree; | ||
} | ||
|
||
private static function &getElementByPath(array &$tree, array $parts) | ||
{ | ||
$elem = &$tree; | ||
$parentOfElem = null; | ||
|
||
foreach ($parts as $i => $part) { | ||
if (isset($elem[$part]) && is_string($elem[$part])) { | ||
/* Process next case: | ||
* 'foo': 'test1', | ||
* 'foo.bar': 'test2' | ||
* | ||
* $tree['foo'] was string before we found array {bar: test2}. | ||
* Treat new element as string too, e.g. add $tree['foo.bar'] = 'test2'; | ||
*/ | ||
$elem = &$elem[ implode('.', array_slice($parts, $i)) ]; | ||
break; | ||
} | ||
$parentOfElem = &$elem; | ||
$elem = &$elem[$part]; | ||
} | ||
|
||
if (is_array($elem) && count($elem) > 0 && $parentOfElem) { | ||
/* Process next case: | ||
* 'foo.bar': 'test1' | ||
* 'foo': 'test2' | ||
* | ||
* $tree['foo'] was array = {bar: 'test1'} before we found string constant `foo`. | ||
* Cancel treating $tree['foo'] as array and cancel back it expansion, | ||
* e.g. make it $tree['foo.bar'] = 'test1' again. | ||
*/ | ||
self::cancelExpand($parentOfElem, $part, $elem); | ||
} | ||
|
||
return $elem; | ||
} | ||
|
||
private static function cancelExpand(array &$tree, $prefix, array $node) | ||
{ | ||
$prefix .= '.'; | ||
|
||
foreach ($node as $id => $value) { | ||
if (is_string($value)) { | ||
$tree[$prefix.$id] = $value; | ||
} else { | ||
self::cancelExpand($tree, $prefix.$id, $value); | ||
} | ||
} | ||
} | ||
} |