8000 Add support for translator paths and twig paths in translation commands · symfony/symfony@6077362 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6077362

Browse files
committed
Add support for translator paths and twig paths in translation commands
1 parent 8d277ce commit 6077362

File tree

6 files changed

+40
-16
lines changed

6 files changed

+40
-16
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.3.0
5+
-----
6+
7+
* Add support for Translator paths and Twig paths in translation commands.
8+
49
4.2.0
510
-----
611

src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ class TranslationDebugCommand extends Command
5050
private $extractor;
5151
private $defaultTransPath;
5252
private $defaultViewsPath;
53+
private $transPaths;
54+
private $viewsPaths;
5355

5456
/**
5557
* @param TranslatorInterface $translator
5658
*/
57-
public function __construct($translator, TranslationReaderInterface $reader, ExtractorInterface $extractor, string $defaultTransPath = null, string $defaultViewsPath = null)
59+
public function __construct($translator, TranslationReaderInterface $reader, ExtractorInterface $extractor, string $defaultTransPath = null, string $defaultViewsPath = null, array $transPaths = array(), array $viewsPaths = array())
5860
{
5961
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
6062
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
@@ -66,6 +68,8 @@ public function __construct($translator, TranslationReaderInterface $reader, Ext
6668
$this->extractor = $extractor;
6769
$this->defaultTransPath = $defaultTransPath;
6870
$this->defaultViewsPath = $defaultViewsPath;
71+
$this->transPaths = $transPaths;
72+
$this->viewsPaths = $viewsPaths;
6973
}
7074

7175
/**
@@ -131,7 +135,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
131135
$rootDir = $kernel->getContainer()->getParameter('kernel.root_dir');
132136

133137
// Define Root Paths
134-
$transPaths = array();
138+
$transPaths = $this->transPaths;
135139
if (is_dir($dir = $rootDir.'/Resources/translations')) {
136140
if ($dir !== $this->defaultTransPath) {
137141
$notice = sprintf('Storing translations in the "%s" directory is deprecated since Symfony 4.2, ', $dir);
@@ -142,7 +146,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
142146
if ($this->defaultTransPath) {
143147
$transPaths[] = $this->defaultTransPath;
144148
}
145-
$viewsPaths = array();
149+
$viewsPaths = $this->viewsPaths;
146150
if (is_dir($dir = $rootDir.'/Resources/views')) {
147151
if ($dir !== $this->defaultViewsPath) {
148152
$notice = sprintf('Storing templates in the "%s" directory is deprecated since Symfony 4.2, ', $dir);
@@ -165,7 +169,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
165169
if (is_dir($dir = sprintf('%s/Resources/%s/translations', $rootDir, $bundle->getName()))) {
166170
$transPaths[] = $dir;
167171
$notice = sprintf('Storing translations files for "%s" in the "%s" directory is deprecated since Symfony 4.2, ', $dir, $bundle->getName());
168-
@trigger_error($notice.($this->defaultTransPath ? sprintf('use the "%s" directory instead.', $this->defaultTransPath) : sprintf('configure and use "framework.translator.default_path" instead.', $bundle->getName())), E_USER_DEPRECATED);
172+
@trigger_error($notice.($this->defaultTransPath ? sprintf('use the "%s" directory instead.', $this->defaultTransPath) : 'configure and use "framework.translator.default_path" instead.'), E_USER_DEPRECATED);
169173
}
170174
$viewsPaths = array($bundle->getPath().'/Resources/views');
171175
if ($this->defaultViewsPath) {
@@ -174,7 +178,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
174178
if (is_dir($dir = sprintf('%s/Resources/%s/views', $rootDir, $bundle->getName()))) {
175179
$viewsPaths[] = $dir;
176180
$notice = sprintf('Storing templates for "%s" in the "%s" directory is deprecated since Symfony 4.2, ', $bundle->getName(), $dir);
177-
@trigger_error($notice.($this->defaultViewsPath ? sprintf('use the "%s" directory instead.', $this->defaultViewsPath) : sprintf('configure and use "twig.default_path" instead.', $bundle->getName())), E_USER_DEPRECATED);
181+
@trigger_error($notice.($this->defaultViewsPath ? sprintf('use the "%s" directory instead.', $this->defaultViewsPath) : 'configure and use "twig.default_path" instead.'), E_USER_DEPRECATED);
178182
}
179183
} catch (\InvalidArgumentException $e) {
180184
// such a bundle does not exist, so treat the argument as path
@@ -212,7 +216,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
212216
if (is_dir($deprecatedPath = sprintf('%s/Resources/%s/views', $rootDir, $bundle->getName()))) {
213217
$viewsPaths[] = $deprecatedPath;
214218
$notice = sprintf('Storing templates for "%s" in the "%s" directory is deprecated since Symfony 4.2, ', $bundle->getName(), $deprecatedPath);
215-
@trigger_error($notice.($this->defaultViewsPath ? sprintf('use the "%s" directory instead.', $this->defaultViewsPath) : sprintf('configure and use "twig.default_path" instead.', $bundle->getName())), E_USER_DEPRECATED);
219+
@trigger_error($notice.($this->defaultViewsPath ? sprintf('use the "%s" directory instead.', $this->defaultViewsPath) : 'configure and use "twig.default_path" instead.'), E_USER_DEPRECATED);
216220
}
217221
}
218222
}

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ class TranslationUpdateCommand extends Command
4444
private $defaultLocale;
4545
private $defaultTransPath;
4646
private $defaultViewsPath;
47+
private $transPaths;
48+
private $viewsPaths;
4749

48-
public function __construct(TranslationWriterInterface $writer, TranslationReaderInterface $reader, ExtractorInterface $extractor, string $defaultLocale, string $defaultTransPath = null, string $defaultViewsPath = null)
50+
public function __construct(TranslationWriterInterface $writer, TranslationReaderInterface $reader, ExtractorInterface $extractor, string $defaultLocale, string $defaultTransPath = null, string $defaultViewsPath = null, array $transPaths = array(), array $viewsPaths = array())
4951
{
5052
parent::__construct();
5153

@@ -55,6 +57,8 @@ public function __construct(TranslationWriterInterface $writer, TranslationReade
5557
$this->defaultLocale = $defaultLocale;
5658
$this->defaultTransPath = $defaultTransPath;
5759
$this->defaultViewsPath = $defaultViewsPath;
60+
$this->transPaths = $transPaths;
61+
$this->viewsPaths = $viewsPaths;
5862
}
5963

6064
/**
@@ -111,7 +115,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
111115

112116
// check format
113117
$supportedFormats = $this->writer->getFormats();
114-
if (!\in_array($input->getOption('output-format'), $supportedFormats)) {
118+
if (!\in_array($input->getOption('output-format'), $supportedFormats, true)) {
115119
$errorIo->error(array('Wrong output format', 'Supported formats are: '.implode(', ', $supportedFormats).'.'));
116120

117121
return 1;
@@ -121,7 +125,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
121125
$rootDir = $kernel->getContainer()->getParameter('kernel.root_dir');
122126

123127
// Define Root Paths
124-
$transPaths = array();
128+
$transPaths = $this->transPaths;
125129
if (is_dir($dir = $rootDir.'/Resources/translations')) {
126130
if ($dir !== $this->defaultTransPath) {
127131
$notice = sprintf('Storing translations in the "%s" directory is deprecated since Symfony 4.2, ', $dir);
@@ -132,7 +136,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
132136
if ($this->defaultTransPath) {
133137
$transPaths[] = $this->defaultTransPath;
134138
}
135-
$viewsPaths = array();
139+
$viewsPaths = $this->viewsPaths;
136140
if (is_dir($dir = $rootDir.'/Resources/views')) {
137141
if ($dir !== $this->defaultViewsPath) {
138142
$notice = sprintf('Storing templates in the "%s" directory is deprecated since Symfony 4.2, ', $dir);
@@ -165,7 +169,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
165169
if (is_dir($dir = sprintf('%s/Resources/%s/views', $rootDir, $foundBundle->getName()))) {
166170
$viewsPaths[] = $dir;
167171
$notice = sprintf('Storing templates for "%s" in the "%s" directory is deprecated since Symfony 4.2, ', $foundBundle->getName(), $dir);
168-
@trigger_error($notice.($this->defaultViewsPath ? sprintf('use the "%s" directory instead.', $this->defaultViewsPath) : sprintf('configure and use "twig.default_path" instead.', $foundBundle->getName())), E_USER_DEPRECATED);
172+
@trigger_error($notice.($this->defaultViewsPath ? sprintf('use the "%s" directory instead.', $this->defaultViewsPath) : 'configure and use "twig.default_path" instead.'), E_USER_DEPRECATED);
169173
}
170174
$currentName = $foundBundle->getName();
171175
} catch (\InvalidArgumentException $e) {
@@ -261,7 +265,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
261265
$extractedMessagesCount += $domainMessagesCount;
262266
}
263267

264-
if ('xlf' == $input->getOption('output-format')) {
268+
if ('xlf' === $input->getOption('output-format')) {
265269
$errorIo->comment('Xliff output version is <info>1.2</info>');
266270
}
267271

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -986,20 +986,21 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
986986

987987
// Discover translation directories
988988
$dirs = array();
989+
$transPaths = array();
989990
if (class_exists('Symfony\Component\Validator\Validation')) {
990991
$r = new \ReflectionClass('Symfony\Component\Validator\Validation');
991992

992-
$dirs[] = \dirname($r->getFileName()).'/Resources/translations';
993+
$dirs[] = $transPaths[] = \dirname($r->getFileName()).'/Resources/translations';
993994
}
994995
if (class_exists('Symfony\Component\Form\Form')) {
995996
$r = new \ReflectionClass('Symfony\Component\Form\Form');
996997

997-
$dirs[] = \dirname($r->getFileName()).'/Resources/translations';
998+
$dirs[] = $transPaths[] = \dirname($r->getFileName()).'/Resources/translations';
998999
}
9991000
if (class_exists('Symfony\Component\Security\Core\Exception\AuthenticationException')) {
10001001
$r = new \ReflectionClass('Symfony\Component\Security\Core\Exception\AuthenticationException');
10011002

1002-
$dirs[] = \dirname(\dirname($r->getFileName())).'/Resources/translations';
1003+
$dirs[] = $transPaths[] = \dirname(\dirname($r->getFileName())).'/Resources/translations';
10031004
}
10041005
$defaultDir = $container->getParameterBag()->resolveValue($config['default_path']);
10051006
$rootDir = $container->getParameter('kernel.root_dir');
@@ -1016,11 +1017,13 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
10161017

10171018
foreach ($config['paths'] as $dir) {
10181019
if ($container->fileExists($dir)) {
1019-
$dirs[] = $dir;
1020+
$dirs[] = $transPaths[] = $dir;
10201021
} else {
10211022
throw new \UnexpectedValueException(sprintf('%s defined in translator.paths does not exist or is not a directory', $dir));
10221023
}
10231024
}
1025+
$container->getDefinition('console.command.translation_debug')->replaceArgument(5, $transPaths);
1026+
$container->getDefinition('console.command.translation_update')->replaceArgument(6, $transPaths);
10241027

10251028
if ($container->fileExists($defaultDir)) {
10261029
$dirs[] = $defaultDir;

src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@
100100
<argument type="service" id="translation.extractor" />
101101
<argument>%translator.default_path%</argument>
102102
<argument /> <!-- %twig.default_path% -->
103+
<argument type="collection" /> <!-- Translator paths -->
104+
<argument type="collection" /> <!-- Twig paths -->
103105
<tag name="console.command" command="debug:translation" />
104106
</service>
105107

@@ -110,6 +112,8 @@
110112
<argument>%kernel.default_locale%</argument>
111113
<argument>%translator.default_path%</argument>
112114
<argument /> <!-- %twig.default_path% -->
115+
<argument type="collection" /> <!-- Translator paths -->
116+
<argument type="collection" /> <!-- Twig paths -->
113117
<tag name="console.command" command="translation:update" />
114118
</service>
115119

src/Symfony/Component/Translation/DependencyInjection/TranslatorPass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,16 @@ public function process(ContainerBuilder $container)
6868
return;
6969
}
7070

71+
$paths = $container->getDefinition('twig.template_iterator')->getArgument(2);
72+
7173
if ($container->hasDefinition($this->debugCommandServiceId)) {
7274
$container->getDefinition($this->debugCommandServiceId)->replaceArgument(4, $container->getParameter('twig.default_path'));
75+
$container->getDefinition($this->debugCommandServiceId)->replaceArgument(6, $paths);
7376
}
7477

7578
if ($container->hasDefinition($this->updateCommandServiceId)) {
7679
$container->getDefinition($this->updateCommandServiceId)->replaceArgument(5, $container->getParameter('twig.default_path'));
80+
$container->getDefinition($this->updateCommandServiceId)->replaceArgument(7, $paths);
7781
}
7882
}
7983
}

0 commit comments

Comments
 (0)
0