-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Translator] Dump translation constants as tree instead of simple list #14434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
2.8.0 | ||
----- | ||
|
||
* added options 'as_tree', 'inline' to YamlFileDumper | ||
|
||
2.7.0 | ||
----- | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Symfony\Component\Translation\Dumper; | ||
|
||
use Symfony\Component\Translation\MessageCatalogue; | ||
use Symfony\Component\Translation\Util\ArrayConverter; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be above the YAML component There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was fixed |
||
use Symfony\Component\Yaml\Yaml; | ||
|
||
/** | ||
|
@@ -24,9 +25,19 @@ class YamlFileDumper extends FileDumper | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function format(MessageCatalogue $messages, $domain) | ||
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array()) | ||
{ | ||
return Yaml::dump($messages->all($domain)); | ||
$data = $messages->all($domain); | ||
|
||
if (isset($options['as_tree']) && $options['as_tree']) { | ||
$data = ArrayConverter::expandToTree($data); | ||
} | ||
|
||
if (isset($options['inline']) && ($inline = (int) $options['inline']) > 0) { | ||
return Yaml::dump($data, $inline); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (isset($options['inline'])) {
return Yaml::dump($messages, (int) $inline);
}
return Yaml::dump($messages); |
||
|
||
return Yaml::dump($data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could be simplified: $inline = isset($options['inline']) && $options['inline'] > 0;
return Yaml::dump($data, $inline); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yaml:dump second parameter is integer with default value 2 - "The level where you switch to inline YAML". So, it can't be simplified neither like this nor Yaml::dump($data, isset($options['inline']) ? (int)$options['inline'] : 2); because default value for argument can be changed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops. sorry :) argument name is misleading. |
||
} | ||
|
||
/** | ||
|
@@ -36,4 +47,12 @@ protected function getExtension() | |
{ | ||
return 'yml'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function format(MessageCatalogue $messages, $domain) | ||
{ | ||
return $this->formatCatalogue($messages, $domain); | ||
} | ||
} |
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', | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
foo: | ||
bar1: value1 | ||
bar2: value2 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
foo.bar1: value1 | ||
foo.bar2: value2 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing break line between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
*/ | ||
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); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
array
should bestring[]