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
Taken into consideration first review
  • Loading branch information
welcoMattic committed Sep 28, 2020
commit d66519734049c2549af614920c88e6e07ee3920f
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
use Symfony\Component\Translation\Remotes;
use Symfony\Component\Translation\Writer\TranslationWriterInterface;

/**
* @final
*/
class TranslationPullCommand extends Command
final class TranslationPullCommand extends Command
{
use TranslationTrait;

Expand Down Expand Up @@ -81,18 +78,18 @@ protected function configure()

<info>php %command.full_name% --force remote</info>

You can remote local translations which are not present on the remote:
You can remove local translations which are not present on the remote:

<info>php %command.full_name% --delete-obsolete remote</info>

Full example:

<info>php %command.full_name% remote --force --delete-obsolete --domains=messages,validators --locales=en</info>

This command will pull all translations linked to domains messages & validators
for the locale en. Local translations for the specified domains & locale will
This command will pull all translations linked to domains messages and validators
for the locale en. Local translations for the specified domains and locale will
be erased if they're not present on the remote and overwritten if it's the
case. Local translations for others domains & locales will be ignored.
case. Local translations for others domains and locales will be ignored.
EOF
)
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
use Symfony\Component\Translation\Remotes;

/**
* @final
*/
class TranslationPushCommand extends Command
final class TranslationPushCommand extends Command
{
use TranslationTrait;

Expand Down Expand Up @@ -87,10 +84,10 @@ protected function configure()

<info>php %command.full_name% remote --force --delete-obsolete --domains=messages,validators --locales=en</info>

This command will push all translations linked to domains messages & validators
for the locale en. Remote translations for the specified domains & locale will
This command will push all translations linked to domains messages and validators
for the locale en. Remote translations for the specified domains and locale will
be erased if they're not present locally and overwritten if it's the
case. Remote translations for others domains & locales will be ignored.
case. Remote translations for others domains and locales will be ignored.
EOF
)
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected function configure()
<info>php %command.full_name% --dump-messages en</info>
<info>php %command.full_name% --force --prefix="new_" fr</info>

You can sort the output with the <comment>--sort</comment> flag:
You can sort the output with the <comment>--sort</> flag:

<info>php %command.full_name% --dump-messages --sort=asc en AcmeBundle</info>
<info>php %command.full_name% --dump-messages --sort=desc fr</info>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1204,8 +1204,8 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
$container->getDefinition('console.command.translation_update')->replaceArgument(6, $transPaths);
}

if (!empty($config['remotes'])) {
if (empty($config['enabled_locales'])) {
if ($config['remotes']) {
if (!$config['enabled_locales']) {
throw new LogicException('You must specify framework.translator.enabled_locales in order to use remotes.');
}

Expand Down
33 changes: 28 additions & 5 deletions src/Symfony/Component/Translation/Bridge/Crowdin/CrowdinRemote.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Symfony\Component\Translation\Bridge\Crowdin;

use Psr\Log\LoggerInterface;
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 All @@ -20,22 +23,23 @@
* @author Fabien Potencier <fabien@symfony.com>
*
* @experimental in 5.2
* @final
*
* In Crowdin:
*/
class CrowdinRemote extends AbstractRemote
final class CrowdinRemote extends AbstractRemote
{
protected const HOST = 'crowdin.com/api/v2';
protected const HOST = 'api.crowdin.com';

private $apiKey;
private $loader;
private $logger;
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 All @@ -53,7 +57,26 @@ public function write(TranslatorBag $translations, bool $override = false): void

public function read(array $domains, array $locales): TranslatorBag
{
// TODO: Implement read() method.
$filter = $domains ? implode(',', $domains) : '*';
$translatorBag = new 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' => $this->getDefaultHeaders(),
]);

$responseContent = $response->getContent(false);

if (Response::HTTP_OK !== $response->getStatusCode()) {
throw new TransportException('Unable to read the Loco response: '.$responseContent, $response);
}

foreach ($domains as $domain) {
$translatorBag->addCatalogue($this->loader->load($responseContent, $locale, $domain));
}
}

return $translatorBag;
}

public function delete(TranslatorBag $translations): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,10 @@ final class CrowdinRemoteFactory extends AbstractRemoteFactory
*/
public function create(Dsn $dsn): RemoteInterface
{
$scheme = $dsn->getScheme();
$apiKey = $this->getUser($dsn);
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

if ('crowdin' === $scheme) {
return (new CrowdinRemote($apiKey, $this->client, $this->loader, $this->defaultLocale))
->setHost($host)
->setPort($port)
if ('crowdin' === $dsn->getScheme()) {
return (new CrowdinRemote($this->getUser($dsn), $this->client, $this->loader, $this->logger, $this->defaultLocale))
->setHost('default' === $dsn->getHost() ? null : $dsn->getHost())
->setPort($dsn->getPort())
;
}

Expand Down
3 changes: 1 addition & 2 deletions src/Symfony/Component/Translation/Bridge/Loco/LocoRemote.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
* @author Fabien Potencier <fabien@symfony.com>
*
* @experimental in 5.2
* @final
*
* In Loco:
* tags refers to Symfony's translation domains
* assets refers to Symfony's translation keys
* translations refers to Symfony's translation messages
*/
class LocoRemote extends AbstractRemote
final class LocoRemote extends AbstractRemote
{
protected const HOST = 'localise.biz';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,10 @@ final class LocoRemoteFactory extends AbstractRemoteFactory
*/
public function create(Dsn $dsn): RemoteInterface
{
$scheme = $dsn->getScheme();
$apiKey = $this->getUser($dsn);
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

if ('loco' === $scheme) {
return (new LocoRemote($apiKey, $this->client, $this->logger, $this->loader, $this->defaultLocale))
->setHost($host)
->setPort($port)
if ('loco' === $dsn->getScheme()) {
return (new LocoRemote($this->getUser($dsn), $this->client, $this->loader, $this->logger, $this->defaultLocale))
->setHost('default' === $dsn->getHost() ? null : $dsn->getHost())
->setPort($dsn->getPort())
;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @experimental in 5.1
* @experimental in 5.2
*/
class TransportException extends RuntimeException implements TransportExceptionInterface
{
Expand Down
56 changes: 0 additions & 56 deletions src/Symfony/Component/Translation/Remote/NullRemote.php

This file was deleted.

34 changes: 0 additions & 34 deletions src/Symfony/Component/Translation/Remote/NullRemoteFactory.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function write(TranslatorBag $translations, bool $override = false): void
*/
public function read(array $domains, array $locales): TranslatorBag
{
$domains = empty($this->domains) ? $domains : array_intersect($this->domains, $domains);
$domains = $this->domains ? $domains : array_intersect($this->domains, $domains);
$locales = array_intersect($this->locales, $locales);

return $this->remote->read($domains, $locales);
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/Translation/Remote/RemoteInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@
use Symfony\Component\Translation\TranslatorBag;

/**
* Remote is used to sync translations with a remote.
* Providers are used to sync translations with a translation provider.
*/
interface RemoteInterface
{
/**
* Write given translation to the remote.
* Writes given translation to the provider.
*
* * Translations available in the MessageCatalogue only must be created.
* * Translations available in both the MessageCatalogue and on the remote
* * Translations available in both the MessageCatalogue and on the provider
* must be overwritten.
* * Translations available on the remote only must be kept.
*/
public function write(TranslatorBag $translations, bool $override = false): void;

/**
* This method must return asked translations.
* Returns asked translations.
*/
public function read(array $domains, array $locales): TranslatorBag;

/**
* This method must delete all translation given in the TranslatorBag.
* Delete all translation given in the TranslatorBag.
*/
public function delete(TranslatorBag $translations): void;
}
5 changes: 1 addition & 4 deletions src/Symfony/Component/Translation/Remotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
use Symfony\Component\Translation\Exception\InvalidArgumentException;
use Symfony\Component\Translation\Remote\RemoteInterface;

/**
* @final
*/
class Remotes
final class Remotes
{
private $remotes;

Expand Down
Loading
0