@@ -59,30 +59,23 @@ public function write(TranslatorBagInterface $translatorBag): void
59
59
$ catalogue = $ translatorBag ->getCatalogues ()[0 ];
60
60
}
61
61
62
- // Create keys on Loco
63
62
foreach ($ catalogue ->all () as $ domain => $ messages ) {
64
- $ ids = [];
65
- foreach ($ messages as $ id => $ message ) {
66
- $ ids [] = $ id ;
67
- $ this ->createAsset ($ id );
68
- }
69
- if ($ ids ) {
70
- $ this ->tagsAssets ($ ids , $ domain );
63
+ $ createdIds = $ this ->createAssets (array_keys ($ messages ));
64
+ if ($ createdIds ) {
65
+ $ this ->tagsAssets ($ createdIds , $ domain );
71
66
}
72
67
}
73
68
74
- // Push translations in all locales and tag them with domain
75
69
foreach ($ translatorBag ->getCatalogues () as $ catalogue ) {
76
70
$ locale = $ catalogue ->getLocale ();
77
71
78
72
if (!\in_array ($ locale , $ this ->getLocales ())) {
79
73
$ this ->createLocale ($ locale );
80
74
}
81
75
82
- foreach ($ catalogue ->all () as $ messages ) {
83
- foreach ($ messages as $ id => $ message ) {
84
- $ this ->translateAsset ($ id , $ message , $ locale );
85
- }
76
+ foreach ($ catalogue ->all () as $ domain => $ messages ) {
77
+ $ ids = $ this ->getAssetsIds ($ domain );
78
+ $ this ->translateAssets (array_combine ($ ids , array_values ($ messages )), $ locale );
86
79
}
87
80
}
88
81
}
@@ -91,92 +84,142 @@ public function read(array $domains, array $locales): TranslatorBag
91
84
{
92
85
$ domains = $ domains ?: ['* ' ];
93
86
$ translatorBag = new TranslatorBag ();
87
+ $ responses = [];
94
88
95
89
foreach ($ locales as $ locale ) {
96
90
foreach ($ domains as $ domain ) {
97
- $ response = $ this ->client ->request ('GET ' , sprintf ('export/locale/%s.xlf?filter=%s&status=translated ' , $ locale , $ domain ));
91
+ $ responses [] = [
92
+ 'response ' => $ this ->client ->request ('GET ' , sprintf ('export/locale/%s.xlf ' , rawurlencode ($ locale )), [
93
+ 'query ' => [
94
+ 'filter ' => $ domain ,
95
+ 'status ' => 'translated ' ,
96
+ ],
97
+ ]),
98
+ 'locale ' => $ locale ,
99
+ 'domain ' => $ domain ,
100
+ ];
101
+ }
102
+ }
98
103
99
- if ( 404 === $ response-> getStatusCode () ) {
100
- $ this -> logger -> error ( sprintf ( ' Locale "%s" for domain "%s" does not exist in Loco. ' , $ locale, $ domain )) ;
101
- continue ;
102
- }
104
+ foreach ( $ responses as $ response ) {
105
+ $ locale = $ response [ ' locale ' ] ;
106
+ $ domain = $ response [ ' domain ' ] ;
107
+ $ response = $ response [ ' response ' ];
103
108
104
- $ responseContent = $ response ->getContent (false );
109
+ if (404 === $ response ->getStatusCode ()) {
110
+ $ this ->logger ->warning (sprintf ('Locale "%s" for domain "%s" does not exist in Loco. ' , $ locale , $ domain ));
111
+ continue ;
112
+ }
105
113
106
- if (200 !== $ response ->getStatusCode ()) {
107
- throw new ProviderException ('Unable to read the Loco response: ' .$ responseContent , $ response );
108
- }
114
+ $ responseContent = $ response ->getContent (false );
109
115
110
- $ translatorBag ->addCatalogue ($ this ->loader ->load ($ responseContent , $ locale , $ domain ));
116
+ if (200 !== $ response ->getStatusCode ()) {
117
+ throw new ProviderException ('Unable to read the Loco response: ' .$ responseContent , $ response );
111
118
}
119
+
120
+ $ translatorBag ->addCatalogue ($ this ->loader ->load ($ responseContent , $ locale , $ domain ));
112
121
}
113
122
114
123
return $ translatorBag ;
115
124
}
116
125
117
126
public function delete (TranslatorBagInterface $ translatorBag ): void
118
127
{
119
- $ deletedIds = [] ;
128
+ $ catalogue = $ translatorBag -> getCatalogue ( $ this -> defaultLocale ) ;
120
129
121
- foreach ($ translatorBag ->getCatalogues () as $ catalogue ) {
122
- foreach ($ catalogue ->all () as $ messages ) {
123
- foreach ($ messages as $ id => $ message ) {
124
- if (\in_array ($ id , $ deletedIds , true )) {
125
- continue ;
126
- }
127
-
128
- $ this ->deleteAsset ($ id );
129
- $ deletedIds [] = $ id ;
130
- }
130
+ if (!$ catalogue ) {
131
+ $ catalogue = $ translatorBag ->getCatalogues ()[0 ];
132
+ }
133
+
134
+ $ responses = [];
135
+
136
+ foreach (array_keys ($ catalogue ->all ()) as $ domain ) {
137
+ foreach ($ this ->getAssetsIds ($ domain ) as $ id ) {
138
+ $ responses [$ id ] = $ this ->client ->request ('DELETE ' , sprintf ('assets/%s.json ' , $ id ));
139
+ }
140
+ }
141
+
142
+ foreach ($ responses as $ key => $ response ) {
143
+ if (403 === $ response ->getStatusCode ()) {
144
+ $ this ->logger ->error ('The API key used does not have sufficient permissions to delete assets. ' );
145
+ }
146
+
147
+ if (200 !== $ response ->getStatusCode () && 404 !== $ response ->getStatusCode ()) {
148
+ $ this ->logger ->error (sprintf ('Unable to delete translation key "%s" to Loco: "%s". ' , $ key , $ response ->getContent (false )));
131
149
}
132
150
}
133
151
}
134
152
135
- private function createAsset (string $ id ): void
153
+ /**
154
+ * Returns array of internal Loco's unique ids.
155
+ */
156
+ private function getAssetsIds (string $ domain ): array
136
157
{
137
- $ response = $ this ->client ->request ('POST ' , 'assets ' , [
138
- 'body ' => [
139
- 'name ' => $ id ,
140
- 'id ' => $ id ,
141
- 'type ' => 'text ' ,
142
- 'default ' => 'untranslated ' ,
143
- ],
144
- ]);
158
+ $ response = $ this ->client ->request ('GET ' , 'assets ' , ['query ' => ['filter ' => $ domain ]]);
145
159
146
- if (409 === $ response ->getStatusCode ()) {
147
- $ this ->logger ->info (sprintf ('Translation key "%s" already exists in Loco. ' , $ id ), [
148
- 'id ' => $ id ,
160
+ if (200 !== $ response ->getStatusCode ()) {
161
+ $ this ->logger ->error (sprintf ('Unable to get assets from Loco: "%s". ' , $ response ->getContent (false )));
162
+ }
163
+
164
+ return array_map (function ($ asset ) {
165
+ return $ asset ['id ' ];
166
+ }, $ response ->toArray (false ));
167
+ }
168
+
169
+ private function createAssets (array $ keys ): array
170
+ {
171
+ $ responses = $ createdIds = [];
172
+
173
+ foreach ($ keys as $ key ) {
174
+ $ responses [$ key ] = $ this ->client ->request ('POST ' , 'assets ' , [
175
+ 'body ' => [
176
+ 'text ' => $ key ,
177
+ 'type ' => 'text ' ,
178
+ 'default ' => 'untranslated ' ,
179
+ ],
149
180
]);
150
- } elseif (201 !== $ response ->getStatusCode ()) {
151
- $ this ->logger ->error (sprintf ('Unable to add new translation key "%s" to Loco: (status code: "%s") "%s". ' , $ id , $ response ->getStatusCode (), $ response ->getContent (false )));
152
181
}
182
+
183
+ foreach ($ responses as $ key => $ response ) {
184
+ if (201 !== $ response ->getStatusCode ()) {
185
+ $ this ->logger ->error (sprintf ('Unable to add new translation key "%s" to Loco: (status code: "%s") "%s". ' , $ key , $ response ->getStatusCode (), $ response ->getContent (false )));
186
+ } else {
187
+ $ createdIds [] = $ response ->toArray (false )['id ' ];
188
+ }
189
+ }
190
+
191
+ return $ createdIds ;
153
192
}
154
193
155
- private function translateAsset ( string $ id , string $ message , string $ locale ): void
194
+ private function translateAssets ( array $ translations , string $ locale ): void
156
195
{
157
- $ response = $ this ->client ->request ('POST ' , sprintf ('translations/%s/%s ' , $ id , $ locale ), [
158
- 'body ' => $ message ,
159
- ]);
196
+ $ responses = [];
160
197
161
- if (200 !== $ response ->getStatusCode ()) {
162
- $ this ->logger ->error (sprintf ('Unable to add translation message "%s" (for key: "%s" in locale "%s") to Loco: "%s". ' , $ message , $ id , $ locale , $ response ->getContent (false )));
198
+ foreach ($ translations as $ id => $ message ) {
199
+ $ responses [$ id ] = $ this ->client ->request ('POST ' , sprintf ('translations/%s/%s ' , $ id , $ locale ), [
200
+ 'body ' => $ message ,
201
+ ]);
202
+ }
203
+
204
+ foreach ($ responses as $ id => $ response ) {
205
+ if (200 !== $ response ->getStatusCode ()) {
206
+ $ this ->logger ->error (sprintf ('Unable to add translation for key "%s" in locale "%s" to Loco: "%s". ' , $ id , $ locale , $ response ->getContent (false )));
207
+ }
163
208
}
164
209
}
165
210
166
211
private function tagsAssets (array $ ids , string $ tag ): void
167
212
{
168
- $ idsAsString = implode (', ' , array_unique ($ ids ));
169
-
170
213
if (!\in_array ($ tag , $ this ->getTags (), true )) {
171
214
$ this ->createTag ($ tag );
172
215
}
173
216
174
217
$ response = $ this ->client ->request ('POST ' , sprintf ('tags/%s.json ' , $ tag ), [
175
- 'body ' => $ idsAsString ,
218
+ 'body ' => implode ( ' , ' , $ ids ) ,
176
219
]);
177
220
178
221
if (200 !== $ response ->getStatusCode ()) {
179
- $ this ->logger ->error (sprintf ('Unable to add tag "%s" on translation keys "%s" to Loco: "%s". ' , $ tag, $ idsAsString , $ response ->getContent (false )));
222
+ $ this ->logger ->error (sprintf ('Unable to tag assets with "%s" on Loco: "%s". ' , $ tag , $ response ->getContent (false )));
180
223
}
181
224
}
182
225
@@ -233,13 +276,4 @@ private function getLocales(): array
233
276
return $ carry ;
234
277
}, []);
235
278
}
236
-
237
- private function deleteAsset (string $ id ): void
238
- {
239
- $ response = $ this ->client ->request ('DELETE ' , sprintf ('assets/%s.json ' , $ id ));
240
-
241
- if (200 !== $ response ->getStatusCode ()) {
242
- $ this ->logger ->error (sprintf ('Unable to delete translation key "%s" to Loco: "%s". ' , $ id , $ response ->getContent (false )));
243
- }
244
- }
245
279
}
0 commit comments