Skip to content

Commit 648dd92

Browse files
committed
ACP2E-4354: Customer create form error messages are not translated
1 parent 9bcd880 commit 648dd92

File tree

1 file changed

+62
-4
lines changed

1 file changed

+62
-4
lines changed

app/code/Magento/Customer/Controller/Account/CreatePost.php

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Magento\Framework\App\RequestInterface;
2222
use Magento\Framework\Controller\Result\Redirect;
2323
use Magento\Framework\Exception\LocalizedException;
24+
use Magento\Framework\Message\AbstractMessage;
2425
use Magento\Framework\Message\MessageInterface;
2526
use Magento\Framework\Phrase;
2627
use Magento\Store\Model\StoreManagerInterface;
@@ -40,6 +41,7 @@
4041
use Magento\Framework\Exception\InputException;
4142
use Magento\Framework\Data\Form\FormKey\Validator;
4243
use Magento\Customer\Controller\AbstractAccount;
44+
use Magento\Framework\Validator\Exception as ValidatorException;
4345

4446
/**
4547
* Post create customer action
@@ -423,10 +425,7 @@ public function execute()
423425
]
424426
);
425427
} catch (InputException $e) {
426-
$this->messageManager->addErrorMessage($e->getMessage());
427-
foreach ($e->getErrors() as $error) {
428-
$this->messageManager->addErrorMessage($error->getMessage());
429-
}
428+
$this->processInputException($e);
430429
} catch (LocalizedException $e) {
431430
$this->messageManager->addErrorMessage($e->getMessage());
432431
} catch (\Exception $e) {
@@ -517,6 +516,65 @@ private function getMessageManagerSuccessMessage(): MessageInterface
517516
return $message;
518517
}
519518

519+
/**
520+
* Process InputException and add error messages to message manager
521+
*
522+
* @param InputException $exception
523+
* @return void
524+
*/
525+
private function processInputException(InputException $exception): void
526+
{
527+
if ($exception instanceof ValidatorException) {
528+
$this->processValidatorException($exception);
529+
} else {
530+
$this->processStandardInputException($exception);
531+
}
532+
}
533+
534+
/**
535+
* Process ValidatorException by extracting, translating and merging again individual messages
536+
*
537+
* @param ValidatorException $exception
538+
* @return void
539+
*/
540+
private function processValidatorException(ValidatorException $exception): void
541+
{
542+
$validatorMessages = $exception->getMessages();
543+
if (empty($validatorMessages)) {
544+
$this->messageManager->addErrorMessage($exception->getMessage());
545+
return;
546+
}
547+
548+
$translatedMessages = [];
549+
foreach ($validatorMessages as $message) {
550+
$messageText = $message instanceof AbstractMessage
551+
? $message->getText()
552+
: (string)$message;
553+
$translatedMessages[] = (string)__($messageText);
554+
}
555+
556+
$combinedTranslatedMessage = implode(' ', $translatedMessages);
557+
$this->messageManager->addErrorMessage($combinedTranslatedMessage);
558+
}
559+
560+
/**
561+
* Process standard InputException by extracting individual errors
562+
*
563+
* @param InputException $exception
564+
* @return void
565+
*/
566+
private function processStandardInputException(InputException $exception): void
567+
{
568+
$errors = $exception->getErrors();
569+
if (!empty($errors)) {
570+
foreach ($errors as $error) {
571+
$this->messageManager->addErrorMessage($error->getMessage());
572+
}
573+
} else {
574+
$this->messageManager->addErrorMessage($exception->getMessage());
575+
}
576+
}
577+
520578
/**
521579
* Convert punycode email back to Unicode
522580
*

0 commit comments

Comments
 (0)