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

Skip to content

[Translation] [PoEditor] Fixed languages creation #41438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,7 @@ private function getLanguages(): array
$responseContent = $response->toArray(false);

if (\array_key_exists('languages', $responseContent)) {
return array_map(function ($language) {
return $language['lang_iso'];
}, $responseContent['languages']);
return array_column($responseContent['languages'], 'lang_iso');
}

return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function request(string $method, string $url, array $options = []): Respo
{
if (isset($options['poeditor_credentials'])) {
if ('POST' === $method) {
$options['body'] = $options['poeditor_credentials'] + $options['body'];
$options['body'] = $options['poeditor_credentials'] + ($options['body'] ?? []);
}
unset($options['poeditor_credentials']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public function write(TranslatorBagInterface $translatorBag): void
$defaultCatalogue = $translatorBag->getCatalogues()[0];
}

$this->ensureAllLocalesAreCreated($translatorBag);
$terms = $translationsToAdd = [];

foreach ($defaultCatalogue->all() as $domain => $messages) {
foreach ($messages as $id => $message) {
$terms[] = [
Expand Down Expand Up @@ -116,7 +118,7 @@ public function read(array $domains, array $locales): TranslatorBag
$responseContent = $response->toArray(false);

if (200 !== $response->getStatusCode() || '200' !== (string) $responseContent['response']['code']) {
$this->logger->error('Unable to read the PoEditor response: '.$response->getContent(false));
$this->logger->info('Unable to read the PoEditor response: '.$response->getContent(false));
continue;
}

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

if (!$responseContent) {
$this->logger->error(sprintf('The exported file "%s" from PoEditor is empty.', $fileUrl));
$this->logger->info(sprintf('The exported file "%s" from PoEditor is empty.', $fileUrl));
continue;
}

Expand Down Expand Up @@ -211,4 +213,50 @@ private function deleteTerms(array $ids): void
throw new ProviderException(sprintf('Unable to delete translation keys on PoEditor: "%s".', $response->getContent(false)), $response);
}
}

private function ensureAllLocalesAreCreated(TranslatorBagInterface $translatorBag)
{
$providerLanguages = $this->getLanguages();
$missingLanguages = array_reduce($translatorBag->getCatalogues(), function ($carry, $catalogue) use ($providerLanguages) {
if (!\in_array($catalogue->getLocale(), $providerLanguages)) {
$carry[] = $catalogue->getLocale();
}

return $carry;
}, []);

if ($missingLanguages) {
$this->createLanguages($missingLanguages);
}
}

private function getLanguages(): array
{
$response = $this->client->request('POST', 'languages/list');

if (200 !== $response->getStatusCode() || '200' !== (string) $response->toArray(false)['response']['code']) {
throw new ProviderException(sprintf('Unable to list languages on PoEditor: "%s".', $response->getContent(false)), $response);
}

return array_column($response->toArray(false)['result']['languages'], 'code');
}

private function createLanguages(array $languages)
{
$responses = [];

foreach ($languages as $language) {
$responses[] = $this->client->request('POST', 'languages/add', [
'body' => [
'language' => $language,
],
]);
}

foreach ($responses as $response) {
if (200 !== $response->getStatusCode() || '200' !== (string) $response->toArray(false)['response']['code']) {
$this->logger->error(sprintf('Unable to add new language to PoEditor: (status code: "%s") "%s".', $response->getStatusCode(), $response->getContent(false)));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,39 @@ public function testCompleteWriteProcess()
]));

$responses = [
'getLanguages' => function (string $method, string $url, array $options = []): MockResponse {
$this->assertSame('POST', $method);
$this->assertSame(http_build_query([
'api_token' => 'API_KEY',
'id' => 'PROJECT_ID',
]), $options['body']);

return new MockResponse(json_encode([
'response' => [
'status' => 'success',
'code' => '200',
'message' => 'OK',
],
'result' => [
'languages' => [
[
'name' => 'English',
'code' => 'en',
],
],
],
]));
},
'createLanguages' => function (string $method, string $url, array $options = []) use ($successResponse): MockResponse {
$this->assertSame('POST', $method);
$this->assertSame(http_build_query([
'api_token' => 'API_KEY',
'id' => 'PROJECT_ID',
'language' => 'fr',
]), $options['body']);

return $successResponse;
},
'addTerms' => function (string $method, string $url, array $options = []) use ($successResponse): MockResponse {
$this->assertSame('POST', $method);
$this->assertSame(http_build_query([
Expand Down