1212namespace Translation \Bundle \Catalogue \Operation ;
1313
1414use Symfony \Component \Translation \Catalogue \AbstractOperation ;
15+ use Symfony \Component \Translation \MessageCatalogueInterface ;
1516use Symfony \Component \Translation \MetadataAwareInterface ;
1617
1718/**
@@ -36,40 +37,72 @@ protected function processDomain($domain)
3637 'new ' => [],
3738 'obsolete ' => [],
3839 ];
39- $ sourceMessages = $ this ->source ->all ($ domain );
40+ if (defined (sprintf ('%s::INTL_DOMAIN_SUFFIX ' , MessageCatalogueInterface::class))) {
41+ $ intlDomain = $ domain .MessageCatalogueInterface::INTL_DOMAIN_SUFFIX ;
42+ } else {
43+ $ intlDomain = $ domain ;
44+ }
4045
41- foreach ($ this ->target ->all ($ domain ) as $ id => $ message ) {
46+ foreach ($ this ->source ->all ($ domain ) as $ id => $ message ) {
47+ $ messageDomain = $ this ->source ->defines ($ id , $ intlDomain ) ? $ intlDomain : $ domain ;
4248 $ this ->messages [$ domain ]['all ' ][$ id ] = $ message ;
49+ $ this ->result ->add ([$ id => $ message ], $ messageDomain );
50+
51+ if (!$ this ->target ->has ($ id , $ domain )) {
52+ // No merge required
53+ $ this ->messages [$ domain ]['new ' ][$ id ] = $ message ;
54+ $ resultMeta = $ this ->getMetadata ($ this ->source , $ messageDomain , $ id );
55+ } else {
56+ // Merge required
57+ $ resultMeta = null ;
58+ $ sourceMeta = $ this ->getMetadata ($ this ->source , $ messageDomain , $ id );
59+ $ targetMeta = $ this ->getMetadata ($ this ->target , $ this ->target ->defines ($ id , $ intlDomain ) ? $ intlDomain : $ domain , $ id );
60+ if (is_array ($ sourceMeta ) && is_array ($ targetMeta )) {
61+ // We can only merge meta if both is an array
62+ $ resultMeta = $ this ->mergeMetadata ($ sourceMeta , $ targetMeta );
63+ } elseif (!empty ($ sourceMeta )) {
64+ $ resultMeta = $ sourceMeta ;
65+ } else {
66+ // Assert: true === empty($sourceMeta);
67+ $ resultMeta = $ targetMeta ;
68+ }
69+ }
4370
44- // If $id is NOT defined in source.
45- if (!array_key_exists ($ id , $ sourceMessages )) {
46- $ this ->messages [$ domain ]['obsolete ' ][$ id ] = $ message ;
71+ if (!empty ($ resultMeta )) {
72+ $ this ->result ->setMetadata ($ id , $ resultMeta , $ messageDomain );
4773 }
4874 }
4975
50- foreach ($ sourceMessages as $ id => $ message ) {
51- if (!empty ($ message )) {
52- $ this ->messages [$ domain ]['all ' ][$ id ] = $ message ;
76+ foreach ($ this ->target ->all ($ domain ) as $ id => $ message ) {
77+ if ($ this ->result ->has ($ id , $ domain )) {
78+ // We've already merged this
79+ // That message was in source
80+ continue ;
5381 }
5482
55- if (!$ this ->target ->has ($ id , $ domain )) {
56- $ this ->messages [$ domain ]['new ' ][$ id ] = $ message ;
83+ $ messageDomain = $ this ->target ->defines ($ id , $ intlDomain ) ? $ intlDomain : $ domain ;
84+ $ this ->messages [$ domain ]['all ' ][$ id ] = $ message ;
85+ $ this ->messages [$ domain ]['obsolete ' ][$ id ] = $ message ;
86+ $ this ->result ->add ([$ id => $ message ], $ messageDomain );
5787
58- // Make sure to add it to the source if even if empty($message)
59- $ this ->messages [$ domain ]['all ' ][$ id ] = $ message ;
88+ $ resultMeta = $ this ->getMetadata ($ this ->target , $ messageDomain , $ id );
89+ if (!empty ($ resultMeta )) {
90+ $ this ->result ->setMetadata ($ id , $ resultMeta , $ messageDomain );
6091 }
6192 }
93+ }
6294
63- $ this ->result ->add ($ this ->messages [$ domain ]['all ' ], $ domain );
64-
65- $ targetMetadata = $ this ->target instanceof MetadataAwareInterface ? $ this ->target ->getMetadata ('' , $ domain ) : [];
66- $ sourceMetadata = $ this ->source instanceof MetadataAwareInterface ? $ this ->source ->getMetadata ('' , $ domain ) : [];
67- $ resultMetadata = $ this ->mergeMetaData ($ sourceMetadata , $ targetMetadata );
68-
69- // Write back metadata
70- foreach ($ resultMetadata as $ id => $ data ) {
71- $ this ->result ->setMetadata ($ id , $ data , $ domain );
95+ /**
96+ * @return array|null|string|mixed Can return anything..
97+ */
98+ private function getMetadata (MessageCatalogueInterface $ catalogue , string $ domain , string $ key = '' )
99+ {
100+ if (!$ this ->target instanceof MetadataAwareInterface) {
101+ return [];
72102 }
103+
104+ /* @var MetadataAwareInterface $catalogue */
105+ return $ catalogue ->getMetadata ($ key , $ domain );
73106 }
74107
75108 /**
0 commit comments