Skip to content

Commit

Permalink
Merge pull request #384 from bartmcleod/5.0
Browse files Browse the repository at this point in the history
Updated many things after testing in actual Symfony 5 project
  • Loading branch information
bartmcleod authored Feb 4, 2021
2 parents e6500c5 + b872a5d commit 940300f
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 72 deletions.
78 changes: 44 additions & 34 deletions Command/ExportTranslationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace Lexik\Bundle\TranslationBundle\Command;

use Lexik\Bundle\TranslationBundle\Manager\FileInterface;
use Lexik\Bundle\TranslationBundle\Storage\StorageInterface;
use Lexik\Bundle\TranslationBundle\Translation\Translator;
use Lexik\Bundle\TranslationBundle\Translation\Exporter\ExporterCollector;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Lexik\Bundle\TranslationBundle\Manager\FileInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Export translations from the database in to files.
Expand All @@ -18,22 +19,28 @@
*/
class ExportTranslationsCommand extends Command
{
/**
* @var InputInterface
*/
private $input;

/**
* @var OutputInterface
*/
private $output;
private InputInterface $input;
private OutputInterface $output;
private StorageInterface $storage;
private Translator $translator;
private TranslatorInterface $translator;
private string $projectDir;
private Filesystem $fileSystem;
private ExporterCollector $exporterCollector;

public function __construct(
StorageInterface $storage,
TranslatorInterface $translator,
FileSystem $fileSystem,
ExporterCollector $exporterCollector,
string $projectDir
) {
parent::__construct();

public function __construct(StorageInterface $storage, Translator $translator)
{
$this->storage = $storage;
$this->translator = $translator;
$this->projectDir = $projectDir;
$this->fileSystem = $fileSystem;
$this->exporterCollector = $exporterCollector;
}

/**
Expand All @@ -44,10 +51,13 @@ protected function configure()
$this->setName('lexik:translations:export');
$this->setDescription('Export translations from the database to files.');

$this->addOption('locales', 'l', InputOption::VALUE_OPTIONAL, 'Only export files for given locales. e.g. "--locales=en,de"', null);
$this->addOption('domains', 'd', InputOption::VALUE_OPTIONAL, 'Only export files for given domains. e.g. "--domains=messages,validators"', null);
$this->addOption('locales', 'l', InputOption::VALUE_OPTIONAL,
'Only export files for given locales. e.g. "--locales=en,de"', null);
$this->addOption('domains', 'd', InputOption::VALUE_OPTIONAL,
'Only export files for given domains. e.g. "--domains=messages,validators"', null);
$this->addOption('format', 'f', InputOption::VALUE_OPTIONAL, 'Force the output format.', null);
$this->addOption('override', 'o', InputOption::VALUE_NONE, 'Only export modified phrases (app/Resources/translations are exported fully anyway)');
$this->addOption('override', 'o', InputOption::VALUE_NONE,
'Only export modified phrases (app/Resources/translations are exported fully anyway)');
$this->addOption('export-path', 'p', InputOption::VALUE_REQUIRED, 'Export files to given path.');
}

Expand All @@ -65,9 +75,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
foreach ($filesToExport as $file) {
$this->exportFile($file);
}
} else {
$this->output->writeln('<comment>No translation\'s files in the database.</comment>');

return 0;
}

$this->output->writeln('<comment>No translation\'s files in the database.</comment>');

return 1;
}

/**
Expand All @@ -77,12 +91,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
protected function getFilesToExport()
{
$locales = $this->input->getOption('locales') ? explode(',', $this->input->getOption('locales')) : array();
$domains = $this->input->getOption('domains') ? explode(',', $this->input->getOption('domains')) : array();
$locales = $this->input->getOption('locales') ? explode(',', $this->input->getOption('locales')) : [];
$domains = $this->input->getOption('domains') ? explode(',', $this->input->getOption('domains')) : [];

return $this->getContainer()
->get('lexik_translation.translation_storage')
->getFilesByLocalesAndDomains($locales, $domains);
return $this->storage->getFilesByLocalesAndDomains($locales, $domains);
}

/**
Expand All @@ -92,7 +104,7 @@ protected function getFilesToExport()
*/
protected function exportFile(FileInterface $file)
{
$rootDir = $this->input->getOption('export-path') ? $this->input->getOption('export-path') . '/' : $this->getContainer()->getParameter('kernel.project_dir');
$rootDir = $this->input->getOption('export-path') ? $this->input->getOption('export-path') . '/' : $this->projectDir;

$this->output->writeln(sprintf('<info># Exporting "%s/%s":</info>', $file->getPath(), $file->getName()));
$override = $this->input->getOption('override');
Expand Down Expand Up @@ -129,10 +141,8 @@ protected function exportFile(FileInterface $file)

// ensure the path exists
if ($this->input->getOption('export-path')) {
/** @var Filesystem $fs */
$fs = $this->getContainer()->get('filesystem');
if (!$fs->exists($outputPath)) {
$fs->mkdir($outputPath);
if (!$this->fileSystem->exists($outputPath)) {
$this->fileSystem->mkdir($outputPath);
}
}

Expand All @@ -147,15 +157,15 @@ protected function exportFile(FileInterface $file)
* If the output file exists we merge existing translations with those from the database.
*
* @param FileInterface $file
* @param string $outputFile
* @param array $translations
* @param string $outputFile
* @param array $translations
* @return array
*/
protected function mergeExistingTranslations($file, $outputFile, $translations)
{
if (file_exists($outputFile)) {
$extension = pathinfo($outputFile, PATHINFO_EXTENSION);
$loader = $this->getContainer()->get('lexik_translation.translator')->getLoader($extension);
$loader = $this->translator->getLoader($extension);
$messageCatalogue = $loader->load($outputFile, $file->getLocale(), $file->getDomain());

$translations = array_merge($messageCatalogue->all($file->getDomain()), $translations);
Expand All @@ -168,7 +178,7 @@ protected function mergeExistingTranslations($file, $outputFile, $translations)
* Export translations.
*
* @param string $outputFile
* @param array $translations
* @param array $translations
* @param string $format
*/
protected function doExport($outputFile, $translations, $format)
Expand All @@ -177,7 +187,7 @@ protected function doExport($outputFile, $translations, $format)
$this->output->write(sprintf('<comment>%d translations to export: </comment>', count($translations)));

try {
$exported = $this->getContainer()->get('lexik_translation.exporter_collector')->export(
$exported = $this->exporterCollector->export(
$format,
$outputFile,
$translations
Expand Down
10 changes: 5 additions & 5 deletions Command/ImportTranslationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Lexik\Bundle\TranslationBundle\Manager\LocaleManager;
use Lexik\Bundle\TranslationBundle\Translation\Importer\FileImporter;
use Lexik\Bundle\TranslationBundle\Translation\Translator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -13,6 +12,7 @@
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Imports translation files content in the database.
Expand All @@ -23,16 +23,16 @@
*/
class ImportTranslationsCommand extends Command
{
private Translator $translator;
private TranslatorInterface $translator;

private LocaleManager $localeManager;
private FileImporter $fileImporter;

/**
* @param Translator $translator
* @param TranslatorInterface $translator
*/
public function __construct(
Translator $translator,
TranslatorInterface $translator,
LocaleManager $localeManager,
FileImporter $fileImporter
)
Expand Down Expand Up @@ -299,7 +299,7 @@ protected function findTranslationsFiles($path, array $locales, array $domains,
}

if (true === $autocompletePath) {
$dir = (0 === strpos($path, $this->getApplication()->getKernel()->getRootDir() . '/Resources')) ? $path : $path . '/Resources/translations';
$dir = (0 === strpos($path, $this->getApplication()->getKernel()->getProjectDir() . '/Resources')) ? $path : $path . '/Resources/translations';
} else {
$dir = $path;
}
Expand Down
8 changes: 4 additions & 4 deletions Controller/RestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Lexik\Bundle\TranslationBundle\Controller;

use Lexik\Bundle\TranslationBundle\Util\Csrf\CsrfCheckerTrait;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

/**
* @author Cédric Girard <[email protected]>
*/
class RestController extends Controller
class RestController extends AbstractController
{
use CsrfCheckerTrait;

Expand All @@ -21,7 +21,7 @@ class RestController extends Controller
*/
public function listAction(Request $request)
{
list($transUnits, $count) = $this->get('lexik_translation.data_grid.request_handler')->getPage($request);
[$transUnits, $count] = $this->get('lexik_translation.data_grid.request_handler')->getPage($request);

return $this->get('lexik_translation.data_grid.formatter')->createListResponse($transUnits, $count);
}
Expand All @@ -34,7 +34,7 @@ public function listAction(Request $request)
*/
public function listByProfileAction(Request $request, $token)
{
list($transUnits, $count) = $this->get('lexik_translation.data_grid.request_handler')->getPageByToken($request, $token);
[$transUnits, $count] = $this->get('lexik_translation.data_grid.request_handler')->getPageByToken($request, $token);

return $this->get('lexik_translation.data_grid.formatter')->createListResponse($transUnits, $count);
}
Expand Down
4 changes: 2 additions & 2 deletions Controller/TranslationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
use Lexik\Bundle\TranslationBundle\Form\Type\TransUnitType;
use Lexik\Bundle\TranslationBundle\Storage\StorageInterface;
use Lexik\Bundle\TranslationBundle\Util\Csrf\CsrfCheckerTrait;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

/**
* @author Cédric Girard <[email protected]>
*/
class TranslationController extends Controller
class TranslationController extends AbstractController
{
use CsrfCheckerTrait;

Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/Compiler/TranslatorPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public function process(ContainerBuilder $container)
}
}

if ($container->hasDefinition('lexik_translation.importer.file')) {
$container->findDefinition('lexik_translation.importer.file')->replaceArgument(0, $loadersReferences);
if ($container->hasDefinition('Lexik\Bundle\TranslationBundle\Translation\Importer\FileImporter')) {
$container->findDefinition('Lexik\Bundle\TranslationBundle\Translation\Importer\FileImporter')->replaceArgument(0, $loadersReferences);
}

// exporters
Expand Down
10 changes: 5 additions & 5 deletions Resources/config/routing/api.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
lexik_translation_list:
path: /translations
defaults: { _controller: "LexikTranslationBundle:Rest:list" }
defaults: { _controller: 'Lexik\Bundle\TranslationBundle\Controller\RestController:listAction' }
methods: [GET]

lexik_translation_profiler:
path: /translations/profiler/{token}
defaults: { _controller: "LexikTranslationBundle:Rest:listByProfile" }
defaults: { _controller: 'Lexik\Bundle\TranslationBundle\Controller\RestController:listByProfileAction' }
methods: [GET]

lexik_translation_update:
path: /translations/{id}
defaults: { _controller: "LexikTranslationBundle:Rest:update" }
defaults: { _controller: 'Lexik\Bundle\TranslationBundle\Controller\RestController:updateAction' }
methods: [PUT]

lexik_translation_delete_locale:
path: /translations/{id}/{locale}
defaults: { _controller: "LexikTranslationBundle:Rest:deleteTranslation" }
defaults: { _controller: 'Lexik\Bundle\TranslationBundle\Controller\RestController:deleteTranslationAction' }
methods: [DELETE]

lexik_translation_delete:
path: /translations/{id}
defaults: { _controller: "LexikTranslationBundle:Rest:delete" }
defaults: { _controller: 'Lexik\Bundle\TranslationBundle\Controller\RestController:deleteAction' }
methods: [DELETE]
8 changes: 4 additions & 4 deletions Resources/config/routing/app.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
lexik_translation_overview:
path: /
defaults: { _controller: "LexikTranslationBundle:Translation:overview" }
defaults: { _controller: 'Lexik\Bundle\TranslationBundle\Controller\TranslationController:overviewAction' }
methods: [GET]

lexik_translation_grid:
path: /grid
defaults: { _controller: "LexikTranslationBundle:Translation:grid" }
defaults: { _controller: 'Lexik\Bundle\TranslationBundle\Controller\TranslationController:gridAction' }
methods: [GET]

lexik_translation_new:
path: /new
defaults: { _controller: "LexikTranslationBundle:Translation:new" }
defaults: { _controller: 'Lexik\Bundle\TranslationBundle\Controller\TranslationController:newAction' }
methods: [GET, POST]

lexik_translation_invalidate_cache:
path: /invalidate-cache
defaults: { _controller: "LexikTranslationBundle:Translation:invalidateCache" }
defaults: { _controller: 'Lexik\Bundle\TranslationBundle\Controller\TranslationController:invalidateCacheAction' }
methods: [GET]
Loading

0 comments on commit 940300f

Please sign in to comment.