Skip to content

Commit

Permalink
add importer
Browse files Browse the repository at this point in the history
  • Loading branch information
Radhi Guennichi committed May 11, 2020
1 parent 9d8aed2 commit 4aecdc6
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 45 deletions.
8 changes: 4 additions & 4 deletions Command/TranslationImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

namespace CosaVostra\LocaliseBundle\Command;

use CosaVostra\LocaliseBundle\Exporter\Exception\InvalidExporterException;
use CosaVostra\LocaliseBundle\Exporter\Registry;
use CosaVostra\LocaliseBundle\Importer\Exception\InvalidExporterException;
use CosaVostra\LocaliseBundle\Importer\Registry;
use CosaVostra\LocaliseBundle\Helper\TranslationPurger;
use CosaVostra\LocaliseBundle\Http\Request;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -69,7 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$io = new SymfonyStyle($input, $output);

try {
$exporter = $this->registry->get($input->getOption('extension') ?? 'yaml');
$importer = $this->registry->get($input->getOption('extension') ?? 'yaml');
} catch (InvalidExporterException $exception) {
$io->error($exception->getMessage());
return 1;
Expand All @@ -84,7 +84,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

foreach ($this->request->getTags() as $tag) {
foreach ($this->request->getLocales() as $locale) {
$exporter->export($locale['code'], $tag);
$importer->import($locale['code'], $tag);
// TODO: print information about the exported file path?
}
}
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/Compiler/ExporterRegistryPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace CosaVostra\LocaliseBundle\DependencyInjection\Compiler;

use CosaVostra\LocaliseBundle\Exporter\Registry;
use CosaVostra\LocaliseBundle\Importer\Registry;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
Expand All @@ -24,7 +24,7 @@ class ExporterRegistryPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container)
{
$exporters = $container->findTaggedServiceIds('localise.exporter');
$exporters = $container->findTaggedServiceIds('localise.importer');
$registry = $container->getDefinition(Registry::class);

foreach ($exporters as $class => $attrs) {
Expand Down
6 changes: 3 additions & 3 deletions DependencyInjection/CosaVostraLocaliseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace CosaVostra\LocaliseBundle\DependencyInjection;

use CosaVostra\LocaliseBundle\Exporter\ExporterInterface;
use CosaVostra\LocaliseBundle\Importer\ImporterInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
Expand All @@ -25,8 +25,8 @@ class CosaVostraLocaliseExtension extends Extension
*/
public function load(array $configs, ContainerBuilder $container)
{
$container->registerForAutoconfiguration(ExporterInterface::class)
->addTag('localise.exporter');
$container->registerForAutoconfiguration(ImporterInterface::class)
->addTag('localise.importer');

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

declare(strict_types=1);

namespace CosaVostra\LocaliseBundle\Exporter\Exception;
namespace CosaVostra\LocaliseBundle\Importer\Exception;

use InvalidArgumentException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

declare(strict_types=1);

namespace CosaVostra\LocaliseBundle\Exporter;
namespace CosaVostra\LocaliseBundle\Importer;

interface ExporterInterface
interface ImporterInterface
{
/**
* The exporter file extension.
Expand All @@ -29,5 +29,5 @@ public static function getExtension(): string;
*
* @return string The full path of the translation file.
*/
public function export(string $locale, string $tag): string;
public function import(string $locale, string $tag): string;
}
6 changes: 3 additions & 3 deletions Exporter/PhpExporter.php → Importer/PhpImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

declare(strict_types=1);

namespace CosaVostra\LocaliseBundle\Exporter;
namespace CosaVostra\LocaliseBundle\Importer;

use CosaVostra\LocaliseBundle\Helper\FilenameGenerator;
use CosaVostra\LocaliseBundle\Http\Request;
use Symfony\Component\Filesystem\Filesystem;

class PhpExporter implements ExporterInterface
class PhpImporter implements ImporterInterface
{
/**
* @var Request
Expand Down Expand Up @@ -51,7 +51,7 @@ public static function getExtension(): string
/**
* @inheritDoc
*/
public function export(string $locale, string $tag): string
public function import(string $locale, string $tag): string
{
$this->filesystem->dumpFile(
$path = $this->filenameGenerator->getFilename($tag, $locale, static::getExtension()),
Expand Down
10 changes: 5 additions & 5 deletions Exporter/Registry.php → Importer/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@

declare(strict_types=1);

namespace CosaVostra\LocaliseBundle\Exporter;
namespace CosaVostra\LocaliseBundle\Importer;

use CosaVostra\LocaliseBundle\Exporter\Exception\InvalidExporterException;
use CosaVostra\LocaliseBundle\Importer\Exception\InvalidExporterException;

class Registry
{
/**
* @var ExporterInterface[]
* @var ImporterInterface[]
*/
protected $exporters = [];

public function add(ExporterInterface $exporter): void
public function add(ImporterInterface $exporter): void
{
$this->exporters[$exporter->getExtension()] = $exporter;
}

public function get(string $extension): ExporterInterface
public function get(string $extension): ImporterInterface
{
if (!array_key_exists($extension, $this->exporters)) {
throw new InvalidExporterException("Exporter with the extension \"$extension\" is not supported.");
Expand Down
6 changes: 3 additions & 3 deletions Exporter/XlfExporter.php → Importer/XlfImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

declare(strict_types=1);

namespace CosaVostra\LocaliseBundle\Exporter;
namespace CosaVostra\LocaliseBundle\Importer;

use CosaVostra\LocaliseBundle\Helper\FilenameGenerator;
use CosaVostra\LocaliseBundle\Http\Request;
use Symfony\Component\Filesystem\Filesystem;

class XlfExporter implements ExporterInterface
class XlfImporter implements ImporterInterface
{
/**
* @var Request
Expand Down Expand Up @@ -51,7 +51,7 @@ public static function getExtension(): string
/**
* @inheritDoc
*/
public function export(string $locale, string $tag): string
public function import(string $locale, string $tag): string
{
$this->filesystem->dumpFile(
$path = $this->filenameGenerator->getFilename($tag, $locale, static::getExtension()),
Expand Down
6 changes: 3 additions & 3 deletions Exporter/YamlExporter.php → Importer/YamlImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

declare(strict_types=1);

namespace CosaVostra\LocaliseBundle\Exporter;
namespace CosaVostra\LocaliseBundle\Importer;

use CosaVostra\LocaliseBundle\Helper\FilenameGenerator;
use CosaVostra\LocaliseBundle\Http\Request;
use Symfony\Component\Filesystem\Filesystem;

class YamlExporter implements ExporterInterface
class YamlImporter implements ImporterInterface
{
/**
* @var Request
Expand Down Expand Up @@ -51,7 +51,7 @@ public static function getExtension(): string
/**
* @inheritDoc
*/
public function export(string $locale, string $tag): string
public function import(string $locale, string $tag): string
{
$this->filesystem->dumpFile(
$path = $this->filenameGenerator->getFilename($tag, $locale, static::getExtension()),
Expand Down
8 changes: 4 additions & 4 deletions LocaliseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

namespace CosaVostra\LocaliseBundle;

use CosaVostra\LocaliseBundle\Exporter\Exception\InvalidExporterException;
use CosaVostra\LocaliseBundle\Exporter\Registry;
use CosaVostra\LocaliseBundle\Importer\Exception\InvalidExporterException;
use CosaVostra\LocaliseBundle\Importer\Registry;
use CosaVostra\LocaliseBundle\Helper\TranslationPurger;
use CosaVostra\LocaliseBundle\Http\Request;

Expand Down Expand Up @@ -47,7 +47,7 @@ public function __construct(Registry $exporterRegistry, Request $localiseRequest
*
* @throws InvalidExporterException
*/
public function export(string $extension = 'yaml', bool $purge = false): void
public function import(string $extension = 'yaml', bool $purge = false): void
{
$exporter = $this->exporterRegistry->get($extension);

Expand All @@ -57,7 +57,7 @@ public function export(string $extension = 'yaml', bool $purge = false): void

foreach ($this->localiseRequest->getTags() as $tag) {
foreach ($this->localiseRequest->getLocales() as $locale) {
$exporter->export($locale['code'], $tag);
$exporter->import($locale['code'], $tag);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ LOCALISE_EXPORT_KEY=XXXXXXXXXXXXXXX
Usage
=====

This bundle is very useful to export translation files from Localise.biz, after installation you'll be able
This bundle is very useful to import translation files from Localise.biz, after installation you'll be able
to access to the command:

```console
Expand Down Expand Up @@ -84,10 +84,10 @@ and consider using the right environment option, in your prod environment you sh
- XLF

```console
$ php bin/console --env=prod localise:translation:export --extension=yaml --purge
$ php bin/console --env=prod localise:translation:import --extension=yaml --purge
```

You can also use the `CosaVostra\LocaliseBundle\LocaliseManager` service to export translations manually
You can also use the `CosaVostra\LocaliseBundle\LocaliseManager` service to import translations manually
(for example in a controller) like below:

```php
Expand All @@ -99,11 +99,11 @@ public function action(LocaliseManager $localiseManager): Response
$extension = 'yaml';
$purge = true; // This flag should be "TRUE" to purge translation directory and remove old files.

$localiseManager->export($extension, $purge);
$localiseManager->import($extension, $purge);

// Clear the cache manually here ...

return new Response('Translations exported.');
return new Response('Translations imported.');
}
```

Expand Down
18 changes: 9 additions & 9 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@
<argument type="service" id="parameter_bag"/>
</service>

<service id="CosaVostra\LocaliseBundle\Exporter\Registry" public="false"/>
<service id="CosaVostra\LocaliseBundle\Importer\Registry" public="false"/>

<service id="CosaVostra\LocaliseBundle\Exporter\YamlExporter">
<service id="CosaVostra\LocaliseBundle\Importer\YamlImporter">
<argument type="service" id="CosaVostra\LocaliseBundle\Http\Request"/>
<argument type="service" id="filesystem"/>
<argument type="service" id="CosaVostra\LocaliseBundle\Helper\FilenameGenerator"/>
<tag name="localise.exporter"/>
<tag name="localise.importer"/>
</service>

<service id="CosaVostra\LocaliseBundle\Exporter\XlfExporter" public="false">
<service id="CosaVostra\LocaliseBundle\Importer\XlfImporter" public="false">
<argument type="service" id="CosaVostra\LocaliseBundle\Http\Request"/>
<argument type="service" id="filesystem"/>
<argument type="service" id="CosaVostra\LocaliseBundle\Helper\FilenameGenerator"/>
<tag name="localise.exporter"/>
<tag name="localise.importer"/>
</service>

<service id="CosaVostra\LocaliseBundle\Exporter\PhpExporter">
<service id="CosaVostra\LocaliseBundle\Importer\PhpImporter">
<argument type="service" id="CosaVostra\LocaliseBundle\Http\Request"/>
<argument type="service" id="filesystem"/>
<argument type="service" id="CosaVostra\LocaliseBundle\Helper\FilenameGenerator"/>
<tag name="localise.exporter"/>
<tag name="localise.importer"/>
</service>

<service id="CosaVostra\LocaliseBundle\Helper\TranslationPurger">
Expand All @@ -53,13 +53,13 @@
</service>

<service id="CosaVostra\LocaliseBundle\LocaliseManager">
<argument type="service" id="CosaVostra\LocaliseBundle\Exporter\Registry"/>
<argument type="service" id="CosaVostra\LocaliseBundle\Importer\Registry"/>
<argument type="service" id="CosaVostra\LocaliseBundle\Http\Request"/>
<argument type="service" id="CosaVostra\LocaliseBundle\Helper\TranslationPurger"/>
</service>

<service id="CosaVostra\LocaliseBundle\Command\TranslationImportCommand">
<argument type="service" id="CosaVostra\LocaliseBundle\Exporter\Registry"/>
<argument type="service" id="CosaVostra\LocaliseBundle\Importer\Registry"/>
<argument type="service" id="CosaVostra\LocaliseBundle\Http\Request"/>
<argument type="service" id="CosaVostra\LocaliseBundle\Helper\TranslationPurger"/>
<tag name="console.command"/>
Expand Down

0 comments on commit 4aecdc6

Please sign in to comment.