skip to Main Content

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


  1. 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(MagentoFrameworkAppArea::PART_DESIGN);
        $area->load(MagentoFrameworkAppArea::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="MagentoGraphQlControllerGraphQl">
            <plugin name="graphql_load_translate" type="VendorModulePluginControllerGraphQlPlugin" sortOrder="1" disabled="false"/>
        </type>
    </config>
    

    2. Create your plugin class under app/code/Vendor/Module/Plugin/Controller/GraphQlPlugin.php

        <?php
    
        namespace VendorModulePluginController;
    
        class GraphQlPlugin
        {
            /** @var MagentoFrameworkAppAreaList $areaList */
            private $areaList;
    
            /** @var MagentoFrameworkAppState $appState */
            private $appState;
    
            public function __construct(
                MagentoFrameworkAppAreaList $areaList,
                MagentoFrameworkAppState $appState
            )
            {
                $this->areaList = $areaList;
                $this->appState = $appState;
            }
    
            public function beforeDispatch(MagentoGraphQlControllerGraphQl $subject)
            {
                $area = $this->areaList->getArea($this->appState->getAreaCode());
                $area->load(MagentoFrameworkAppArea::PART_TRANSLATE);
            }
        }
    

    3. Compile classes

    php bin/magento setup:di:compile
    

    This worked for me.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search