diff --git a/composer.json b/composer.json index 53520a9..c7419b8 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ "require": { "php": "^7.0", "php-translation/common": "^0.2", + "symfony/translation": "^2.7 || ^3.0", "friendsofapi/localise.biz": "^0.2" }, "require-dev": { diff --git a/src/Loco.php b/src/Loco.php index 08b0790..f2a4a8e 100644 --- a/src/Loco.php +++ b/src/Loco.php @@ -14,16 +14,19 @@ use FAPI\Localise\Exception\Domain\AssetConflictException; use FAPI\Localise\Exception\Domain\NotFoundException; use FAPI\Localise\LocoClient; +use Symfony\Component\Translation\Loader\ArrayLoader; +use Symfony\Component\Translation\MessageCatalogueInterface; use Translation\Common\Exception\StorageException; use Translation\Common\Model\Message; use Translation\Common\Storage; +use Translation\Common\TransferableStorage; /** * Localize.biz. * * @author Tobias Nyholm */ -class Loco implements Storage +class Loco implements Storage, TransferableStorage { /** * @var LocoClient @@ -122,12 +125,48 @@ public function delete($locale, $domain, $key) $this->client->translations()->delete($projectKey, $key, $locale); } + /** + * {@inheritdoc} + */ + public function export(MessageCatalogueInterface $catalogue) + { + $locale = $catalogue->getLocale(); + $loader = new ArrayLoader(); + foreach ($this->domainToProjectId as $domain => $projectKey) { + try { + $data = $this->client->export()->locale( + $projectKey, + $locale, + 'json', + ['format' => 'symfony'] + ); + $array = json_decode($data, true); + $catalogue->addCatalogue( + $loader->load($array, $locale, $domain) + ); + } catch (NotFoundException $e) { + } + } + } + + /** + * {@inheritdoc} + */ + public function import(MessageCatalogueInterface $catalogue) + { + $locale = $catalogue->getLocale(); + foreach ($this->domainToProjectId as $domain => $projectKey) { + $data = json_encode($catalogue->all($domain)); + $this->client->import()->import($projectKey, 'json', $data, ['locale' => $locale, 'async' => 1]); + } + } + /** * @param string $domain * * @return string */ - protected function getApiKey($domain) + private function getApiKey($domain) { if (isset($this->domainToProjectId[$domain])) { return $this->domainToProjectId[$domain];