@@ -61,7 +61,9 @@ public function write(TranslatorBagInterface $translatorBag): void
61
61
$ defaultCatalogue = $ translatorBag ->getCatalogues ()[0 ];
62
62
}
63
63
64
+ $ this ->ensureAllLocalesAreCreated ($ translatorBag );
64
65
$ terms = $ translationsToAdd = [];
66
+
65
67
foreach ($ defaultCatalogue ->all () as $ domain => $ messages ) {
66
68
foreach ($ messages as $ id => $ message ) {
67
69
$ terms [] = [
@@ -116,7 +118,7 @@ public function read(array $domains, array $locales): TranslatorBag
116
118
$ responseContent = $ response ->toArray (false );
117
119
118
120
if (200 !== $ response ->getStatusCode () || '200 ' !== (string ) $ responseContent ['response ' ]['code ' ]) {
119
- $ this ->logger ->error ('Unable to read the PoEditor response: ' .$ response ->getContent (false ));
121
+ $ this ->logger ->info ('Unable to read the PoEditor response: ' .$ response ->getContent (false ));
120
122
continue ;
121
123
}
122
124
@@ -133,7 +135,7 @@ public function read(array $domains, array $locales): TranslatorBag
133
135
}
134
136
135
137
if (!$ responseContent ) {
136
- $ this ->logger ->error (sprintf ('The exported file "%s" from PoEditor is empty. ' , $ fileUrl ));
138
+ $ this ->logger ->info (sprintf ('The exported file "%s" from PoEditor is empty. ' , $ fileUrl ));
137
139
continue ;
138
140
}
139
141
@@ -211,4 +213,50 @@ private function deleteTerms(array $ids): void
211
213
throw new ProviderException (sprintf ('Unable to delete translation keys on PoEditor: "%s". ' , $ response ->getContent (false )), $ response );
212
214
}
213
215
}
216
+
217
+ private function ensureAllLocalesAreCreated (TranslatorBagInterface $ translatorBag )
218
+ {
219
+ $ providerLanguages = $ this ->getLanguages ();
220
+ $ missingLanguages = array_reduce ($ translatorBag ->getCatalogues (), function ($ carry , $ catalogue ) use ($ providerLanguages ) {
221
+ if (!\in_array ($ catalogue ->getLocale (), $ providerLanguages )) {
222
+ $ carry [] = $ catalogue ->getLocale ();
223
+ }
224
+
225
+ return $ carry ;
226
+ }, []);
227
+
228
+ if ($ missingLanguages ) {
229
+ $ this ->createLanguages ($ missingLanguages );
230
+ }
231
+ }
232
+
233
+ private function getLanguages (): array
234
+ {
235
+ $ response = $ this ->client ->request ('POST ' , 'languages/list ' );
236
+
237
+ if (200 !== $ response ->getStatusCode () || '200 ' !== (string ) $ response ->toArray (false )['response ' ]['code ' ]) {
238
+ throw new ProviderException (sprintf ('Unable to list languages on PoEditor: "%s". ' , $ response ->getContent (false )), $ response );
239
+ }
240
+
241
+ return array_column ($ response ->toArray (false )['result ' ]['languages ' ], 'code ' );
242
+ }
243
+
244
+ private function createLanguages (array $ languages )
245
+ {
246
+ $ responses = [];
247
+
248
+ foreach ($ languages as $ language ) {
249
+ $ responses [] = $ this ->client ->request ('POST ' , 'languages/add ' , [
250
+ 'body ' => [
251
+ 'language ' => $ language ,
252
+ ],
253
+ ]);
254
+ }
255
+
256
+ foreach ($ responses as $ response ) {
257
+ if (200 !== $ response ->getStatusCode () || '200 ' !== (string ) $ response ->toArray (false )['response ' ]['code ' ]) {
258
+ $ this ->logger ->error (sprintf ('Unable to add new language to PoEditor: (status code: "%s") "%s". ' , $ response ->getStatusCode (), $ response ->getContent (false )));
259
+ }
260
+ }
261
+ }
214
262
}
0 commit comments