Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Translation] added extract messages from controllers #26738

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

Closed
wants to merge 7 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
use Symfony\Component\Translation\Writer\TranslationWriterInterface;
use Symfony\Component\Routing\RouterInterface;

/**
* A command that parses templates to extract translation messages and adds them
Expand All @@ -43,8 +44,10 @@ class TranslationUpdateCommand extends Command
private $defaultLocale;
private $defaultTransPath;
private $defaultViewsPath;
private $router;
private $defaultControllersPath;

public function __construct(TranslationWriterInterface $writer, TranslationReaderInterface $reader, ExtractorInterface $extractor, string $defaultLocale, string $defaultTransPath = null, string $defaultViewsPath = null)
public function __construct(TranslationWriterInterface $writer, TranslationReaderInterface $reader, ExtractorInterface $extractor, string $defaultLocale, string $defaultTransPath = null, string $defaultViewsPath = null, RouterInterface $router = null, array $defaultControllersPath = array())
{
parent::__construct();

Expand All @@ -54,6 +57,8 @@ public function __construct(TranslationWriterInterface $writer, TranslationReade
$this->defaultLocale = $defaultLocale;
$this->defaultTransPath = $defaultTransPath;
$this->defaultViewsPath = $defaultViewsPath;
$this->router = $router;
$this->defaultControllersPath = $defaultControllersPath;
}

/**
Expand Down Expand Up @@ -127,6 +132,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath;
}
$controllersPaths = array($kernel->getRootDir().'/Controller');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if my controllers are somewhere else?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iltar you're right. I added a parameter to constructor (as for the views). But I'm not sure how to test it. Can you help me?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about making the parameter an array of paths? That would cover multiple locations. You can test it by writing fixtures for your test, writing controllers with translations.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An array of paths sounds great to me, but for the tests I wonder how to set path(s) when I call the command on CLI (bin/console translation:update)?

When I try your suggestion, it returns null for $this->router.
I'm not sure it's OK to have a behaviour for templates (parameter in constructor) and another behaviour for controllers (find auto).

if ($this->defaultControllersPath) {
$controllersPaths = array_merge($controllersPaths, $this->defaultControllersPath);
}
if ($this->router instanceof RouterInterface) {
foreach ($this->router->getRouteCollection() as $route) {
if (!$route->hasDefault('_controller')) {
continue;
}
$controllersPaths[] = dirname($route->getPath());
}
$controllersPaths = array_unique($controllersPaths);
}

$currentName = 'app folder';

// Override with provided Bundle info
Expand Down Expand Up @@ -169,6 +188,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

// load any messages from controllers
$errorIo->comment('Parsing controllers...');
foreach ($controllersPaths as $path) {
if (is_dir($path)) {
$this->extractor->extract($path, $extractedCatalogue);
}
}

// load any existing messages from the translation files
$currentCatalogue = new MessageCatalogue($input->getArgument('locale'));
$errorIo->comment('Loading translation files...');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Symfony\Bundle\FrameworkBundle\Command\TranslationUpdateCommand;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;

class TranslationUpdateCommandTest extends TestCase
{
Expand Down Expand Up @@ -95,6 +97,7 @@ protected function setUp()
$this->fs->mkdir($this->translationDir.'/Resources/views');
$this->fs->mkdir($this->translationDir.'/translations');
$this->fs->mkdir($this->translationDir.'/templates');
$this->fs->mkdir($this->translationDir.'/controllers');
}

protected function tearDown()
Expand Down Expand Up @@ -179,7 +182,13 @@ private function createCommandTester($extractedMessages = array(), $loadedMessag
->method('getContainer')
->will($this->returnValue($this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock()));

$command = new TranslationUpdateCommand($writer, $loader, $extractor, 'en', $this->translationDir.'/translations', $this->translationDir.'/templates');
$router = $this->getMockBuilder(RouterInterface::class)->getMock();
$router
->expects($this->any())
->method('getRouteCollection')
->will($this->returnValue(new RouteCollection()));

$command = new TranslationUpdateCommand($writer, $loader, $extractor, 'en', $this->translationDir.'/translations', $this->translationDir.'/templates', $router, array($this->translationDir.'/controllers'));

$application = new Application($kernel);
$application->add($command);
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.