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 cc11de0

Browse filesBrowse files
committed
feature #58072 [Translation] [Loco] Ability to configure value of status query-variable (mathielen)
This PR was squashed before being merged into the 7.2 branch. Discussion ---------- [Translation] [Loco] Ability to configure value of `status` query-variable Added ability to configure value of `status` query-variable when fetching translations from loco API. | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | | License | MIT With loco it is possible to flag your translations as `provisional`, meaning that they might not be approved by someone in charge of your project's translations. For example these provisional translations might be auto-translated. To still be able to use these auto-translated translations in your project it must be possible to integrate them (i.e. via CLI command `bin/console translation:pull loco`) Therefore, when invoking the export-API of loco a different value for the field `status` has to be given. Right now it is statically set to `translated,blank-translation`, meaning provisional translations will *not* get fetched. With the DSN to the translation provider, we can already provide options to the translation provider implementation with query-parameters like this: `loco://API_KEY@default?status=translated,provisional` I have used this functionality for setting the desired value. By default (if omitted) the current value `translated,blank-translation` will be used. Commits ------- 5eff168 [Translation] [Loco] Ability to configure value of `status` query-variable
2 parents 183137e + 5eff168 commit cc11de0
Copy full SHA for cc11de0

File tree

Expand file treeCollapse file tree

7 files changed

+53
-7
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+53
-7
lines changed

‎src/Symfony/Component/Translation/Bridge/Loco/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Bridge/Loco/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.2
5+
---
6+
7+
* Add support for the `status` query parameter of Loco translation API
8+
49
6.1
510
---
611

‎src/Symfony/Component/Translation/Bridge/Loco/LocoProvider.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Bridge/Loco/LocoProvider.php
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,16 @@ public function __construct(
3838
private string $defaultLocale,
3939
private string $endpoint,
4040
private ?TranslatorBagInterface $translatorBag = null,
41+
private ?string $restrictToStatus = null,
4142
) {
4243
}
4344

4445
public function __toString(): string
4546
{
47+
if ($this->restrictToStatus) {
48+
return \sprintf('loco://%s?status=%s', $this->endpoint, $this->restrictToStatus);
49+
}
50+
4651
return \sprintf('loco://%s', $this->endpoint);
4752
}
4853

@@ -96,7 +101,7 @@ public function read(array $domains, array $locales): TranslatorBag
96101
$response = $this->client->request('GET', \sprintf('export/locale/%s.xlf', rawurlencode($locale)), [
97102
'query' => [
98103
'filter' => $domain,
99-
'status' => 'translated,blank-translation',
104+
'status' => $this->restrictToStatus ?? 'translated,blank-translation',
100105
],
101106
'headers' => [
102107
'If-Modified-Since' => $previousCatalogue instanceof CatalogueMetadataAwareInterface ? $previousCatalogue->getCatalogueMetadata('last-modified', $domain) : null,

‎src/Symfony/Component/Translation/Bridge/Loco/LocoProviderFactory.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Bridge/Loco/LocoProviderFactory.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function create(Dsn $dsn): LocoProvider
4343

4444
$endpoint = 'default' === $dsn->getHost() ? self::HOST : $dsn->getHost();
4545
$endpoint .= $dsn->getPort() ? ':'.$dsn->getPort() : '';
46+
$restrictToStatus = $dsn->getOption('status');
4647

4748
$client = $this->client->withOptions([
4849
'base_uri' => 'https://'.$endpoint.'/api/',
@@ -51,7 +52,7 @@ public function create(Dsn $dsn): LocoProvider
5152
],
5253
]);
5354

54-
return new LocoProvider($client, $this->loader, $this->logger, $this->defaultLocale, $endpoint, $this->translatorBag);
55+
return new LocoProvider($client, $this->loader, $this->logger, $this->defaultLocale, $endpoint, $this->translatorBag, $restrictToStatus);
5556
}
5657

5758
protected function getSupportedSchemes(): array

‎src/Symfony/Component/Translation/Bridge/Loco/README.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Bridge/Loco/README.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ DSN example
88

99
```
1010
// .env file
11-
LOCO_DSN=loco://API_KEY@default
11+
LOCO_DSN=loco://API_KEY@default?status=translated,blank-translation
1212
```
1313

1414
where:

‎src/Symfony/Component/Translation/Bridge/Loco/Tests/LocoProviderFactoryTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Bridge/Loco/Tests/LocoProviderFactoryTest.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public static function createProvider(): iterable
3434
'loco://localise.biz',
3535
'loco://API_KEY@default',
3636
];
37+
38+
yield [
39+
'loco://localise.biz?status=translated,provisional',
40+
'loco://API_KEY@default?status=translated,provisional',
41+
];
3742
}
3843

3944
public static function incompleteDsnProvider(): iterable

‎src/Symfony/Component/Translation/Bridge/Loco/Tests/LocoProviderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Bridge/Loco/Tests/LocoProviderTest.php
+32-2Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030

3131
class LocoProviderTest extends ProviderTestCase
3232
{
33-
public static function createProvider(HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint, ?TranslatorBagInterface $translatorBag = null): ProviderInterface
33+
public static function createProvider(HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint, ?TranslatorBagInterface $translatorBag = null, ?string $restrictToStatus = null): ProviderInterface
3434
{
35-
return new LocoProvider($client, $loader, $logger, $defaultLocale, $endpoint, $translatorBag ?? new TranslatorBag());
35+
return new LocoProvider($client, $loader, $logger, $defaultLocale, $endpoint, $translatorBag ?? new TranslatorBag(), $restrictToStatus);
3636
}
3737

3838
public static function toStringProvider(): iterable
@@ -1170,4 +1170,34 @@ public static function getResponsesForReadWithLastModified(): \Generator
11701170
yield [$locales, $domains, $responseContents, $lastModifieds, $expectedTranslatorBag];
11711171
}
11721172
}
1173+
1174+
public function testReadWithRestrictToStatus()
1175+
{
1176+
$loader = $this->getLoader();
1177+
1178+
$loader
1179+
->expects($this->once())
1180+
->method('load')
1181+
->willReturn($this->createMock(MessageCatalogue::class));
1182+
1183+
$provider = self::createProvider(
1184+
new MockHttpClient([
1185+
function (string $method, string $url, array $options = []): ResponseInterface {
1186+
$this->assertSame('GET', $method);
1187+
$this->assertSame('https://localise.biz/api/export/locale/de.xlf?filter=messages&status=translated%2Cprovisional', $url);
1188+
$this->assertSame(['filter' => 'messages', 'status' => 'translated,provisional'], $options['query']);
1189+
1190+
return new MockResponse();
1191+
},
1192+
], 'https://localise.biz/api/'),
1193+
$this->getLoader(),
1194+
$this->getLogger(),
1195+
$this->getDefaultLocale(),
1196+
'localise.biz/api/',
1197+
null,
1198+
'translated,provisional'
1199+
);
1200+
1201+
$this->translatorBag = $provider->read(['messages'], ['de']);
1202+
}
11731203
}

‎src/Symfony/Component/Translation/Bridge/Loco/Tests/LocoProviderWithoutTranslatorBagTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Bridge/Loco/Tests/LocoProviderWithoutTranslatorBagTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525

2626
class LocoProviderWithoutTranslatorBagTest extends LocoProviderTest
2727
{
28-
public static function createProvider(HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint, ?TranslatorBagInterface $translatorBag = null): ProviderInterface
28+
public static function createProvider(HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint, ?TranslatorBagInterface $translatorBag = null, ?string $restrictToStatus = null): ProviderInterface
2929
{
30-
return new LocoProvider($client, $loader, $logger, $defaultLocale, $endpoint, null);
30+
return new LocoProvider($client, $loader, $logger, $defaultLocale, $endpoint, null, $restrictToStatus);
3131
}
3232

3333
/**

0 commit comments

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