I was with same problem. I'm building some modules to work with GraphQl and I have this problem. After search a lot and not found anything, I debugged Magento core to found the problem.
Magento 2 works with multiple areas, like you can see in vendor/magento/framework/App/Area.php
const AREA_GLOBAL = 'global';
const AREA_FRONTEND = 'frontend';
const AREA_ADMINHTML = 'adminhtml';
const AREA_DOC = 'doc';
const AREA_CRONTAB = 'crontab';
const AREA_WEBAPI_REST = 'webapi_rest';
const AREA_WEBAPI_SOAP = 'webapi_soap';
const AREA_GRAPHQL = 'graphql';
I'm debugged frontend area and found this in the file vendor/magento/framework/View/DesignLoader.php
public function load()
{
$area = $this->_areaList->getArea($this->appState->getAreaCode());
$area->load(\Magento\Framework\App\Area::PART_DESIGN);
$area->load(\Magento\Framework\App\Area::PART_TRANSLATE);
$area->detectDesign($this->_request);
}
To resolve my problem I'd create a plugin to load translate at the GraphQl area.
Steps:
1. Create a di.xml file under app/code/Vendor/Module/etc/graphql:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\GraphQl\Controller\GraphQl">
<plugin name="graphql_load_translate" type="Vendor\Module\Plugin\Controller\GraphQlPlugin" sortOrder="1" disabled="false"/>
</type>
</config>
2. Create your plugin class under app/code/Vendor/Module/Plugin/Controller/GraphQlPlugin.php
<?php
namespace Vendor\Module\Plugin\Controller;
class GraphQlPlugin
{
/** @var \Magento\Framework\App\AreaList $areaList */
private $areaList;
/** @var \Magento\Framework\App\State $appState */
private $appState;
public function __construct(
\Magento\Framework\App\AreaList $areaList,
\Magento\Framework\App\State $appState
)
{
$this->areaList = $areaList;
$this->appState = $appState;
}
public function beforeDispatch(\Magento\GraphQl\Controller\GraphQl $subject)
{
$area = $this->areaList->getArea($this->appState->getAreaCode());
$area->load(\Magento\Framework\App\Area::PART_TRANSLATE);
}
}
3. Compile classes
php bin/magento setup:di:compile
This worked for me.