From 2662244eb81e8e9a829a7c7ee308c41f6849258f Mon Sep 17 00:00:00 2001 From: Alexander Schwenn Date: Sun, 18 Jan 2015 21:33:17 +0100 Subject: [PATCH 1/3] [FrameworkBundle] Enable translation debugging in directories Harmonize TranslationDebugCommand and TranslationUpdateCommand to expect an optional bundle name or a directory and fall back to kernel root dir if none of them is given. --- .../Command/TranslationDebugCommand.php | 42 +++++++++++++++---- .../Command/TranslationUpdateCommand.php | 21 +++++++--- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php index f5cc1fb805..e10085921c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php @@ -44,16 +44,16 @@ class TranslationDebugCommand extends ContainerAwareCommand )) ->setDefinition(array( new InputArgument('locale', InputArgument::REQUIRED, 'The locale'), - new InputArgument('bundle', InputArgument::REQUIRED, 'The bundle name'), + new InputArgument('bundle', InputArgument::OPTIONAL, 'The bundle name or directory where to load the messages, defaults to app/Resources folder'), new InputOption('domain', null, InputOption::VALUE_OPTIONAL, 'The messages domain'), new InputOption('only-missing', null, InputOption::VALUE_NONE, 'Displays only missing messages'), new InputOption('only-unused', null, InputOption::VALUE_NONE, 'Displays only unused messages'), )) - ->setDescription('Displays translation messages informations') + ->setDescription('Displays translation messages information') ->setHelp(<<%command.name% 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. +templates and translation files of a given bundle or the app folder. You can display information about bundle translations in a specific locale: @@ -71,6 +71,10 @@ You can only display unused messages: php %command.full_name% --only-unused en AcmeDemoBundle +You can display information about app translations in a specific locale: + + php %command.full_name% en + EOF ) ; @@ -87,17 +91,39 @@ EOF $locale = $input->getArgument('locale'); $domain = $input->getOption('domain'); - $bundle = $this->getContainer()->get('kernel')->getBundle($input->getArgument('bundle')); $loader = $this->getContainer()->get('translation.loader'); + $kernel = $this->getContainer()->get('kernel'); + + // Define Root Path to App folder + $rootPath = $kernel->getRootDir(); + + // Override with provided Bundle info + if (null !== $input->getArgument('bundle')) { + try { + $rootPath = $kernel->getBundle($input->getArgument('bundle'))->getPath(); + } catch (\InvalidArgumentException $e) { + // such a bundle does not exist, so treat the argument as path + $rootPath = $input->getArgument('bundle'); + + if (!is_dir($rootPath)) { + throw new \InvalidArgumentException(sprintf('"%s" is neither an enabled bundle nor a directory.', $rootPath)); + } + } + } + + // get bundle directory + $translationsPath = $rootPath.'/Resources/translations'; // Extract used messages $extractedCatalogue = new MessageCatalogue($locale); - $this->getContainer()->get('translation.extractor')->extract($bundle->getPath().'/Resources/views', $extractedCatalogue); + if (is_dir($rootPath.'/Resources/views')) { + $this->getContainer()->get('translation.extractor')->extract($rootPath.'/Resources/views', $extractedCatalogue); + } // Load defined messages $currentCatalogue = new MessageCatalogue($locale); - if (is_dir($bundle->getPath().'/Resources/translations')) { - $loader->loadMessages($bundle->getPath().'/Resources/translations', $currentCatalogue); + if (is_dir($translationsPath)) { + $loader->loadMessages($translationsPath, $currentCatalogue); } // Merge defined and extracted messages to get all message ids @@ -130,7 +156,7 @@ EOF } $fallbackCatalogue = new MessageCatalogue($fallbackLocale); - $loader->loadMessages($bundle->getPath().'/Resources/translations', $fallbackCatalogue); + $loader->loadMessages($translationsPath, $fallbackCatalogue); $fallbackCatalogues[] = $fallbackCatalogue; } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php index f30e9fd833..41464168ea 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php @@ -35,7 +35,7 @@ class TranslationUpdateCommand extends ContainerAwareCommand ->setName('translation:update') ->setDefinition(array( new InputArgument('locale', InputArgument::REQUIRED, 'The locale'), - new InputArgument('bundle', InputArgument::OPTIONAL, 'The bundle where to load the messages, defaults to app/Resources folder', null), + new InputArgument('bundle', InputArgument::OPTIONAL, 'The bundle name or directory where to load the messages, defaults to app/Resources folder'), new InputOption('prefix', null, InputOption::VALUE_OPTIONAL, 'Override the default prefix', '__'), new InputOption('output-format', null, InputOption::VALUE_OPTIONAL, 'Override the default output format', 'yml'), new InputOption('dump-messages', null, InputOption::VALUE_NONE, 'Should the messages be dumped in the console'), @@ -83,16 +83,27 @@ EOF return 1; } + $kernel = $this->getContainer()->get('kernel'); // Define Root Path to App folder - $rootPath = $this->getApplication()->getKernel()->getRootDir(); + $rootPath = $kernel->getRootDir(); $currentName = "app folder"; // Override with provided Bundle info if (null !== $input->getArgument('bundle')) { - $foundBundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle')); - $rootPath = $foundBundle->getPath(); - $currentName = $foundBundle->getName(); + try { + $foundBundle = $kernel->getBundle($input->getArgument('bundle')); + $rootPath = $foundBundle->getPath(); + $currentName = $foundBundle->getName(); + } catch (\InvalidArgumentException $e) { + // such a bundle does not exist, so treat the argument as path + $rootPath = $input->getArgument('bundle'); + $currentName = $rootPath; + + if (!is_dir($rootPath)) { + throw new \InvalidArgumentException(sprintf('"%s" is neither an enabled bundle nor a directory.', $rootPath)); + } + } } // get bundle directory From 21526ea58338cc1b6734d6df697c4a49eaafbf6e Mon Sep 17 00:00:00 2001 From: Abdellatif Ait boudad Date: Mon, 6 Apr 2015 16:49:58 +0100 Subject: [PATCH 2/3] [Translation][Profiler] reduce memory usage for collected messages. --- .../DataCollector/TranslationDataCollector.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Translation/DataCollector/TranslationDataCollector.php b/src/Symfony/Component/Translation/DataCollector/TranslationDataCollector.php index 4e3f54b608..eb9d1e7333 100644 --- a/src/Symfony/Component/Translation/DataCollector/TranslationDataCollector.php +++ b/src/Symfony/Component/Translation/DataCollector/TranslationDataCollector.php @@ -95,21 +95,22 @@ class TranslationDataCollector extends DataCollector implements LateDataCollecto private function sanitizeCollectedMessages($messages) { + $result = array(); foreach ($messages as $key => $message) { - $messages[$key]['translation'] = $this->sanitizeString($message['translation']); - } - - return array_reduce($messages, function ($result, $message) { $messageId = $message['locale'].$message['domain'].$message['id']; + if (!isset($result[$messageId])) { $message['count'] = 1; + $messages[$key]['translation'] = $this->sanitizeString($message['translation']); $result[$messageId] = $message; } else { $result[$messageId]['count']++; } - return $result; - }, array()); + unset($messages[$key]); + } + + return $result; } private function computeCount($messages) From aa82fb065d970c1fd02bf35790e4da8970f45e7b Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 6 Apr 2015 19:27:58 +0200 Subject: [PATCH 3/3] Fix composer.json dependencies --- src/Symfony/Bridge/Twig/composer.json | 2 +- src/Symfony/Bundle/SecurityBundle/composer.json | 2 +- src/Symfony/Bundle/TwigBundle/composer.json | 2 +- src/Symfony/Component/Form/composer.json | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Bridge/Twig/composer.json b/src/Symfony/Bridge/Twig/composer.json index 85f9ea3cc2..3bc4bc8e2a 100644 --- a/src/Symfony/Bridge/Twig/composer.json +++ b/src/Symfony/Bridge/Twig/composer.json @@ -32,7 +32,7 @@ "symfony/yaml": "~2.0,>=2.0.5|~3.0.0", "symfony/security": "~2.6|~3.0.0", "symfony/stopwatch": "~2.2|~3.0.0", - "symfony/console": "~2.4|~3.0.0", + "symfony/console": "~2.7|~3.0.0", "symfony/var-dumper": "~2.6|~3.0.0", "symfony/expression-language": "~2.4|~3.0.0" }, diff --git a/src/Symfony/Bundle/SecurityBundle/composer.json b/src/Symfony/Bundle/SecurityBundle/composer.json index c1c2b73dc5..c5219093e6 100644 --- a/src/Symfony/Bundle/SecurityBundle/composer.json +++ b/src/Symfony/Bundle/SecurityBundle/composer.json @@ -25,7 +25,7 @@ "symfony/browser-kit": "~2.4|~3.0.0", "symfony/console": "~2.5|~3.0.0", "symfony/css-selector": "~2.0,>=2.0.5|~3.0.0", - "symfony/dependency-injection": "~2.3|~3.0.0", + "symfony/dependency-injection": "~2.6,>=2.6.6|~3.0.0", "symfony/dom-crawler": "~2.0,>=2.0.5|~3.0.0", "symfony/form": "~2.7|~3.0.0", "symfony/framework-bundle": "~2.7|~3.0.0", diff --git a/src/Symfony/Bundle/TwigBundle/composer.json b/src/Symfony/Bundle/TwigBundle/composer.json index 962ea81f10..cb8a143bd5 100644 --- a/src/Symfony/Bundle/TwigBundle/composer.json +++ b/src/Symfony/Bundle/TwigBundle/composer.json @@ -25,7 +25,7 @@ "require-dev": { "symfony/phpunit-bridge": "~2.7|~3.0.0", "symfony/stopwatch": "~2.2|~3.0.0", - "symfony/dependency-injection": "~2.2|~3.0.0", + "symfony/dependency-injection": "~2.6,>=2.6.6|~3.0.0", "symfony/expression-language": "~2.4|~3.0.0", "symfony/config": "~2.2|~3.0.0", "symfony/routing": "~2.1|~3.0.0", diff --git a/src/Symfony/Component/Form/composer.json b/src/Symfony/Component/Form/composer.json index 5c10be5cf8..9a0346c2b6 100644 --- a/src/Symfony/Component/Form/composer.json +++ b/src/Symfony/Component/Form/composer.json @@ -33,8 +33,8 @@ }, "conflict": { "symfony/doctrine-bridge": "<2.7", - "symfony/twig-bridge": "<2.7", - "symfony/doctrine-bridge": "<2.7" + "symfony/framework-bundle": "<2.7", + "symfony/twig-bridge": "<2.7" }, "suggest": { "symfony/validator": "For form validation.",