Skip to content

[Translation] [Loco] Ability to configure value of status query-variable #58072

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
Sep 1, 2024
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Translation/Bridge/Loco/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Add support for the `status` query parameter of Loco translation API

6.1
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,16 @@ public function __construct(
private string $defaultLocale,
private string $endpoint,
private ?TranslatorBagInterface $translatorBag = null,
private ?string $restrictToStatus = null,
) {
}

public function __toString(): string
{
if ($this->restrictToStatus) {
return \sprintf('loco://%s?status=%s', $this->endpoint, $this->restrictToStatus);
}

return \sprintf('loco://%s', $this->endpoint);
}

Expand Down Expand Up @@ -96,7 +101,7 @@ public function read(array $domains, array $locales): TranslatorBag
$response = $this->client->request('GET', \sprintf('export/locale/%s.xlf', rawurlencode($locale)), [
'query' => [
'filter' => $domain,
'status' => 'translated,blank-translation',
'status' => $this->restrictToStatus ?? 'translated,blank-translation',
],
'headers' => [
'If-Modified-Since' => $previousCatalogue instanceof CatalogueMetadataAwareInterface ? $previousCatalogue->getCatalogueMetadata('last-modified', $domain) : null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function create(Dsn $dsn): LocoProvider

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

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

return new LocoProvider($client, $this->loader, $this->logger, $this->defaultLocale, $endpoint, $this->translatorBag);
return new LocoProvider($client, $this->loader, $this->logger, $this->defaultLocale, $endpoint, $this->translatorBag, $restrictToStatus);
}

protected function getSupportedSchemes(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Translation/Bridge/Loco/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ DSN example

```
// .env file
LOCO_DSN=loco://API_KEY@default
LOCO_DSN=loco://API_KEY@default?status=translated,blank-translation
```

where:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public static function createProvider(): iterable
'loco://localise.biz',
'loco://API_KEY@default',
];

yield [
'loco://localise.biz?status=translated,provisional',
'loco://API_KEY@default?status=translated,provisional',
];
}

public static function incompleteDsnProvider(): iterable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@

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

public static function toStringProvider(): iterable
Expand Down Expand Up @@ -1170,4 +1170,34 @@ public static function getResponsesForReadWithLastModified(): \Generator
yield [$locales, $domains, $responseContents, $lastModifieds, $expectedTranslatorBag];
}
}

public function testReadWithRestrictToStatus()
{
$loader = $this->getLoader();

$loader
->expects($this->once())
->method('load')
->willReturn($this->createMock(MessageCatalogue::class));

$provider = self::createProvider(
new MockHttpClient([
function (string $method, string $url, array $options = []): ResponseInterface {
$this->assertSame('GET', $method);
$this->assertSame('https://localise.biz/api/export/locale/de.xlf?filter=messages&status=translated%2Cprovisional', $url);
$this->assertSame(['filter' => 'messages', 'status' => 'translated,provisional'], $options['query']);

return new MockResponse();
},
], 'https://localise.biz/api/'),
$this->getLoader(),
$this->getLogger(),
$this->getDefaultLocale(),
'localise.biz/api/',
null,
'translated,provisional'
);

$this->translatorBag = $provider->read(['messages'], ['de']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

class LocoProviderWithoutTranslatorBagTest extends LocoProviderTest
{
public static function createProvider(HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint, ?TranslatorBagInterface $translatorBag = null): ProviderInterface
public static function createProvider(HttpClientInterface $client, LoaderInterface $loader, LoggerInterface $logger, string $defaultLocale, string $endpoint, ?TranslatorBagInterface $translatorBag = null, ?string $restrictToStatus = null): ProviderInterface
{
return new LocoProvider($client, $loader, $logger, $defaultLocale, $endpoint, null);
return new LocoProvider($client, $loader, $logger, $defaultLocale, $endpoint, null, $restrictToStatus);
}

/**
Expand Down
Loading