Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ff03093

Browse files
committed
Fixed ensure languages creation in PoEditor Provider
1 parent 6c84cc8 commit ff03093

File tree

4 files changed

+85
-6
lines changed

4 files changed

+85
-6
lines changed

src/Symfony/Component/Translation/Bridge/Lokalise/LokaliseProvider.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,7 @@ private function getLanguages(): array
313313
$responseContent = $response->toArray(false);
314314

315315
if (\array_key_exists('languages', $responseContent)) {
316-
return array_map(function ($language) {
317-
return $language['lang_iso'];
318-
}, $responseContent['languages']);
316+
return array_column($responseContent['languages'], 'lang_iso');
319317
}
320318

321319
return [];

src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function request(string $method, string $url, array $options = []): Respo
2424
{
2525
if (isset($options['poeditor_credentials'])) {
2626
if ('POST' === $method) {
27-
$options['body'] = $options['poeditor_credentials'] + $options['body'];
27+
$options['body'] = $options['poeditor_credentials'] + ($options['body'] ?? []);
2828
}
2929
unset($options['poeditor_credentials']);
3030
}

src/Symfony/Component/Translation/Bridge/PoEditor/PoEditorProvider.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public function write(TranslatorBagInterface $translatorBag): void
6161
$defaultCatalogue = $translatorBag->getCatalogues()[0];
6262
}
6363

64+
$this->ensureAllLocalesAreCreated($translatorBag);
6465
$terms = $translationsToAdd = [];
66+
6567
foreach ($defaultCatalogue->all() as $domain => $messages) {
6668
foreach ($messages as $id => $message) {
6769
$terms[] = [
@@ -116,7 +118,7 @@ public function read(array $domains, array $locales): TranslatorBag
116118
$responseContent = $response->toArray(false);
117119

118120
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));
120122
continue;
121123
}
122124

@@ -133,7 +135,7 @@ public function read(array $domains, array $locales): TranslatorBag
133135
}
134136

135137
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));
137139
continue;
138140
}
139141

@@ -211,4 +213,50 @@ private function deleteTerms(array $ids): void
211213
throw new ProviderException(sprintf('Unable to delete translation keys on PoEditor: "%s".', $response->getContent(false)), $response);
212214
}
213215
}
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+
}
214262
}

src/Symfony/Component/Translation/Bridge/PoEditor/Tests/PoEditorProviderTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,39 @@ public function testCompleteWriteProcess()
5454
]));
5555

5656
$responses = [
57+
'getLanguages' => function (string $method, string $url, array $options = []): MockResponse {
58+
$this->assertSame('POST', $method);
59+
$this->assertSame(http_build_query([
60+
'api_token' => 'API_KEY',
61+
'id' => 'PROJECT_ID',
62+
]), $options['body']);
63+
64+
return new MockResponse(json_encode([
65+
'response' => [
66+
'status' => 'success',
67+
'code' => '200',
68+
'message' => 'OK',
69+
],
70+
'result' => [
71+
'languages' => [
72+
[
73+
'name' => 'English',
74+
'code' => 'en',
75+
],
76+
],
77+
],
78+
]));
79+
},
80+
'createLanguages' => function (string $method, string $url, array $options = []) use ($successResponse): MockResponse {
81+
$this->assertSame('POST', $method);
82+
$this->assertSame(http_build_query([
83+
'api_token' => 'API_KEY',
84+
'id' => 'PROJECT_ID',
85+
'language' => 'fr',
86+
]), $options['body']);
87+
88+
return $successResponse;
89+
},
5790
'addTerms' => function (string $method, string $url, array $options = []) use ($successResponse): MockResponse {
5891
$this->assertSame('POST', $method);
5992
$this->assertSame(http_build_query([

0 commit comments

Comments
 (0)