8000 [Translator] Dump translation constants as tree instead of simple list by gepo · Pull Request #14434 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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

Closed
wants to merge 1 commit into from

Conversation

gepo
Copy link
Contributor
@gepo gepo commented Apr 21, 2015
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? no
Fixed tickets
License MIT
Doc PR

Dump translations constants as tree based on '.' character as a delimeter in path.

YamlFileDumper converts constants like these:

'foo.bar1' => 'test1',
'foo.bar2' => 'test2'

to next .yml file:

foo.bar1: 'test1'
foo.bar2: 'test2'

This PR convert the same input to this:

foo:
    bar1: 'test1'
    bar2: 'test2'

Which is more useful if you use translation import/export tools while storing translations in YAML files.

@gepo gepo force-pushed the yaml_tree_dumper branch from 305107d to 98f6407 Compare April 21, 2015 21:44
@@ -36,4 +36,68 @@ protected function getExtension()
{
return 'yml';
}

protected function expandToTree(array $messages)
Copy link
Member

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)

@stof
Copy link
Member
stof commented Apr 21, 2015

I think this should be configurable through an option


foreach ($parts as $i => $part) {
if (! isset($elem[$part])) {
$elem[$part] = [];
Copy link
Contributor

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

@aitboudad
Copy link
Contributor

This look nice addition, what do you think to move this feature into `yaml dumper' ?

@jakzal
Copy link
Contributor
jakzal commented Apr 22, 2015

@aitboudad I wouldn't add this feature to the yaml component as it's not part of the yaml standard.

@gepo
Copy link
Contributor Author
10000
gepo commented Apr 22, 2015

I think this should be configurable through an option

ok, sure, did it

*
* @param bool $asTree
*/
public function setDumpAsTree($asTree)
Copy link
Member

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)

Copy link
Contributor Author

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:

  1. Override dump() function, save options internally and reset them after format() call
  2. Change FileDumper - add optional third parameter to format function and pass it in dump() function.
  3. Copy-paste&edit dump function from FileDumper to YamlFileDumper (bad, very bad).
  4. 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?

Copy link
Contributor

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.

Copy link
Contributor

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

Copy link
Member

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

Copy link
Member

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

Copy link
Contributor Author

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:

  1. cleanup tests
  2. write doc block
  3. update UPGRADE file

Copy link
Member

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)

@gepo
Copy link
Contributor Author
gepo commented Apr 22, 2015

Also add this behavior to json and php dumpers.

8000

{
$data = $messages->all($domain);

if (array_key_exists('as_tree', $options) && $options['as_tree']) {
Copy link
Contributor

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'])) {
}

@gepo gepo force-pushed the yaml_tree_dumper branch from 5854add to 0beb817 Compare April 25, 2015 09:33
@gepo
Copy link
Contributor Author
gepo commented Apr 25, 2015

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)
Copy link
Contributor

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

@gepo gepo force-pushed the yaml_tree_dumper branch from 0beb817 to 3e3f6e2 Compare April 26, 2015 10:11
@@ -36,4 +47,17 @@ protected function getExtension()
{
return 'yml';
}

/**
* This function provided for backward compatibility.
Copy link
Contributor

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}
*/

@gepo gepo force-pushed the yaml_tree_dumper branch 3 times, most recently from ab97816 to f9a6412 Compare April 27, 2015 09:11
@gepo gepo force-pushed the yaml_tree_dumper branch from f9a6412 to 8c1e21e Compare April 27, 2015 09:13
@aitboudad
Copy link
Contributor

👍, ping @symfony/deciders

*
* @param MessageCatalogue $messages
* @param string $domain
* @param array $options
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array should be string[]

@gepo gepo force-pushed the yaml_tree_dumper branch from 8c1e21e to 3c9bca7 Compare April 28, 2015 21:20
@aitboudad aitboudad added the Ready label May 4, 2015
@@ -13,6 +13,7 @@

use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Translation\Util\ArrayConverter;
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was fixed

@gepo gepo force-pushed the yaml_tree_dumper branch from 3c9bca7 to 8f3f744 Compare May 7, 2015 18:53
@xabbuh
Copy link
Member
xabbuh commented May 12, 2015

👍

return Yaml::dump($data, $inline);
}

return Yaml::dump($data);
Copy link
Contributor

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);

Copy link
Contributor Author

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.

Copy link
Contributor

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.

@aitboudad
Copy link
Contributor

I got some conflicts when I attempt to merge into 2.8, @gepo would you mind to fix the conflicts and open new PR for 2.8 branch to make it easy for me :) ?

@jakzal
Copy link
Contributor
jakzal commented 10000 May 13, 2015

@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.

@stof
Copy link
Member
stof commented May 13, 2015

@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.
The branch switching works fine when we rebase during the switching branch, not when the rebase is done previously without changing the target branch.

}
}

/**
* Transforms a domain of a message catalogue to its string representation.
*
* This function is provided for backward compatibility.
Copy link
Member

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.

Copy link
Contributor Author

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?

@jakzal
Copy link
Contributor
jakzal commented May 13, 2015

@stof I didn't mean rebasing to 2.8. It needs rebasing to 2.7. Can be
merged to 2.8.
On Wed, 13 May 2015 at 18:35, Christophe Coevoet notifications@github.com
wrote:

@jakzal https://github.com/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.
The branch switching works fine when we rebase during the switching
branch, not when the rebase is done previously without changing the target
branch.


Reply to this email directly or view it on GitHub
#14434 (comment).

…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'].
@gepo
Copy link
Contributor Author
gepo commented May 13, 2015

create #14630 for 2.8 branch

@aitboudad
Copy link
Contributor

@jakzal it work only if there is not conflict.

@aitboudad
Copy link
Contributor

closed in favor of #14630.

@aitboudad aitboudad closed this May 13, 2015
aitboudad added a commit that referenced this pull request May 20, 2015
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0