Skip to content

Magento 2 error message not rendering

I’ve recently come across an issue while trying to render an error message on a page with the use of the MagentoFrameworkMessageMessageInterface

For some reason this code does not render the message:

$resultPage = $this->resultPageFactory->create();

$resultPage->getConfig()->getTitle()->prepend(__('Add Language'));

try {
    $this->translationClient->getTranslationClient();
} catch (TranslationSourceNotSetException|InvalidTranslationSourceException $e) {
    $this->messageManager->addWarningMessage("....");
}

But this code does:

$resultPage = $this->resultPageFactory->create();

try {
    $this->translationClient->getTranslationClient();
} catch (TranslationSourceNotSetException|InvalidTranslationSourceException $e) {
    $this->messageManager->addWarningMessage("....");
}

$resultPage->getConfig()->getTitle()->prepend(__('Add Language'));

Does something in this line: $resultPage->getConfig()->getTitle()->prepend(__('Add Language')); trigger page intialization? Or is something else going on?