PageRenderTime 20ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php

https://gitlab.com/cuza/Clinic_Recods
PHP | 216 lines | 150 code | 35 blank | 31 comment | 16 complexity | 773016cdf50f96d4043c1e67831dc995 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Command;
  11. use Symfony\Component\Console\Style\SymfonyStyle;
  12. use Symfony\Component\Translation\Catalogue\TargetOperation;
  13. use Symfony\Component\Translation\Catalogue\MergeOperation;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Input\InputArgument;
  17. use Symfony\Component\Console\Input\InputOption;
  18. use Symfony\Component\Translation\MessageCatalogue;
  19. /**
  20. * A command that parses templates to extract translation messages and adds them
  21. * into the translation files.
  22. *
  23. * @author Michel Salib <michelsalib@hotmail.com>
  24. */
  25. class TranslationUpdateCommand extends ContainerAwareCommand
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function configure()
  31. {
  32. $this
  33. ->setName('translation:update')
  34. ->setDefinition(array(
  35. new InputArgument('locale', InputArgument::REQUIRED, 'The locale'),
  36. new InputArgument('bundle', InputArgument::OPTIONAL, 'The bundle name or directory where to load the messages, defaults to app/Resources folder'),
  37. new InputOption('prefix', null, InputOption::VALUE_OPTIONAL, 'Override the default prefix', '__'),
  38. new InputOption('output-format', null, InputOption::VALUE_OPTIONAL, 'Override the default output format', 'yml'),
  39. new InputOption('dump-messages', null, InputOption::VALUE_NONE, 'Should the messages be dumped in the console'),
  40. new InputOption('force', null, InputOption::VALUE_NONE, 'Should the update be done'),
  41. new InputOption('no-backup', null, InputOption::VALUE_NONE, 'Should backup be disabled'),
  42. new InputOption('clean', null, InputOption::VALUE_NONE, 'Should clean not found messages'),
  43. ))
  44. ->setDescription('Updates the translation file')
  45. ->setHelp(<<<'EOF'
  46. The <info>%command.name%</info> command extracts translation strings from templates
  47. of a given bundle or the app folder. It can display them or merge the new ones into the translation files.
  48. When new translation strings are found it can automatically add a prefix to the translation
  49. message.
  50. Example running against a Bundle (AcmeBundle)
  51. <info>php %command.full_name% --dump-messages en AcmeBundle</info>
  52. <info>php %command.full_name% --force --prefix="new_" fr AcmeBundle</info>
  53. Example running against app messages (app/Resources folder)
  54. <info>php %command.full_name% --dump-messages en</info>
  55. <info>php %command.full_name% --force --prefix="new_" fr</info>
  56. EOF
  57. )
  58. ;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. protected function execute(InputInterface $input, OutputInterface $output)
  64. {
  65. $io = new SymfonyStyle($input, $output);
  66. // check presence of force or dump-message
  67. if ($input->getOption('force') !== true && $input->getOption('dump-messages') !== true) {
  68. $io->error('You must choose one of --force or --dump-messages');
  69. return 1;
  70. }
  71. // check format
  72. $writer = $this->getContainer()->get('translation.writer');
  73. $supportedFormats = $writer->getFormats();
  74. if (!in_array($input->getOption('output-format'), $supportedFormats)) {
  75. $io->error(array('Wrong output format', 'Supported formats are: '.implode(', ', $supportedFormats).'.'));
  76. return 1;
  77. }
  78. $kernel = $this->getContainer()->get('kernel');
  79. // Define Root Path to App folder
  80. $transPaths = array($kernel->getRootDir().'/Resources/');
  81. $currentName = 'app folder';
  82. // Override with provided Bundle info
  83. if (null !== $input->getArgument('bundle')) {
  84. try {
  85. $foundBundle = $kernel->getBundle($input->getArgument('bundle'));
  86. $transPaths = array(
  87. $foundBundle->getPath().'/Resources/',
  88. sprintf('%s/Resources/%s/', $kernel->getRootDir(), $foundBundle->getName()),
  89. );
  90. $currentName = $foundBundle->getName();
  91. } catch (\InvalidArgumentException $e) {
  92. // such a bundle does not exist, so treat the argument as path
  93. $transPaths = array($input->getArgument('bundle').'/Resources/');
  94. $currentName = $transPaths[0];
  95. if (!is_dir($transPaths[0])) {
  96. throw new \InvalidArgumentException(sprintf('<error>"%s" is neither an enabled bundle nor a directory.</error>', $transPaths[0]));
  97. }
  98. }
  99. }
  100. $io->title('Translation Messages Extractor and Dumper');
  101. $io->comment(sprintf('Generating "<info>%s</info>" translation files for "<info>%s</info>"', $input->getArgument('locale'), $currentName));
  102. // load any messages from templates
  103. $extractedCatalogue = new MessageCatalogue($input->getArgument('locale'));
  104. $io->comment('Parsing templates...');
  105. $extractor = $this->getContainer()->get('translation.extractor');
  106. $extractor->setPrefix($input->getOption('prefix'));
  107. foreach ($transPaths as $path) {
  108. $path .= 'views';
  109. if (is_dir($path)) {
  110. $extractor->extract($path, $extractedCatalogue);
  111. }
  112. }
  113. // load any existing messages from the translation files
  114. $currentCatalogue = new MessageCatalogue($input->getArgument('locale'));
  115. $io->comment('Loading translation files...');
  116. $loader = $this->getContainer()->get('translation.loader');
  117. foreach ($transPaths as $path) {
  118. $path .= 'translations';
  119. if (is_dir($path)) {
  120. $loader->loadMessages($path, $currentCatalogue);
  121. }
  122. }
  123. // process catalogues
  124. $operation = $input->getOption('clean')
  125. ? new TargetOperation($currentCatalogue, $extractedCatalogue)
  126. : new MergeOperation($currentCatalogue, $extractedCatalogue);
  127. // Exit if no messages found.
  128. if (!count($operation->getDomains())) {
  129. $io->warning('No translation messages were found.');
  130. return;
  131. }
  132. $resultMessage = 'Translation files were successfully updated';
  133. // show compiled list of messages
  134. if (true === $input->getOption('dump-messages')) {
  135. $extractedMessagesCount = 0;
  136. $io->newLine();
  137. foreach ($operation->getDomains() as $domain) {
  138. $newKeys = array_keys($operation->getNewMessages($domain));
  139. $allKeys = array_keys($operation->getMessages($domain));
  140. $domainMessagesCount = count($newKeys) + count($allKeys);
  141. $io->section(sprintf('Messages extracted for domain "<info>%s</info>" (%d messages)', $domain, $domainMessagesCount));
  142. $io->listing(array_merge(
  143. array_diff($allKeys, $newKeys),
  144. array_map(function ($id) {
  145. return sprintf('<fg=green>%s</>', $id);
  146. }, $newKeys),
  147. array_map(function ($id) {
  148. return sprintf('<fg=red>%s</>', $id);
  149. }, array_keys($operation->getObsoleteMessages($domain)))
  150. ));
  151. $extractedMessagesCount += $domainMessagesCount;
  152. }
  153. if ($input->getOption('output-format') == 'xlf') {
  154. $io->comment('Xliff output version is <info>1.2</info>');
  155. }
  156. $resultMessage = sprintf('%d messages were successfully extracted', $extractedMessagesCount);
  157. }
  158. if ($input->getOption('no-backup') === true) {
  159. $writer->disableBackup();
  160. }
  161. // save the files
  162. if ($input->getOption('force') === true) {
  163. $io->comment('Writing files...');
  164. $bundleTransPath = false;
  165. foreach ($transPaths as $path) {
  166. $path .= 'translations';
  167. if (is_dir($path)) {
  168. $bundleTransPath = $path;
  169. }
  170. }
  171. if (!$bundleTransPath) {
  172. $bundleTransPath = end($transPaths).'translations';
  173. }
  174. $writer->writeTranslations($operation->getResult(), $input->getOption('output-format'), array('path' => $bundleTransPath, 'default_locale' => $this->getContainer()->getParameter('kernel.default_locale')));
  175. if (true === $input->getOption('dump-messages')) {
  176. $resultMessage .= ' and translation files were updated';
  177. }
  178. }
  179. $io->success($resultMessage.'.');
  180. }
  181. }