While working with the Magento 2 GraphQl it seems that the string translation does not work

Viewed 2101

Within a GraphQl Resolver we want to give back a translated string, using the __() method. Unfortunately it seems that this does not work. We also debugged a bit but could not figure out why this is not working. My guess ist, that the resolvers are not able to translate because they don't know into which language. But yet I could not figure out how to set the language.

Has anyone else experienced this kind of problem and how does this work in the PWA Studio?

3 Answers

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.

It seems like Magento didn't consider translations for graphql. rodslva solutions works just fine but of course this cannot be state of the art to not have such a simple feature. Someone openend an issue about this topic: https://github.com/magento/magento2/issues/31351. Hopefully it'll be resolved soon.

For today, it looks like the issue shared by @Timen is close to be solved in core. In the meantime, I packed the @rodslva into a module in case anybody wants to use it: https://github.com/ebolution/magento2-graphql-translations.

In any case, it looks it doesn't work on multi-language sites, I haven't tested this yet, but be aware.

Related