Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 40b724c

Browse filesBrowse files
committed
bug #42649 [Translation] Fix message key handling for the Localise provider (xepozz)
This PR was squashed before being merged into the 5.3 branch. Discussion ---------- [Translation] Fix message key handling for the Localise provider | Q | A | ------------- | --- | Branch? | 5.3 and above | Bug fix? | yes | New feature? |no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? |no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | - Fixed non-recursively merging existing keys and created. Now created keys totally removed by usual merge. Handled situation when no keys are created in localise service. Commits ------- 36b4837 [Translation] Fix message key handling for the Localise provider
2 parents 75ace35 + 36b4837 commit 40b724c
Copy full SHA for 40b724c

File tree

3 files changed

+28
-24
lines changed
Filter options

3 files changed

+28
-24
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Bridge/Lokalise/LokaliseProvider.php
+9-10Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function write(TranslatorBagInterface $translatorBag): void
9292
$createdKeysByDomain[$domain] = $this->createKeys($keys, $domain);
9393
}
9494

95-
$this->updateTranslations(array_merge($createdKeysByDomain, $existingKeysByDomain), $translatorBag);
95+
$this->updateTranslations(array_merge_recursive($createdKeysByDomain, $existingKeysByDomain), $translatorBag);
9696
}
9797

9898
public function read(array $domains, array $locales): TranslatorBag
@@ -203,7 +203,8 @@ private function createKeys(array $keys, string $domain): array
203203
continue;
204204
}
205205

206-
$createdKeys = array_reduce($response->toArray(false)['keys'], function ($carry, array $keyItem) {
206+
$keys = $response->toArray(false)['keys'] ?? [];
207+
$createdKeys = array_reduce($keys, static function ($carry, array $keyItem) {
207208
$carry[$keyItem['key_name']['web']] = $keyItem['key_id'];
208209

209210
return $carry;
@@ -232,7 +233,7 @@ private function updateTranslations(array $keysByDomain, TranslatorBagInterface
232233
'android' => null,
233234
'other' => null,
234235
],
235-
'translations' => array_reduce($translatorBag->getCatalogues(), function ($carry, MessageCatalogueInterface $catalogue) use ($keyName, $domain) {
236+
'translations' => array_reduce($translatorBag->getCatalogues(), static function ($carry, MessageCatalogueInterface $catalogue) use ($keyName, $domain) {
236237
// Message could be not found because the catalogue is empty.
237238
// We must not send the key in place of the message to avoid wrong message update on the provider.
238239
if ($catalogue->get($keyName, $domain) !== $keyName) {
@@ -277,7 +278,7 @@ private function getKeysIds(array $keys, string $domain): array
277278
$this->logger->error(sprintf('Unable to get keys ids from Lokalise: "%s".', $response->getContent(false)));
278279
}
279280

280-
return array_reduce($response->toArray(false)['keys'], function ($carry, array $keyItem) {
281+
return array_reduce($response->toArray(false)['keys'], static function ($carry, array $keyItem) {
281282
$carry[$keyItem['key_name']['web']] = $keyItem['key_id'];
282283

283284
return $carry;
@@ -287,8 +288,8 @@ private function getKeysIds(array $keys, string $domain): array
287288
private function ensureAllLocalesAreCreated(TranslatorBagInterface $translatorBag)
288289
{
289290
$providerLanguages = $this->getLanguages();
290-
$missingLanguages = array_reduce($translatorBag->getCatalogues(), function ($carry, $catalogue) use ($providerLanguages) {
291-
if (!\in_array($catalogue->getLocale(), $providerLanguages)) {
291+
$missingLanguages = array_reduce($translatorBag->getCatalogues(), static function ($carry, $catalogue) use ($providerLanguages) {
292+
if (!\in_array($catalogue->getLocale(), $providerLanguages, true)) {
292293
$carry[] = $catalogue->getLocale();
293294
}
294295

@@ -313,9 +314,7 @@ private function getLanguages(): array
313314
$responseContent = $response->toArray(false);
314315

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

321320
return [];
@@ -325,7 +324,7 @@ private function createLanguages(array $languages): void
325324
{
326325
$response = $this->client->request('POST', 'languages', [
327326
'json' => [
328-
'languages' => array_map(function ($language) {
327+
'languages' => array_map(static function ($language) {
329328
return ['lang_iso' => $language];
330329
}, $languages),
331330
],

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Bridge/Lokalise/Tests/LokaliseProviderTest.php
+16-14Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ public function testCompleteWriteProcess()
6969

7070
$this->assertSame('POST', $method);
7171
$this->assertSame('https://api.lokalise.com/api2/projects/PROJECT_ID/languages', $url);
72-
$this->assertSame($expectedBody, $options['body']);
72+
$this->assertJsonStringEqualsJsonString($expectedBody, $options['body']);
7373

7474
return new MockResponse();
7575
};
7676

7777
$getKeysIdsForMessagesDomainResponse = function (string $method, string $url, array $options = []): ResponseInterface {
7878
$expectedQuery = [
79-
'filter_keys' => 'a',
79+
'filter_keys' => 'young_dog',
8080
'filter_filenames' => 'messages.xliff',
8181
];
8282

@@ -104,7 +104,7 @@ public function testCompleteWriteProcess()
104104
$expectedBody = json_encode([
105105
'keys' => [
106106
[
107-
'key_name' => 'a',
107+
'key_name' => 'young_dog',
108108
'platforms' => ['web'],
109109
'filenames' => [
110110
'web' => 'messages.xliff',
@@ -117,11 +117,11 @@ public function testCompleteWriteProcess()
117117
]);
118118

119119
$this->assertSame('POST', $method);
120-
$this->assertSame($expectedBody, $options['body']);
120+
$this->assertJsonStringEqualsJsonString($expectedBody, $options['body']);
121121

122122
return new MockResponse(json_encode(['keys' => [
123123
[
124-
'key_name' => ['web' => 'a'],
124+
'key_name' => ['web' => 'young_dog'],
125125
'key_id' => 29,
126126
],
127127
]]));
@@ -144,7 +144,7 @@ public function testCompleteWriteProcess()
144144
]);
145145

146146
$this->assertSame('POST', $method);
147-
$this->assertSame($expectedBody, $options['body']);
147+
$this->assertJsonStringEqualsJsonString($expectedBody, $options['body']);
148148

149149
return new MockResponse(json_encode(['keys' => [
150150
[
@@ -153,8 +153,8 @@ public function testCompleteWriteProcess()
153153
],
154154
]]));
155155
};
156-
157-
$updateTranslationsResponse = function (string $method, string $url, array $options = []): ResponseInterface {
156+
$updateProcessed = false;
157+
$updateTranslationsResponse = function (string $method, string $url, array $options = []) use (&$updateProcessed): ResponseInterface {
158158
$expectedBody = json_encode([
159159
'keys' => [
160160
[
@@ -169,11 +169,11 @@ public function testCompleteWriteProcess()
169169
'translations' => [
170170
[
171171
'language_iso' => 'en',
172-
'translation' => 'trans_en_a',
172+
'translation' => 'puppy',
173173
],
174174
[
175175
'language_iso' => 'fr',
176-
'translation' => 'trans_fr_a',
176+
'translation' => 'chiot',
177177
],
178178
],
179179
],
@@ -200,8 +200,9 @@ public function testCompleteWriteProcess()
200200
],
201201
]);
202202

203+
$updateProcessed = true;
203204
$this->assertSame('PUT', $method);
204-
$this->assertSame($expectedBody, $options['body']);
205+
$this->assertJsonStringEqualsJsonString($expectedBody, $options['body']);
205206

206207
return new MockResponse();
207208
};
@@ -221,15 +222,16 @@ public function testCompleteWriteProcess()
221222

222223
$translatorBag = new TranslatorBag();
223224
$translatorBag->addCatalogue(new MessageCatalogue('en', [
224-
'messages' => ['a' => 'trans_en_a'],
225+
'messages' => ['young_dog' => 'puppy'],
225226
'validators' => ['post.num_comments' => '{count, plural, one {# comment} other {# comments}}'],
226227
]));
227228
$translatorBag->addCatalogue(new MessageCatalogue('fr', [
228-
'messages' => ['a' => 'trans_fr_a'],
229+
'messages' => ['young_dog' => 'chiot'],
229230
'validators' => ['post.num_comments' => '{count, plural, one {# commentaire} other {# commentaires}}'],
230231
]));
231232

232233
$provider->write($translatorBag);
234+
$this->assertTrue($updateProcessed, 'Translations update was not called.');
233235
}
234236

235237
/**
@@ -248,7 +250,7 @@ public function testReadForOneLocaleAndOneDomain(string $locale, string $domain,
248250

249251
$this->assertSame('POST', $method);
250252
$this->assertSame('https://api.lokalise.com/api2/projects/PROJECT_ID/files/export', $url);
251-
$this->assertSame($expectedBody, $options['body']);
253+
$this->assertJsonStringEqualsJsonString($expectedBody, $options['body']);
252254

253255
return new MockResponse(json_encode([
254256
'files' => [

‎src/Symfony/Component/Translation/Bridge/Lokalise/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Bridge/Lokalise/composer.json
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"symfony/http-client": "^5.3",
2222
"symfony/translation": "^5.3"
2323
},
24+
"require-dev": {
25+
"phpunit/phpunit": "^9.5"
26+
},
2427
"autoload": {
2528
"psr-4": { "Symfony\\Component\\Translation\\Bridge\\Lokalise\\": "" },
2629
"exclude-from-classmap": [

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.