Skip to content

Commit

Permalink
Make sure we are transferable (#2)
Browse files Browse the repository at this point in the history
* Make sure we are transferable

* Use async

* style fix

* Bugfix

* Style fix
  • Loading branch information
Nyholm committed Jan 2, 2017
1 parent 1146e00 commit fa5eef3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
43 changes: 41 additions & 2 deletions src/Loco.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tobias.nyholm@gmail.com>
*/
class Loco implements Storage
class Loco implements Storage, TransferableStorage
{
/**
* @var LocoClient
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit fa5eef3

Please sign in to comment.