Skip to content

Commit

Permalink
Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Bozhidar Hristov committed Feb 8, 2017
1 parent 41aaabc commit 7aa7cb2
Show file tree
Hide file tree
Showing 21 changed files with 434 additions and 122 deletions.
145 changes: 145 additions & 0 deletions src/Command/ExportTranslationsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace ObjectBG\TranslationBundle\Command;

use ObjectBG\TranslationBundle\Entity\Language;
use ObjectBG\TranslationBundle\Entity\TranslationToken;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Translation\Catalogue\MergeOperation;
use Symfony\Component\Translation\Catalogue\TargetOperation;
use Symfony\Component\Translation\MessageCatalogue;

class ExportTranslationsCommand extends ContainerAwareCommand
{

/**
* @var InputInterface
*/
private $input;

/**
* @var OutputInterface
*/
private $output;
private $io;

protected function configure()
{
$this
->setName('objectbg:translation:export')
->setDefinition(
array(
new InputArgument('locale', InputArgument::REQUIRED, 'The locale'),
new InputArgument(
'bundle',
InputArgument::OPTIONAL,
'The bundle name or directory where to load the messages, defaults to app/Resources folder'
),
new InputOption('format', 'f', InputOption::VALUE_OPTIONAL, 'Force the output format.', 'xlf'),
new InputOption('clean', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_NONE),
)
);
}

public function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$this->io = new SymfonyStyle($input, $output);
$kernel = $this->getContainer()->get('kernel');

$format = $this->input->getOption('format');
$clean = $this->input->getOption('clean');
$locale = $this->input->getArgument('locale');

$bundleName = $this->input->getArgument('bundle');
if ($bundleName) {
$bundle = $kernel->getBundle($bundleName);
$transPaths = $bundle->getPath() . '/Resources/translations';
}
$this->exportFile($transPaths, $locale, $format, $clean);
}

private function exportFile($transPaths, $locale, $format, $clean)
{
$db = $this->exportFromDB($locale);
$language = $this->getContainer()->get('doctrine')->getManager()->getRepository(Language::class)->findOneBy(
['locale' => $locale]
);
$currentCatalogue = $this->extractMessages($locale, $transPaths);
$extractedCatalogue = new MessageCatalogue($locale);
if ($db != null) {
foreach ($db as $token) {
$translation = $token->getTranslation($language)->getTranslation();
if (!$translation) {
$translation = $token->getToken();
}
$extractedCatalogue->set($token->getToken(), $translation, $token->getCatalogue());
}
} else {
$this->output->writeln('<comment>No translations to export.</comment>');

return;
}
$writer = $this->getContainer()->get('translation.writer');
$supportedFormats = $writer->getFormats();
if (!in_array($format, $supportedFormats)) {
$this->io->error(
array('Wrong output format', 'Supported formats are: ' . implode(', ', $supportedFormats) . '.')
);

return 1;
}

$operation = $clean ? new TargetOperation($currentCatalogue, $extractedCatalogue) : new MergeOperation(
$currentCatalogue, $extractedCatalogue
);

$writer->writeTranslations(
$operation->getResult(),
$format,
array(
'path' => $transPaths,
'default_locale' => $this->getContainer()->getParameter('kernel.default_locale'),
)
);
}

/**
*
* @param string $locale
* @return TranslationToken[]
*/
private function exportFromDB($locale)
{
$em = $this->getContainer()->get('doctrine')->getManager();

return $em->getRepository(TranslationToken::class)->getAllTokensByLocale($locale);
}

/**
*
* @param type $locale
* @param type $transPaths
* @return MessageCatalogue
*/
private function extractMessages($locale, $transPaths)
{
/** @var TranslationLoader $loader */
$loader = $this->getContainer()->get('translation.loader');
$currentCatalogue = new MessageCatalogue($locale);

if (is_dir($transPaths)) {
$loader->loadMessages($transPaths, $currentCatalogue);
}

return $currentCatalogue;
}

}
155 changes: 155 additions & 0 deletions src/Command/ImportTranslationsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace ObjectBG\TranslationBundle\Command;

use ObjectBG\TranslationBundle\Entity\Language;
use ObjectBG\TranslationBundle\Entity\Translation;
use ObjectBG\TranslationBundle\Entity\TranslationRepository;
use ObjectBG\TranslationBundle\Entity\TranslationToken;
use ObjectBG\TranslationBundle\Entity\TranslationTokenRepository;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Translation\MessageCatalogue;

class ImportTranslationsCommand extends ContainerAwareCommand
{

/**
* @var InputInterface
*/
private $input;

/**
* @var OutputInterface
*/
private $output;

protected function configure()
{
$this
->setName('objectbg:translation:import')
->setDefinition(
array(
new InputArgument('locale', InputArgument::REQUIRED, 'The locale'),
new InputArgument(
'bundle',
InputArgument::OPTIONAL,
'The bundle name or directory where to load the messages, defaults to app/Resources folder'
),
new InputOption('all', null, InputOption::VALUE_NONE, 'Load messages from all registered bundles'),
new InputOption('override', null, InputOption::VALUE_NONE, 'Should the update be done'),
)
)
->setDescription('Displays translation messages information')
->setHelp(
<<<'EOF'
The <info>%command.name%</info> command helps finding unused or missing translation
messages and comparing them with the fallback ones by inspecting the
templates and translation files of a given bundle or the app folder.
You can display information about bundle translations in a specific locale:
<info>php %command.full_name% en AcmeDemoBundle</info>
You can also specify a translation domain for the search:
<info>php %command.full_name% --domain=messages en AcmeDemoBundle</info>
You can display information about app translations in a specific locale:
<info>php %command.full_name% en</info>
You can display information about translations in all registered bundles in a specific locale:
<info>php %command.full_name% --all en</info>
EOF
);
}

public function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;

$kernel = $this->getContainer()->get('kernel');
$transPaths = array($kernel->getRootDir() . '/Resources/');

$locale = $this->input->getArgument('locale');
$override = $this->input->getOption('override');


$bundleName = $this->input->getArgument('bundle');
if ($bundleName) {
$bundle = $kernel->getBundle($bundleName);
$transPaths[] = $bundle->getPath() . '/Resources/';
$transPaths[] = sprintf('%s/Resources/%s/', $kernel->getRootDir(), $bundle->getName());
} elseif ($input->getOption('all')) {
foreach ($kernel->getBundles() as $bundle) {
$transPaths[] = $bundle->getPath() . '/Resources/';
$transPaths[] = sprintf('%s/Resources/%s/', $kernel->getRootDir(), $bundle->getName());
}
}

$catalogue = $this->extractMessages($locale, $transPaths);
$this->importTranslationFiles($catalogue, $locale, $override);
}

/**
* @param string $locale
* @param array $transPaths
*
* @return MessageCatalogue
*/
private function extractMessages($locale, $transPaths)
{
/** @var TranslationLoader $loader */
$loader = $this->getContainer()->get('translation.loader');

$currentCatalogue = new MessageCatalogue($locale);
foreach ($transPaths as $path) {
$path = $path . 'translations';
if (is_dir($path)) {
$loader->loadMessages($path, $currentCatalogue);
}
}

return $currentCatalogue;
}

public function importTranslationFiles(MessageCatalogue $messages, $locale, $override)
{
$domains = $messages->all();
$translationToken = null;
$translation = null;

$em = $this->getContainer()->get('doctrine')->getManager();
$language = $em->getRepository(Language::class)->findOneBy(['locale' => $locale]);

/** @var TranslationTokenRepository $transTokenRepo */
$transTokenRepo = $em->getRepository(TranslationToken::class);
/** @var TranslationRepository $transRepo */
$transRepo = $em->getRepository(Translation::class);

foreach ($domains as $catalogue => $messages) {
foreach ($messages as $token => $val) {
$translationToken = $transTokenRepo->findByTokenAndCatalogue($token, $catalogue);
if (!$translationToken) {
$translationToken = new TranslationToken();
$translationToken->setToken($token);
$translationToken->setCatalogue($catalogue);
} else {
$translation = $transRepo->getTranslationByTokenAndLanguage($translationToken, $language);
}

if (!$translation || $override) {
if (!$translation) {
$translation = new Translation();
}
$translation->setLanguage($language);
$translation->setTranslationToken($translationToken);
$translation->setTranslation($val);
$em->persist($translationToken);
$em->persist($translation);
}
}
}
$em->flush();
}

}
17 changes: 6 additions & 11 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace ObjectBG\TranslationBundle\Controller;

use ObjectBG\TranslationBundle\Entity\Translation;
use Sonata\AdminBundle\Controller\CRUDController as BaseCRUDController;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\HttpFoundation\Request;

class CRUDController extends \Sonata\AdminBundle\Controller\CRUDController
class CRUDController extends BaseCRUDController
{

public function listAction(\Symfony\Component\HttpFoundation\Request $request = null)
public function listAction(Request $request = null)
{
$canEdit = $this->admin->isGranted('EDIT');
$canView = $this->admin->isGranted('LIST');
Expand Down Expand Up @@ -66,14 +69,6 @@ public function listAction(\Symfony\Component\HttpFoundation\Request $request =
$tokens = $qb->getQuery()->getResult();

$formBuilder = $this->createFormBuilder();
// $FormBuilder->add('tokens', 'collection', array(
// 'type' => 'text',
// 'label' => false,
// 'allow_add' => true,
// 'options' => array(
// 'label' => false
// )
// ));

$formBuilder->add(
'translations',
Expand Down Expand Up @@ -151,7 +146,7 @@ function ($item) use ($token, $language) {
continue;
}
if (!$translation) {
$translation = new \ObjectBG\TranslationBundle\Entity\Translation();
$translation = new Translation();
$translation->setLanguage($language);
$translation->setTranslationToken($token);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class OverrideTranslatorCompilerPass implements CompilerPassInterface
{

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class TemplatingCompilerPass implements CompilerPassInterface
{

/**
* {@inheritdoc}
*/
Expand Down
1 change: 0 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
class Configuration implements ConfigurationInterface
{

/**
* {@inheritDoc}
*/
Expand Down
3 changes: 1 addition & 2 deletions src/DependencyInjection/ObjectBGTranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
class ObjectBGTranslationExtension extends Extension
{

/**
* {@inheritDoc}
*/
Expand All @@ -23,7 +22,7 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
$loader->load('admins.xml');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Doctrine/Filter/LanguageFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAli
return "";
}

return $targetTableAlias.'.locale = '.$this->getParameter('locale');
return $targetTableAlias . '.locale = ' . $this->getParameter('locale');
}
}
Loading

0 comments on commit 7aa7cb2

Please sign in to comment.