-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bozhidar Hristov
committed
Feb 8, 2017
1 parent
41aaabc
commit 7aa7cb2
Showing
21 changed files
with
434 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ | |
|
||
class OverrideTranslatorCompilerPass implements CompilerPassInterface | ||
{ | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ | |
|
||
class TemplatingCompilerPass implements CompilerPassInterface | ||
{ | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
*/ | ||
class Configuration implements ConfigurationInterface | ||
{ | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.