Skip to content

Commit

Permalink
allow to download catalogue from selected tag
Browse files Browse the repository at this point in the history
  • Loading branch information
nediam committed Sep 27, 2015
1 parent 662d548 commit 058cbde
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function getConfigTreeBuilder()
->defaultValue('yml')
->end()
->scalarNode('translations_path')->isRequired()->end()
->arrayNode('translations')
->arrayNode('catalogues')
->prototype('scalar')->end()
->end()
->arrayNode('locales')
Expand Down
37 changes: 29 additions & 8 deletions Service/PhraseApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class PhraseApp implements LoggerAwareInterface
* @var LoggerInterface
*/
private $logger;
/**
* @var array
*/
private $catalogues;

/**
* PhraseApp constructor.
Expand All @@ -78,6 +82,7 @@ public function __construct(PhraseAppClient $client, TranslationLoader $translat
$this->locales = $config['locales'];
$this->outputFormat = $config['output_format'];
$this->translationsPath = $config['translations_path'];
$this->catalogues = $config['catalogues'];
$this->logger = $logger;
}

Expand Down Expand Up @@ -122,12 +127,13 @@ public function getLocales()
*
* @return array|string
*/
protected function makeDownloadRequest($locale, $format)
protected function makeDownloadRequest($locale, $format, $tag)
{
$response = $this->client->request('locale.download', [
'project_id' => $this->projectId,
'id' => $locale,
'file_format' => $format,
'tag' => $tag,
]);

return $response['text']->getContents();
Expand All @@ -141,15 +147,20 @@ protected function makeDownloadRequest($locale, $format)
protected function downloadLocale($targetLocale)
{
$sourceLocale = $this->locales[$targetLocale];
$tmpFile = $this->getTmpPath() . '/' . 'messages.' . $targetLocale . '.yml';


if (true === array_key_exists($sourceLocale, $this->downloadedLocales)) {
$this->logger->notice('Copying translations for locale "{targetLocale}" from "{sourceLocale}".', [
'targetLocale' => $targetLocale,
'sourceLocale' => $sourceLocale
]);
// Make copy because operated catalogues must belong to the same locale
copy($this->downloadedLocales[$sourceLocale], $tmpFile);

foreach ($this->catalogues as $catalogueName => $tagName) {
$tmpFile = sprintf('%s/%s.%s.yml', $this->getTmpPath(), $catalogueName, $targetLocale);

// Make copy because operated catalogues must belong to the same locale
copy($this->downloadedLocales[$sourceLocale][$catalogueName], $tmpFile);
}

return $this->downloadedLocales[$sourceLocale];
}
Expand All @@ -158,9 +169,19 @@ protected function downloadLocale($targetLocale)
'targetLocale' => $targetLocale,
'sourceLocale' => $sourceLocale
]);
$phraseAppMessage = $this->makeDownloadRequest($sourceLocale, 'yml_symfony2');
file_put_contents($tmpFile, $phraseAppMessage);
$this->downloadedLocales[$sourceLocale] = $tmpFile;

foreach ($this->catalogues as $catalogueName => $tagName) {

$this->logger->notice('Downloading catalogue "{catalogueName}" by tag "{tagName}".', [
'catalogueName' => $catalogueName,
'tagName' => $tagName
]);

$phraseAppMessage = $this->makeDownloadRequest($sourceLocale, 'yml_symfony2', $tagName);
$tmpFile = sprintf('%s/%s.%s.yml', $this->getTmpPath(), $catalogueName, $targetLocale);
file_put_contents($tmpFile, $phraseAppMessage);
$this->downloadedLocales[$sourceLocale][$catalogueName] = $tmpFile;
}

return $this->downloadedLocales[$sourceLocale];
}
Expand Down Expand Up @@ -229,7 +250,7 @@ protected function generateTmpPath()
protected function cleanUp()
{
$finder = new Finder();
$files = $finder->files()->name('messages.*.yml')->in($this->getTmpPath());
$files = $finder->files()->name('*.*.yml')->in($this->getTmpPath());
/** @var SplFileInfo $file */
foreach ($files as $file) {
if (false === @unlink($file)) {
Expand Down

0 comments on commit 058cbde

Please sign in to comment.