Skip to content

[Translation] [Bridge] [Lokalise] Fix push keys to lokalise. Closes #… #44474

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

Merged
merged 1 commit into from
Dec 11, 2021
Merged
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
[Translation] [Bridge] [Lokalise] Fix push keys to lokalise. Closes #…
  • Loading branch information
olegmifle authored and fabpot committed Dec 11, 2021
commit f259891b37778f558441eb5d5aaae8ecaa5d3064
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
*/
final class LokaliseProvider implements ProviderInterface
{
private const LOKALISE_GET_KEYS_LIMIT = 5000;

private $client;
private $loader;
private $logger;
Expand Down Expand Up @@ -75,7 +77,7 @@ public function write(TranslatorBagInterface $translatorBag): void
$existingKeysByDomain[$domain] = [];
}

$existingKeysByDomain[$domain] += $this->getKeysIds(array_keys($defaultCatalogue->all($domain)), $domain);
$existingKeysByDomain[$domain] += $this->getKeysIds([], $domain);
}

$keysToCreate = $createdKeysByDomain = [];
Expand Down Expand Up @@ -219,7 +221,7 @@ private function createKeys(array $keys, string $domain): array
* Translations will be created for keys without existing translations.
* Translations will be updated for keys with existing translations.
*/
private function updateTranslations(array $keysByDomain, TranslatorBagInterface $translatorBag)
private function updateTranslations(array $keysByDomain, TranslatorBagInterface $translatorBag): void
{
$keysToUpdate = [];

Expand Down Expand Up @@ -250,43 +252,57 @@ private function updateTranslations(array $keysByDomain, TranslatorBagInterface
}
}

$chunks = array_chunk($keysToUpdate, 500);
$responses = [];

foreach ($chunks as $chunk) {
$responses[] = $this->client->request('PUT', 'keys', [
'json' => ['keys' => $chunk],
]);
}
$response = $this->client->request('PUT', 'keys', [
'json' => ['keys' => $keysToUpdate],
]);

foreach ($responses as $response) {
if (200 !== $response->getStatusCode()) {
$this->logger->error(sprintf('Unable to create/update translations to Lokalise: "%s".', $response->getContent(false)));
}
if (200 !== $response->getStatusCode()) {
$this->logger->error(sprintf('Unable to create/update translations to Lokalise: "%s".', $response->getContent(false)));
}
}

private function getKeysIds(array $keys, string $domain): array
private function getKeysIds(array $keys, string $domain, int $page = 1): array
{
$response = $this->client->request('GET', 'keys', [
'query' => [
'filter_keys' => implode(',', $keys),
'filter_filenames' => $this->getLokaliseFilenameFromDomain($domain),
'limit' => self::LOKALISE_GET_KEYS_LIMIT,
'page' => $page,
],
]);

if (200 !== $response->getStatusCode()) {
$this->logger->error(sprintf('Unable to get keys ids from Lokalise: "%s".', $response->getContent(false)));
}

return array_reduce($response->toArray(false)['keys'], static function ($carry, array $keyItem) {
$carry[$keyItem['key_name']['web']] = $keyItem['key_id'];
$result = [];
$keysFromResponse = $response->toArray(false)['keys'] ?? [];

return $carry;
}, []);
if (\count($keysFromResponse) > 0) {
$result = array_reduce($keysFromResponse, static function ($carry, array $keyItem) {
$carry[$keyItem['key_name']['web']] = $keyItem['key_id'];

return $carry;
}, []);
}

$paginationTotalCount = $response->getHeaders(false)['x-pagination-total-count'] ?? [];
$keysTotalCount = (int) (reset($paginationTotalCount) ?? 0);

if (0 === $keysTotalCount) {
return $result;
}

$pages = ceil($keysTotalCount / self::LOKALISE_GET_KEYS_LIMIT);
if ($page < $pages) {
$result = array_merge($result, $this->getKeysIds($keys, $domain, ++$page));
}

return $result;
}

private function ensureAllLocalesAreCreated(TranslatorBagInterface $translatorBag)
private function ensureAllLocalesAreCreated(TranslatorBagInterface $translatorBag): void
{
$providerLanguages = $this->getLanguages();
$missingLanguages = array_reduce($translatorBag->getCatalogues(), static function ($carry, $catalogue) use ($providerLanguages) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ public function testCompleteWriteProcess()

$getKeysIdsForMessagesDomainResponse = function (string $method, string $url, array $options = []): ResponseInterface {
$expectedQuery = [
'filter_keys' => 'young_dog',
'filter_keys' => '',
'filter_filenames' => 'messages.xliff',
'limit' => 5000,
'page' => 1,
];

$this->assertSame('GET', $method);
Expand All @@ -89,8 +91,10 @@ public function testCompleteWriteProcess()

$getKeysIdsForValidatorsDomainResponse = function (string $method, string $url, array $options = []): ResponseInterface {
$expectedQuery = [
'filter_keys' => 'post.num_comments',
'filter_keys' => '',
'filter_filenames' => 'validators.xliff',
'limit' => 5000,
'page' => 1,
];

$this->assertSame('GET', $method);
Expand Down Expand Up @@ -337,6 +341,8 @@ public function testDeleteProcess()
$expectedQuery = [
'filter_keys' => 'a',
'filter_filenames' => 'messages.xliff',
'limit' => 5000,
'page' => 1,
];

$this->assertSame('GET', $method);
Expand All @@ -355,6 +361,8 @@ public function testDeleteProcess()
$expectedQuery = [
'filter_keys' => 'post.num_comments',
'filter_filenames' => 'validators.xliff',
'limit' => 5000,
'page' => 1,
];

$this->assertSame('GET', $method);
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy