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
I’m debugged frontend area and found this in the file vendor/magento/framework/View/DesignLoader.php
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:
2. Create your plugin class under app/code/Vendor/Module/Plugin/Controller/GraphQlPlugin.php
3. Compile classes
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.