8000 [Translation][FrameworkBundle] Adding Translation Providers by welcoMattic · Pull Request #37462 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation][FrameworkBundle] Adding Translation Providers #37462

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Improve DX in Remotes Storages
  • Loading branch information
welcoMattic committed Sep 28, 2020
commit 0fd2e89206b3bfa0cad2804758011d9b4c5e532c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
->args([
service('http_client')->ignoreOnInvalid(),
service('translation.loader.xliff_raw'),
service('logger')->nullOnInvalid(),
param('kernel.default_locale'),
])
->abstract()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Symfony\Component\Translation\Bridge\Crowdin;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\Exception\TransportException;
use Symfony\Component\Translation\Loader\LoaderInterface;
use Symfony\Component\Translation\Remote\AbstractRemote;
use Symfony\Component\Translation\TranslatorBag;
Expand Down
57 changes: 31 additions & 26 deletions src/Symfony/Component/Translation/Bridge/Loco/LocoRemote.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Translation\Bridge\Loco;

use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\Exception\TransportException;
use Symfony\Component\Translation\Loader\LoaderInterface;
Expand All @@ -33,14 +34,23 @@ class LocoRemote extends AbstractRemote
{
protected const HOST = 'localise.biz';

/** @var string */
private $apiKey;

/** @var LoaderInterface|null */
private $loader;

/** @var LoggerInterface|null */
private $logger;

/** @var string|null */
private $defaultLocale;

public function __construct(string $apiKey, HttpClientInterface $client = null, LoaderInterface $loader = null, string $defaultLocale = null)
public function __construct(string $apiKey, HttpClientInterface $client = null, LoaderInterface $loader = null, LoggerInterface $logger = null, string $defaultLocale = null)
{
$this->apiKey = $apiKey;
$this->loader = $loader;
$this->logger = $logger;
$this->defaultLocale = $defaultLocale;

parent::__construct($client);
Expand Down Expand Up @@ -84,9 +94,7 @@ public function read(array $domains, array $locales): TranslatorBag

foreach ($locales as $locale) {
$response = $this->client->request('GET', sprintf('https://%s/api/export/locale/%s.xlf?filter=%s', $this->getEndpoint(), $locale, $filter), [
'headers' => [
'Authorization' => 'Loco '.$this->apiKey,
],
'headers' => $this->getDefaultHeaders(),
]);

$responseContent = $response->getContent(false);
Expand All @@ -110,8 +118,8 @@ public function delete(TranslatorBag $translations): void
{
$deletedIds = [];

foreach ($translations->all() as $locale => $messages) {
foreach ($messages as $domain => $messages) {
foreach ($translations->all() as $locale => $domainMessages) {
foreach ($domainMessages as $domain => $messages) {
foreach ($messages as $id => $message) {
if (\in_array($id, $deletedIds)) {
continue;
Expand All @@ -125,12 +133,17 @@ public function delete(TranslatorBag $translations): void
}
}

protected function getDefaultHeaders(): array
{
return [
'Authorization' => 'Loco '.$this->apiKey,
];
}

private function createAsset(string $id): void
{
$response = $this->client->request('POST', sprintf('https://%s/api/assets', $this->getEndpoint()), [
'headers' => [
'Authorization' => 'Loco '.$this->apiKey,
],
'headers' => $this->getDefaultHeaders(),
'body' => [
'name' => $id,
'id' => $id,
Expand All @@ -140,7 +153,9 @@ private function createAsset(string $id): void
]);

if (Response::HTTP_CONFLICT === $response->getStatusCode()) {
// Translation key already exists in Loco, do nothing
$this->logger->warning(sprintf('Translation key (%s) already exists in Loco.', $id), [
'id' => $id,
]);
} elseif (Response::HTTP_CREATED !== $response->getStatusCode()) {
throw new TransportException(sprintf('Unable to add new translation key (%s) to Loco: (status code: "%s") "%s".', $id, $response->getStatusCode(), $response->getContent(false)), $response);
}
Expand All @@ -149,9 +164,7 @@ private function createAsset(string $id): void
private function translateAsset(string $id, string $message, string $locale): void
{
$response = $this->client->request('POST', sprintf('https://%s/api/translations/%s/%s', $this->getEndpoint(), $id, $locale), [
'headers' => [
'Authorization' => 'Loco '.$this->apiKey,
],
'headers' => $this->getDefaultHeaders(),
'body' => $message,
]);

Expand All @@ -169,9 +182,7 @@ private function tagsAssets(array $ids, string $tag): void
}

$response = $this->client->request('POST', sprintf('https://%s/api/tags/%s.json', $this->getEndpoint(), $tag), [
'headers' => [
'Authorization' => 'Loco '.$this->apiKey,
],
'headers' => $this->getDefaultHeaders(),
'body' => $idsAsString,
]);

Expand All @@ -182,10 +193,8 @@ private function tagsAssets(array $ids, string $tag): void

private function createTag(string $tag): void
{
$response = $this->client->request('POST', sprintf('https://%s/api/tags.json', $this->getEndpoint(), $tag), [
'headers' => [
'Authorization' => 'Loco '.$this->apiKey,
],
$response = $this->client->request('POST', sprintf('https://%s/api/tags.json', $this->getEndpoint()), [
'headers' => $this->getDefaultHeaders(),
'body' => [
'name' => $tag,
],
Expand All @@ -199,9 +208,7 @@ private function createTag(string $tag): void
private function getTags(): array
{
$response = $this->client->request('GET', sprintf('https://%s/api/tags.json', $this->getEndpoint()), [
'headers' => [
'Authorization' => 'Loco '.$this->apiKey,
],
'headers' => $this->getDefaultHeaders(),
]);

$content = $response->getContent(false);
Expand All @@ -216,9 +223,7 @@ private function getTags(): array
private function deleteAsset(string $id): void
{
$response = $this->client->request('DELETE', sprintf('https://%s/api/assets/%s.json', $this->getEndpoint(), $id), [
'headers' => [
'Authorization' => 'Loco '.$this->apiKey,
],
'headers' => $this->getDefaultHeaders(),
]);

if (Response::HTTP_OK !== $response->getStatusCode()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function create(Dsn $dsn): RemoteInterface
$port = $dsn->getPort();

if ('loco' === $scheme) {
return (new LocoRemote($apiKey, $this->client, $this->loader, $this->defaultLocale))
return (new LocoRemote($apiKey, $this->client, $this->logger, $this->loader, $this->defaultLocale))
->setHost($host)
->setPort($port)
;
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Translation/Remote/AbstractRemote.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ protected function getDefaultHost(): string
{
return static::HOST;
}

protected function getDefaultHeaders(): array
{
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,30 @@

namespace Symfony\Component\Translation\Remote;

use Psr\Log\LoggerInterface;
use Symfony\Component\Translation\Exception\IncompleteDsnException;
use Symfony\Component\Translation\Loader\LoaderInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

abstract class AbstractRemoteFactory implements RemoteFactoryInterface
{
/** @var HttpClientInterface|null */
protected $client;

/** @var LoaderInterface|null */
protected $loader;

/** @var LoggerInterface|null */
protected $logger;

/** @var string|null */
protected $defaultLocale;

public function __construct(HttpClientInterface $client = null, LoaderInterface $loader = null, string $defaultLocale = null)
public function __construct(HttpClientInterface $client = null, LoaderInterface $loader = null, LoggerInterface $logger = null, string $defaultLocale = null)
{
$this->client = $client;
$this->loader = $loader;
$this->logger = $logger;
$this->defaultLocale = $defaultLocale;
}

Expand Down
0