Skip to content
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

Translations::mergeWith modifies source translations #152

Closed
lxg opened this issue Jun 29, 2017 · 3 comments
Closed

Translations::mergeWith modifies source translations #152

lxg opened this issue Jun 29, 2017 · 3 comments

Comments

@lxg
Copy link
Contributor

lxg commented Jun 29, 2017

When merging two Translations objects using the mergeWith method, the source catalog is modified in some situations. This is unexpected and undesired behaviour in my opinion.

The problem is in line 164 of Merge.php:

The Translation object from the source catalog is added to the target catalog and is now referenced in the target catalog. But modifications of the target catalog will now also modify the source catalog.

This is undesired for example in situations where a source catalog is generated by an extractor and is used to be merged with several other catalogs (in my case: target catalogs for different languages).

The solution is quite simple. Instead of referencing the source translation, create a clone:

diff --git a/src/Merge.php b/src/Merge.php
--- a/src/Merge.php
+++ b/src/Merge.php
@@ -161,7 +161,7 @@ class Merge
             if (($existing = $to->find($entry))) {
                 $existing->mergeWith($entry);
             } elseif ($options & self::ADD) {
-                $to[] = $entry;
+                $to[] = $entry->getClone();
             }
         }
     }
@oscarotero
Copy link
Member

Yes, it's reasonable. Do you like to work in a PR?
Thanks

@lxg
Copy link
Contributor Author

lxg commented Jun 29, 2017

Sure, here we go: #153

@oscarotero
Copy link
Member

Merged. thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants