-
-
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
Conversation
@@ -36,4 +36,68 @@ protected function getExtension() | |||
{ | |||
return 'yml'; | |||
} | |||
|
|||
protected function expandToTree(array $messages) |
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.
methods should be protected (there is no reason to make them extension points yet)
I think this should be configurable through an option |
|
||
foreach ($parts as $i => $part) { | ||
if (! isset($elem[$part])) { | ||
$elem[$part] = []; |
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.
Should be:
$elem[$part] = array();
This look nice addition, what do you think to move this feature into `yaml dumper' ? |
@aitboudad I wouldn't add this feature to the yaml component as it's not part of the yaml standard. |
ok, sure, did it |
* | ||
* @param bool $asTree | ||
*/ | ||
public function setDumpAsTree($asTree) |
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.
This is not how it should work. It should rely on options passed to the dump method (see the interface)
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.
FileDumper doesn't pass this options to format, so their several ways to do it:
- Override dump() function, save options internally and reset them after format() call
- Change FileDumper - add optional third parameter to format function and pass it in dump() function.
- Copy-paste&edit dump function from FileDumper to YamlFileDumper (bad, very bad).
- or leave it as setter function because if we need it dumping as tree we probably need it in whole application, not just in one case.
I think, way 2. is most suitable, but I'm not sure that it's not BC-break. Is this way ok?
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.
I'd prefer 2.
it's not a BC break but it must be documented in the UPGRADE file.
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.
protected function format(MessageCatalogue $messages, $domain, $options = array());
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.
with a typehint on the options argument
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.
I think the BC break is fine by looking at implementations of this outside Symfony: http://packanalyst.com/class?q=Symfony\Component\Translation\Dumper\FileDumper
Drupal and Prezent are extending the YamlFileDumper to implement this feature, meaning their upgrade path will be easy: switch to the core one.
And then, we could even submit PRs ourselves to Sulu and Kdyby to update their implementations
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.
I certantly can't just add third option to format() in FileDumper without BC break. With introducing two new abstract classes I gain full BC, check it pls. Also, I'm not sure about new class names...
todo:
- cleanup tests
- write doc block
- update UPGRADE file
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.
The new abstract class makes the component more complex without any real benefit IMO.
And if you want to make the tree logic reusable, the right way would be to put it in its own class used as a collaborator of the dumper instead of using an abstract parent class. It would be much more reusable (and easier to test as you could test the tree logic on its own btw)
Also add this behavior to json and php dumpers. 8000 |
{ | ||
$data = $messages->all($domain); | ||
|
||
if (array_key_exists('as_tree', $options) && $options['as_tree']) { |
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.
if (isset($options['as_tree'])) {
}
squash all commits into one to easy review. move expandToTree to separate class and use it only in YamlFileDumper |
@@ -9,6 +9,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c | |||
|
|||
* 2.7.0-BETA1 (2015-04-10) | |||
|
|||
* feature #14434 [Translation] add options 'as_tree', 'inline' to YamlFileDumper (gepo) |
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.
you should update CHANGELOG of Translation and the target should be 2.8
@@ -36,4 +47,17 @@ protected function getExtension() | |||
{ | |||
return 'yml'; | |||
} | |||
|
|||
/** | |||
* This function provided for backward compatibility. |
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.
This function is already documented in FileDumper
, so use inheritdoc
/**
* {@inheritdoc}
*/
ab97816
to
f9a6412
Compare
👍, ping @symfony/deciders |
* | ||
* @param MessageCatalogue $messages | ||
* @param string $domain | ||
* @param array $options |
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 be string[]
@@ -13,6 +13,7 @@ | |||
|
|||
use Symfony\Component\Translation\MessageCatalogue; | |||
use Symfony\Component\Yaml\Yaml; | |||
use Symfony\Component\Translation\Util\ArrayConverter; |
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.
Should be above the YAML component use
statement.
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.
It was fixed
👍 |
return Yaml::dump($data, $inline); | ||
} | ||
|
||
return Yaml::dump($data); |
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.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
oops. sorry :) argument name is misleading.
I got some conflicts when I attempt to merge into |
@aitboudad @gepo a rebase would probably work just fine, no need for a new PR. You'll then be able to switch the target branch while merging. |
@jakzal no we can't. After the rebase, the PR would include all 2.8 commits in the diff too, which means that it will totally mess the merging with GH. |
} | ||
} | ||
|
||
/** | ||
* Transforms a domain of a message catalogue to its string representation. | ||
* | ||
* This function is provided for backward compatibility. |
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.
this is not a BC layer, given that it is the one child class should implement now.
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.
change it to
* Override this function in child class if $options is used for message formatting.
it it more correct?
@stof I didn't mean rebasing to 2.8. It needs rebasing to 2.7. Can be
|
…mp messages as tree instead of simple list. Dump messages as tree based on '.' character as a delimeter in path. For example this rray('foo.bar' => 'value') will be converted to array('foo' => array('bar' => 'value')). Correctly process cases like this ['foo.bar' => 'test1', 'foo' => 'test2'].
create #14630 for 2.8 branch |
@jakzal it work only if there is not conflict. |
closed in favor of #14630. |
…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.
Dump translations constants as tree based on '.' character as a delimeter in path.
YamlFileDumper converts constants like these:
to next .yml file:
This PR convert the same input to this:
Which is more useful if you use translation import/export tools while storing translations in YAML files.