Skip to content

Commit

Permalink
Fix for translation not working in graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
raivisdejus committed Feb 17, 2022
1 parent bc80aa8 commit 9b3b3f6
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/Plugin/InitGraphQlTranslations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* ScandiPWA_CmsGraphQl
*
* @category Scandiweb
* @package ScandiPWA_CmsGraphQl
* @copyright Copyright (c) 2022 Scandiweb, Ltd (https://scandiweb.com)
*/
declare(strict_types=1);

namespace ScandiPWA\CmsGraphQl\Plugin;

use Exception;
use Magento\Framework\App\Area;
use Magento\Framework\App\FrontControllerInterface;
use Magento\Framework\App\AreaList;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\State;
use Magento\Framework\Exception\LocalizedException;
use ScandiPWA\PersistedQuery\Plugin\PersistedQuery;

/**
* Class InitGraphQlTranslations
*
* A fix for https://github.com/magento/magento2/issues/31351
*
* @see \Magento\Framework\App\Action\Plugin\Design
* @see \Magento\Webapi\Controller\Soap
* @package ScandiPWA\CmsGraphQl\Plugin
*/
class InitGraphQlTranslations
{
/**
* Application
*
* @var AreaList
*/
protected $areaList;

/**
* State
*
* @var State
*/
protected $appState;

/**
* @param AreaList $areaList
* @param State $appState
*/
public function __construct(
AreaList $areaList,
State $appState
) {
$this->areaList = $areaList;
$this->appState = $appState;
}

/**
* @param FrontControllerInterface $subject
* @param RequestInterface $request
*
* @return void
* @throws Exception
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeDispatch(
FrontControllerInterface $subject,
RequestInterface $request
) {
$area = $this->areaList->getArea($this->appState->getAreaCode());
if ($area) {
$area->load(Area::PART_TRANSLATE);
}
}

/**
* @param PersistedQuery $subject
* @param RequestInterface $request
* @return void
* @throws LocalizedException
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeProcessRequest(
PersistedQuery $subject,
RequestInterface $request
) {
$area = $this->areaList->getArea($this->appState->getAreaCode());
if ($area) {
$area->load(Area::PART_TRANSLATE);
}
}
}
9 changes: 9 additions & 0 deletions src/etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@
</argument>
</arguments>
</virtualType>

<type name="Magento\GraphQl\Controller\GraphQl">
<plugin name="graphql_init_translations"
type="ScandiPWA\CmsGraphQl\Plugin\InitGraphQlTranslations"/>
</type>
<type name="ScandiPWA\PersistedQuery\Plugin\PersistedQuery">
<plugin name="graphql_init_translations"
type="ScandiPWA\CmsGraphQl\Plugin\InitGraphQlTranslations"/>
</type>
</config>

0 comments on commit 9b3b3f6

Please sign in to comment.