Skip to content

Commit d3c6f40

Browse files
committed
ACP2E-1999: add possibility to translate config error messages by emulating default theme (backend) and locale (en_US).
1 parent 8158fb6 commit d3c6f40

File tree

7 files changed

+1456
-48
lines changed

7 files changed

+1456
-48
lines changed

app/code/Magento/Config/Console/Command/ConfigShowCommand.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,19 @@ class ConfigShowCommand extends Command
9393
*/
9494
private $emulatedAreaProcessor;
9595

96+
/**
97+
* @var LocaleEmulatorInterface|mixed
98+
*/
99+
private mixed $localeEmulator;
100+
96101
/**
97102
* @param ValidatorInterface $scopeValidator
98103
* @param ConfigSourceInterface $configSource
99104
* @param ConfigPathResolver $pathResolver
100105
* @param ValueProcessor $valueProcessor
101106
* @param PathValidatorFactory|null $pathValidatorFactory
102107
* @param EmulatedAdminhtmlAreaProcessor|null $emulatedAreaProcessor
108+
* @param LocaleEmulatorInterface|null $localeEmulator
103109
* @internal param ScopeConfigInterface $appConfig
104110
*/
105111
public function __construct(
@@ -108,7 +114,8 @@ public function __construct(
108114
ConfigPathResolver $pathResolver,
109115
ValueProcessor $valueProcessor,
110116
?PathValidatorFactory $pathValidatorFactory = null,
111-
?EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor = null
117+
?EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor = null,
118+
?LocaleEmulatorInterface $localeEmulator = null
112119
) {
113120
parent::__construct();
114121
$this->scopeValidator = $scopeValidator;
@@ -119,6 +126,8 @@ public function __construct(
119126
?: ObjectManager::getInstance()->get(PathValidatorFactory::class);
120127
$this->emulatedAreaProcessor = $emulatedAreaProcessor
121128
?: ObjectManager::getInstance()->get(EmulatedAdminhtmlAreaProcessor::class);
129+
$this->localeEmulator = $localeEmulator
130+
?: ObjectManager::getInstance()->get(LocaleEmulatorInterface::class);
122131
}
123132

124133
/**
@@ -194,7 +203,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
194203
$this->outputResult($output, $configValue, $this->inputPath);
195204
return Cli::RETURN_SUCCESS;
196205
} catch (\Exception $e) {
197-
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
206+
$this->localeEmulator->emulate(
207+
fn () => $output->writeln(sprintf('<error>%s</error>', __($e->getMessage()))),
208+
);
198209
return Cli::RETURN_FAILURE;
199210
}
200211
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Config\Console\Command;
9+
10+
use Magento\Framework\Locale\ResolverInterface;
11+
use Magento\Framework\Phrase;
12+
use Magento\Framework\Phrase\RendererInterface;
13+
use Magento\Framework\TranslateInterface;
14+
use Magento\Theme\Model\View\Design;
15+
16+
class LocaleEmulator implements LocaleEmulatorInterface
17+
{
18+
/**
19+
* @var bool
20+
*/
21+
private bool $isEmulating = false;
22+
23+
/**
24+
* @param TranslateInterface $translate
25+
* @param RendererInterface $phraseRenderer
26+
* @param ResolverInterface $localeResolver
27+
* @param ResolverInterface $defaultLocaleResolver
28+
*/
29+
public function __construct(
30+
private readonly TranslateInterface $translate,
31+
private readonly RendererInterface $phraseRenderer,
32+
private readonly ResolverInterface $localeResolver,
33+
private readonly ResolverInterface $defaultLocaleResolver,
34+
private readonly Design $design
35+
) {
36+
}
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
public function emulate(callable $callback, ?string $locale = null): mixed
42+
{
43+
if ($this->isEmulating) {
44+
return $callback();
45+
}
46+
$this->isEmulating = true;
47+
$locale ??= $this->defaultLocaleResolver->getLocale();
48+
$initialLocale = $this->localeResolver->getLocale();
49+
$initialPhraseRenderer = Phrase::getRenderer();
50+
$initialTheme = $this->design->getDesignTheme();
51+
Phrase::setRenderer($this->phraseRenderer);
52+
$this->design->setDefaultDesignTheme();
53+
$this->localeResolver->setLocale($locale);
54+
$this->translate->setLocale($locale);
55+
$this->translate->loadData();
56+
try {
57+
$result = $callback();
58+
} finally {
59+
Phrase::setRenderer($initialPhraseRenderer);
60+
$this->design->setDesignTheme($initialTheme);
61+
$this->localeResolver->setLocale($initialLocale);
62+
$this->translate->setLocale($initialLocale);
63+
$this->translate->loadData();
64+
$this->isEmulating = false;
65+
}
66+
return $result;
67+
}
68+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Config\Console\Command;
9+
10+
/**
11+
* Locale emulator for import and export
12+
*/
13+
interface LocaleEmulatorInterface
14+
{
15+
/**
16+
* Emulates given $locale during execution of $callback
17+
*
18+
* @param callable $callback
19+
* @param string|null $locale
20+
* @return mixed
21+
*/
22+
public function emulate(callable $callback, ?string $locale = null): mixed;
23+
}

app/code/Magento/Config/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<preference for="Magento\Framework\App\Config\ConfigResource\ConfigInterface" type="Magento\Config\Model\ResourceModel\Config" />
1212
<preference for="Magento\Framework\App\Config\CommentParserInterface" type="Magento\Config\Model\Config\Parser\Comment" />
1313
<preference for="Magento\Config\Model\Config\Structure\ElementVisibilityInterface" type="Magento\Config\Model\Config\Structure\ElementVisibilityComposite" />
14+
<preference for="Magento\Config\Console\Command\LocaleEmulatorInterface" type="Magento\Config\Console\Command\LocaleEmulator\Proxy" />
1415
<type name="Magento\Config\Model\Config\Structure\ElementVisibilityComposite">
1516
<arguments>
1617
<argument name="visibility" xsi:type="array">

0 commit comments

Comments
 (0)