12
12
namespace Translation \Bundle \Catalogue \Operation ;
13
13
14
14
use Symfony \Component \Translation \Catalogue \AbstractOperation ;
15
+ use Symfony \Component \Translation \MessageCatalogueInterface ;
15
16
use Symfony \Component \Translation \MetadataAwareInterface ;
16
17
17
18
/**
@@ -36,40 +37,72 @@ protected function processDomain($domain)
36
37
'new ' => [],
37
38
'obsolete ' => [],
38
39
];
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
+ }
40
45
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 ;
42
48
$ 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
+ }
43
70
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 );
47
73
}
48
74
}
49
75
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 ;
53
81
}
54
82
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 );
57
87
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 );
60
91
}
61
92
}
93
+ }
62
94
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 [];
72
102
}
103
+
104
+ /* @var MetadataAwareInterface $catalogue */
105
+ return $ catalogue ->getMetadata ($ key , $ domain );
73
106
}
74
107
75
108
/**
0 commit comments