Skip to content

Commit 14bff67

Browse files
committed
bug #59113 [FrameworkBundle][Translation] fix translation lint compatibility with the PseudoLocalizationTranslator (xabbuh)
This PR was merged into the 7.2 branch. Discussion ---------- [FrameworkBundle][Translation] fix translation lint compatibility with the `PseudoLocalizationTranslator` | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #59101 | License | MIT Commits ------- 2695871 fix translation lint compatibility with the PseudoLocalizationTranslator
2 parents 7183de2 + 2695871 commit 14bff67

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\Translation\TranslatorBagInterface;
17+
use Symfony\Contracts\Translation\TranslatorInterface;
18+
19+
final class TranslationLintCommandPass implements CompilerPassInterface
20+
{
21+
public function process(ContainerBuilder $container): void
22+
{
23+
if (!$container->hasDefinition('console.command.translation_lint') || !$container->has('translator')) {
24+
return;
25+
}
26+
27+
$translatorClass = $container->getParameterBag()->resolveValue($container->findDefinition('translator')->getClass());
28+
29+
if (!is_subclass_of($translatorClass, TranslatorInterface::class) || !is_subclass_of($translatorClass, TranslatorBagInterface::class)) {
30+
$container->removeDefinition('console.command.translation_lint');
31+
}
32+
}
33+
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RemoveUnusedSessionMarshallingHandlerPass;
2020
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerRealRefPass;
2121
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerWeakRefPass;
22+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationLintCommandPass;
2223
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass;
2324
use Symfony\Bundle\FrameworkBundle\DependencyInjection\VirtualRequestStackPass;
2425
use Symfony\Component\Cache\Adapter\ApcuAdapter;
@@ -149,6 +150,8 @@ public function build(ContainerBuilder $container): void
149150
$this->addCompilerPassIfExists($container, AddConstraintValidatorsPass::class);
150151
$this->addCompilerPassIfExists($container, AddValidatorInitializersPass::class);
151152
$this->addCompilerPassIfExists($container, AddConsoleCommandPass::class, PassConfig::TYPE_BEFORE_REMOVING);
153+
// must be registered before the AddConsoleCommandPass
154+
$container->addCompilerPass(new TranslationLintCommandPass(), PassConfig::TYPE_BEFORE_REMOVING, 10);
152155
// must be registered as late as possible to get access to all Twig paths registered in
153156
// twig.template_iterator definition
154157
$this->addCompilerPassIfExists($container, TranslatorPass::class, PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);

src/Symfony/Component/Translation/PseudoLocalizationTranslator.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111

1212
namespace Symfony\Component\Translation;
1313

14+
use Symfony\Component\Translation\Exception\LogicException;
1415
use Symfony\Contracts\Translation\TranslatorInterface;
1516

1617
/**
1718
* This translator should only be used in a development environment.
1819
*/
19-
final class PseudoLocalizationTranslator implements TranslatorInterface
20+
final class PseudoLocalizationTranslator implements TranslatorInterface, TranslatorBagInterface
2021
{
2122
private const EXPANSION_CHARACTER = '~';
2223

@@ -115,6 +116,24 @@ public function getLocale(): string
115116
return $this->translator->getLocale();
116117
}
117118

119+
public function getCatalogue(?string $locale = null): MessageCatalogueInterface
120+
{
121+
if (!$this->translator instanceof TranslatorBagInterface) {
122+
throw new LogicException(\sprintf('The "%s()" method cannot be called as the wrapped translator class "%s" does not implement the "%s".', __METHOD__, $this->translator::class, TranslatorBagInterface::class));
123+
}
124+
125+
return $this->translator->getCatalogue($locale);
126+
}
127+
128+
public function getCatalogues(): array
129+
{
130+
if (!$this->translator instanceof TranslatorBagInterface) {
131+
throw new LogicException(\sprintf('The "%s()" method cannot be called as the wrapped translator class "%s" does not implement the "%s".', __METHOD__, $this->translator::class, TranslatorBagInterface::class));
132+
}
133+
134+
return $this->translator->getCatalogues();
135+
}
136+
118137
private function getParts(string $originalTrans): array
119138
{
120139
if (!$this->parseHTML) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy