Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed compatibility with Symfony 3.2.x and Twig 2.x #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
namespace Lsw\GettextTranslationBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use Symfony\Component\Process\Process;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
Expand Down Expand Up @@ -97,25 +93,43 @@ protected function convertTwigToPhp($path, $name)
mkdir($dir, 0755, true);
}

$templates = array();
if ( $name === 'app' ) {
if ($name === 'app') {
$templates = $this->findFilesInFolder($dir . '/../../', 'twig');
} else {
$templates = $this->findFilesInFolder($dir . '/../views', 'twig');
}

$php = "<?php\n";
$twig = $this->getContainer()->get('twig');
$twig->setLoader(new \Twig_Loader_String());

/** @var \Twig_Loader_Filesystem $loader */
$loader = $twig->getLoader();

foreach ($templates as $templateFileName) {
$stream = $twig->tokenize(file_get_contents($templateFileName));
$filename = $templateFileName;
if (0 === mb_strpos($filename, 'app/Resources/views/')) {
$filename = mb_substr($filename, 20);
} elseif ($name !== 'app') {
$filename = $templateFileName;
$bundleViewsDir = $name . '/Resources/views';
$filnamePos = mb_strpos($filename, $bundleViewsDir);
if ($filnamePos === false) {
continue;
}

$filnamePos += mb_strlen($bundleViewsDir) + 1;
$loader->addPath(mb_substr($filename, 0, $filnamePos));
$filename = mb_substr($filename, $filnamePos);
}

$stream = $twig->tokenize(new \Twig_Source(file_get_contents($templateFileName), $filename));
$nodes = $twig->parse($stream);
$template = $twig->compile($nodes);
// remove first line
$template = substr($template, strpos($template, "\n")+strlen("\n"));
$php .= "/*\n * Resource: $name\n * File: $templateFileName\n */\n";
$php .= $template;
$results[$templateFileName]='Scanned';
$results[$templateFileName] = 'Scanned';
}

if (!file_put_contents($path, $php)) {
Expand All @@ -142,15 +156,15 @@ protected function extractFromPhp($path)
}

// clean .tmp file for further using it as cache
if (file_exists("$path.tmp")) {
if (file_exists("$path.tmp")) {
unlink("$path.tmp");
}

// find all *.php files in the given directory
$files = $this->findFilesInFolder(dirname($path) . '/../..', 'php');

// define options for finding translation strings within *.php files
$options = implode(' ',array(
$options = implode(' ', array(
'--keyword=__:1',
'--keyword=__n:1,2',
'--keyword=_:1',
Expand All @@ -163,7 +177,7 @@ protected function extractFromPhp($path)
));

$process = new Process('xgettext '.$options);
$process->setStdin(implode("\n", $files));
$process->setInput(implode("\n", $files));
$process->run();
$output = $process->getOutput();
if (!$process->isSuccessful()) {
Expand Down Expand Up @@ -242,7 +256,7 @@ protected function combineFiles(array $files, $path)
// clean .tmp file for further using it as cache
if (!file_exists(dirname($path))) {
mkdir(dirname($path), 0755, true);
} else if (file_exists("$path.tmp")) {
} elseif (file_exists("$path.tmp")) {
unlink("$path.tmp");
}

Expand All @@ -255,7 +269,7 @@ protected function combineFiles(array $files, $path)
));

$process = new Process('msgcat '.$options);
$process->setStdin(implode("\n", $files));
$process->setInput(implode("\n", $files));
$process->run();
$output = $process->getOutput();

Expand Down
38 changes: 22 additions & 16 deletions Command/CombineAllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use Symfony\Component\Console\Question\Question;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;

Expand Down Expand Up @@ -69,17 +70,19 @@ protected function configure()
* @param OutputInterface $output Output interface
*
* @see Command
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$root = $this->getContainer()->getParameter('kernel.root_dir');
chdir($root.'/..');

$configFile = $root."/Resources/gettext/version";
$configFile = $root . '/Resources/gettext/version';
$languages = explode(',', trim($input->getArgument('languages'), ','));
$bundles = $this->getContainer()->get('kernel')->getBundles();
$version = file_exists($configFile) ? file_get_contents($configFile) : "";
$newVersion = $input->getOption('increase-version') ? "_" . strtotime("now") : $version;
$newVersion = $input->getOption('increase-version') ? '_' . time() : $version;

foreach ($languages as $lang) {
$lang = trim($lang);
Expand Down Expand Up @@ -108,7 +111,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$path = "$root/Resources/gettext/combined/$lang/LC_MESSAGES/messages$newVersion.mo";
$results = $this->compile($file, $path);
foreach ($results as $filename => $status) {
$output->writeln("$status: $filename");
$output->writeln("$status: $filename");
}

if (!$input->getOption('keep-messages')) {
Expand All @@ -117,7 +120,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

if ($version != $newVersion) {
if (!file_put_contents($configFile, $newVersion)) {
$output->writeln("Version was not saved: " . $newVersion);
$output->writeln('Version was not saved: ' . $newVersion);
}

if (file_exists("$root/Resources/gettext/combined/$lang/LC_MESSAGES/messages$version.po")) {
Expand All @@ -130,6 +133,8 @@ protected function execute(InputInterface $input, OutputInterface $output)

}

return 0;

//http://www.gnu.org/software/gettext/manual/html_node/xgettext-Invocation.html
}

Expand All @@ -140,23 +145,24 @@ protected function execute(InputInterface $input, OutputInterface $output)
* @param OutputInterface $output Output interface
*
* @see Command
* @return mixed
* @return void
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
if (!$input->getArgument('languages')) {
$languages = $this->getHelper('dialog')->askAndValidate(
$output,
'Please enter the list of languages (comma seperated):',
function($languages)
{
if (empty($languages)) {
throw new \Exception('Language list can not be empty');
}

return $languages;
$questionHelper = $this->getHelper('question');
$languageQuestion = new Question('Please enter the list of languages (comma seperated): ');
$languageQuestion->setValidator(function ($languages) {
if (empty($languages)) {
throw new \RuntimeException(
'Language list can not be empty'
);
}
);

return $languages;
});

$languages = $questionHelper->ask($input, $output, $languageQuestion);
$input->setArgument('languages', $languages);
}
}
Expand Down
30 changes: 18 additions & 12 deletions Command/ExtractBundleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use Symfony\Component\Console\Question\Question;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;

Expand Down Expand Up @@ -59,6 +60,8 @@ protected function configure()
* @param OutputInterface $output Output interface
*
* @see Command
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand All @@ -84,6 +87,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (!$input->getOption('keep-cache')) {
unlink($twig);
}

return 0;
}

/**
Expand All @@ -93,23 +98,24 @@ protected function execute(InputInterface $input, OutputInterface $output)
* @param OutputInterface $output Output interface
*
* @see Command
* @return mixed
* @return void
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
if (!$input->getArgument('bundle')) {
$bundle = $this->getHelper('dialog')->askAndValidate(
$output,
'Please give the bundle:',
function($bundle)
{
if (empty($bundle)) {
throw new \Exception('Bundle can not be empty');
}

return $bundle;
$questionHelper = $this->getHelper('question');
$bundleQuestion = new Question('Please give the bundle: ');
$bundleQuestion->setValidator(function ($bundle) {
if (empty($bundle)) {
throw new \RuntimeException(
'Bundle can not be empty'
);
}
);

return $bundle;
});

$bundle = $questionHelper->ask($input, $output, $bundleQuestion);
$input->setArgument('bundle', $bundle);
}
}
Expand Down
35 changes: 18 additions & 17 deletions Command/InitializeApplicationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

namespace Lsw\GettextTranslationBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
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\Finder\Finder;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Console\Question\Question;

/**
* InitializeBundleCommand extracts records to be translated from the current application
Expand Down Expand Up @@ -54,6 +50,8 @@ protected function configure()
* @param OutputInterface $output Output interface
*
* @see Command
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand All @@ -65,6 +63,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
foreach ($results as $filename => $status) {
$output->writeln("$status: $filename");
}

return 0;
}

/**
Expand All @@ -74,23 +74,24 @@ protected function execute(InputInterface $input, OutputInterface $output)
* @param OutputInterface $output Output interface
*
* @see Command
* @return mixed
* @return void
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
if (!$input->getArgument('languages')) {
$languages = $this->getHelper('dialog')->askAndValidate(
$output,
'Please enter the list of languages (comma seperated):',
function($languages)
{
if (empty($languages)) {
throw new \Exception('Language list can not be empty');
}

return $languages;
$questionHelper = $this->getHelper('question');
$question = new Question('Please enter the list of languages (comma seperated): ');
$question->setValidator(function ($languages) {
if (empty($languages)) {
throw new \RuntimeException(
'Language list can not be empty'
);
}
);

return $languages;
});

$languages = $questionHelper->ask($input, $output, $question);
$input->setArgument('languages', $languages);
}
}
Expand Down
Loading