Skip to content

Commit 3b6cc05

Browse files
committed
bug symfony#44474 [Translation] [Bridge] [Lokalise] Fix push keys to lokalise. Closes #… (olegmifle)
This PR was squashed before being merged into the 5.3 branch. Discussion ---------- [Translation] [Bridge] [Lokalise] Fix push keys to lokalise. Closes #… | Q | A | ------------- | --- | Branch? | 5.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix symfony#44444 | License | MIT | Doc PR | N/A Fixed issue in symfony#44444 within fix some other problems e.g. * localise GET method "keys" return only 100 keys per request by default, max 5000. Fixed it and get all the available keys. * localise recommend update\create only 500 keys per request but service refuse another chank with error "Your token is currently used to process another request. We do not support concurrent requests.". So I send all keys in one request and it works fine. * add return types to some methods Commits ------- f259891 [Translation] [Bridge] [Lokalise] Fix push keys to lokalise. Closes #…
2 parents 1e50d49 + f259891 commit 3b6cc05

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

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

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
*/
3333
final class LokaliseProvider implements ProviderInterface
3434
{
35+
private const LOKALISE_GET_KEYS_LIMIT = 5000;
36+
3537
private $client;
3638
private $loader;
3739
private $logger;
@@ -75,7 +77,7 @@ public function write(TranslatorBagInterface $translatorBag): void
7577
$existingKeysByDomain[$domain] = [];
7678
}
7779

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

8183
$keysToCreate = $createdKeysByDomain = [];
@@ -219,7 +221,7 @@ private function createKeys(array $keys, string $domain): array
219221
* Translations will be created for keys without existing translations.
220222
* Translations will be updated for keys with existing translations.
221223
*/
222-
private function updateTranslations(array $keysByDomain, TranslatorBagInterface $translatorBag)
224+
private function updateTranslations(array $keysByDomain, TranslatorBagInterface $translatorBag): void
223225
{
224226
$keysToUpdate = [];
225227

@@ -250,43 +252,57 @@ private function updateTranslations(array $keysByDomain, TranslatorBagInterface
250252
}
251253
}
252254

253-
$chunks = array_chunk($keysToUpdate, 500);
254-
$responses = [];
255-
256-
foreach ($chunks as $chunk) {
257-
$responses[] = $this->client->request('PUT', 'keys', [
258-
'json' => ['keys' => $chunk],
259-
]);
260-
}
255+
$response = $this->client->request('PUT', 'keys', [
256+
'json' => ['keys' => $keysToUpdate],
257+
]);
261258

262-
foreach ($responses as $response) {
263-
if (200 !== $response->getStatusCode()) {
264-
$this->logger->error(sprintf('Unable to create/update translations to Lokalise: "%s".', $response->getContent(false)));
265-
}
259+
if (200 !== $response->getStatusCode()) {
260+
$this->logger->error(sprintf('Unable to create/update translations to Lokalise: "%s".', $response->getContent(false)));
266261
}
267262
}
268263

269-
private function getKeysIds(array $keys, string $domain): array
264+
private function getKeysIds(array $keys, string $domain, int $page = 1): array
270265
{
271266
$response = $this->client->request('GET', 'keys', [
272267
'query' => [
273268
'filter_keys' => implode(',', $keys),
274269
'filter_filenames' => $this->getLokaliseFilenameFromDomain($domain),
270+
'limit' => self::LOKALISE_GET_KEYS_LIMIT,
271+
'page' => $page,
275272
],
276273
]);
277274

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

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

285-
return $carry;
286-
}, []);
282+
if (\count($keysFromResponse) > 0) {
283+
$result = array_reduce($keysFromResponse, static function ($carry, array $keyItem) {
284+
$carry[$keyItem['key_name']['web']] = $keyItem['key_id'];
285+
286+
return $carry;
287+
}, []);
288+
}
289+
290+
$paginationTotalCount = $response->getHeaders(false)['x-pagination-total-count'] ?? [];
291+
$keysTotalCount = (int) (reset($paginationTotalCount) ?? 0);
292+
293+
if (0 === $keysTotalCount) {
294+
return $result;
295+
}
296+
297+
$pages = ceil($keysTotalCount / self::LOKALISE_GET_KEYS_LIMIT);
298+
if ($page < $pages) {
299+
$result = array_merge($result, $this->getKeysIds($keys, $domain, ++$page));
300+
}
301+
302+
return $result;
287303
}
288304

289-
private function ensureAllLocalesAreCreated(TranslatorBagInterface $translatorBag)
305+
private function ensureAllLocalesAreCreated(TranslatorBagInterface $translatorBag): void
290306
{
291307
$providerLanguages = $this->getLanguages();
292308
$missingLanguages = array_reduce($translatorBag->getCatalogues(), static function ($carry, $catalogue) use ($providerLanguages) {

src/Symfony/Component/Translation/Bridge/Lokalise/Tests/LokaliseProviderTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ public function testCompleteWriteProcess()
7676

7777
$getKeysIdsForMessagesDomainResponse = function (string $method, string $url, array $options = []): ResponseInterface {
7878
$expectedQuery = [
79-
'filter_keys' => 'young_dog',
79+
'filter_keys' => '',
8080
'filter_filenames' => 'messages.xliff',
81+
'limit' => 5000,
82+
'page' => 1,
8183
];
8284

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

9092
$getKeysIdsForValidatorsDomainResponse = function (string $method, string $url, array $options = []): ResponseInterface {
9193
$expectedQuery = [
92-
'filter_keys' => 'post.num_comments',
94+
'filter_keys' => '',
9395
'filter_filenames' => 'validators.xliff',
96+
'limit' => 5000,
97+
'page' => 1,
9498
];
9599

96100
$this->assertSame('GET', $method);
@@ -337,6 +341,8 @@ public function testDeleteProcess()
337341
$expectedQuery = [
338342
'filter_keys' => 'a',
339343
'filter_filenames' => 'messages.xliff',
344+
'limit' => 5000,
345+
'page' => 1,
340346
];
341347

342348
$this->assertSame('GET', $method);
@@ -355,6 +361,8 @@ public function testDeleteProcess()
355361
$expectedQuery = [
356362
'filter_keys' => 'post.num_comments',
357363
'filter_filenames' => 'validators.xliff',
364+
'limit' => 5000,
365+
'page' => 1,
358366
];
359367

360368
$this->assertSame('GET', $method);

0 commit comments

Comments
 (0)