Skip to content

Commit 047e1df

Browse files
rdohmsfabpot
authored andcommitted
[FrameworkBundle] Enabled translation:update for app folder
Changed the Bundle requirement to allow the command to also be executed for the app/Resources folder of the application, which currently cannot be achieved.
1 parent 792c4b5 commit 047e1df

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

Command/TranslationUpdateCommand.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected function configure()
3535
->setName('translation:update')
3636
->setDefinition(array(
3737
new InputArgument('locale', InputArgument::REQUIRED, 'The locale'),
38-
new InputArgument('bundle', InputArgument::REQUIRED, 'The bundle where to load the messages'),
38+
new InputArgument('bundle', InputArgument::OPTIONAL, 'The bundle where to load the messages, defaults to app/Resources folder', null),
3939
new InputOption(
4040
'prefix', null, InputOption::VALUE_OPTIONAL,
4141
'Override the default prefix', '__'
@@ -64,13 +64,18 @@ protected function configure()
6464
->setDescription('Updates the translation file')
6565
->setHelp(<<<EOF
6666
The <info>%command.name%</info> command extract translation strings from templates
67-
of a given bundle. It can display them or merge the new ones into the translation files.
67+
of a given bundle or the app folder. It can display them or merge the new ones into the translation files.
6868
When new translation strings are found it can automatically add a prefix to the translation
6969
message.
7070
71+
Example running against a Bundle (AcmeBundle)
7172
<info>php %command.full_name% --dump-messages en AcmeBundle</info>
7273
<info>php %command.full_name% --force --prefix="new_" fr AcmeBundle</info>
7374
75+
Example running against app messages (app/Resources folder)
76+
<info>php %command.full_name% --dump-messages en</info>
77+
<info>php %command.full_name% --force --prefix="new_" fr</info>
78+
7479
EOF
7580
)
7681
;
@@ -98,23 +103,33 @@ protected function execute(InputInterface $input, OutputInterface $output)
98103
return 1;
99104
}
100105

106+
// Define Root Path to App folder
107+
$rootPath = $this->getApplication()->getKernel()->getRootDir();
108+
$currentName = "app folder";
109+
110+
// Override with provided Bundle info
111+
if (null !== $input->getArgument('bundle')) {
112+
$foundBundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle'));
113+
$rootPath = $foundBundle->getPath();
114+
$currentName = $foundBundle->getName();
115+
}
116+
101117
// get bundle directory
102-
$foundBundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle'));
103-
$bundleTransPath = $foundBundle->getPath().'/Resources/translations';
104-
$output->writeln(sprintf('Generating "<info>%s</info>" translation files for "<info>%s</info>"', $input->getArgument('locale'), $foundBundle->getName()));
118+
$translationsPath = $rootPath.'/Resources/translations';
119+
$output->writeln(sprintf('Generating "<info>%s</info>" translation files for "<info>%s</info>"', $input->getArgument('locale'), $currentName));
105120

106121
// load any messages from templates
107122
$extractedCatalogue = new MessageCatalogue($input->getArgument('locale'));
108123
$output->writeln('Parsing templates');
109124
$extractor = $this->getContainer()->get('translation.extractor');
110125
$extractor->setPrefix($input->getOption('prefix'));
111-
$extractor->extract($foundBundle->getPath().'/Resources/views/', $extractedCatalogue);
126+
$extractor->extract($rootPath.'/Resources/views/', $extractedCatalogue);
112127

113128
// load any existing messages from the translation files
114129
$currentCatalogue = new MessageCatalogue($input->getArgument('locale'));
115130
$output->writeln('Loading translation files');
116131
$loader = $this->getContainer()->get('translation.loader');
117-
$loader->loadMessages($bundleTransPath, $currentCatalogue);
132+
$loader->loadMessages($translationsPath, $currentCatalogue);
118133

119134
// process catalogues
120135
$operation = $input->getOption('clean')
@@ -150,7 +165,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
150165
// save the files
151166
if ($input->getOption('force') === true) {
152167
$output->writeln('Writing files');
153-
$writer->writeTranslations($operation->getResult(), $input->getOption('output-format'), array('path' => $bundleTransPath, 'default_locale' => $this->getContainer()->getParameter('kernel.default_locale')));
168+
$writer->writeTranslations($operation->getResult(), $input->getOption('output-format'), array('path' => $translationsPath, 'default_locale' => $this->getContainer()->getParameter('kernel.default_locale')));
154169
}
155170
}
156171
}

0 commit comments

Comments
 (0)