Skip to content

Commit 4401d65

Browse files
committed
PWA-806: Localize emails sent through GraphQL application
1 parent 9f53f68 commit 4401d65

File tree

5 files changed

+62
-92
lines changed

5 files changed

+62
-92
lines changed

app/code/Magento/GraphQl/Plugin/StoreResolver.php

Lines changed: 0 additions & 82 deletions
This file was deleted.

app/code/Magento/GraphQl/Plugin/TranslationLoader.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
use Magento\Framework\App\AreaInterface;
66
use Magento\Framework\App\AreaList;
7-
use Magento\Framework\App\ObjectManager;
87
use Magento\Framework\App\State;
9-
use Magento\Store\Model\StoreManagerInterface;
108

119
/**
12-
* Load translations for GraphQL requests
10+
* Load translations on the first instance of a translated string
1311
*/
1412
class TranslationLoader
1513
{
@@ -23,6 +21,11 @@ class TranslationLoader
2321
*/
2422
private $appState;
2523

24+
/**
25+
* @var bool
26+
*/
27+
private $translationsLoaded = false;
28+
2629
/**
2730
* @param AreaList $areaList
2831
* @param State $appState
@@ -36,11 +39,16 @@ public function __construct(
3639
}
3740

3841
/**
39-
* Before rendering any string ensure the translation aspect of area is loaded
42+
* Before render of any localized string ensure the translation data is loaded
43+
*
44+
* @throws \Magento\Framework\Exception\LocalizedException
4045
*/
41-
public function beforeRender(\Magento\Framework\Phrase $subject)
46+
public function beforeRender()
4247
{
43-
$area = $this->areaList->getArea($this->appState->getAreaCode());
44-
$area->load(AreaInterface::PART_TRANSLATE);
48+
if ($this->translationsLoaded === false) {
49+
$area = $this->areaList->getArea($this->appState->getAreaCode());
50+
$area->load(AreaInterface::PART_TRANSLATE);
51+
$this->translationsLoaded = true;
52+
}
4553
}
4654
}

app/code/Magento/GraphQl/etc/graphql/di.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,4 @@
4444
<type name="Magento\Framework\Phrase">
4545
<plugin name="graphQlLocalizationPhrasePlugin" type="Magento\GraphQl\Plugin\TranslationLoader" />
4646
</type>
47-
<type name="Magento\Store\Model\StoreResolver">
48-
<plugin name="graphQlLocalizationStoreResolver" type="Magento\GraphQl\Plugin\StoreResolver" />
49-
</type>
5047
</config>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Magento\StoreGraphQl\Plugin;
4+
5+
use Magento\Framework\App\Request\Http as HttpRequest;
6+
use Magento\Framework\App\RequestInterface;
7+
use Magento\Store\Model\Resolver\Store;
8+
9+
/**
10+
* Ensure the store resolver gets the correct scope based on the GraphQl header
11+
*/
12+
class StoreResolver
13+
{
14+
/**
15+
* @var RequestInterface|HttpRequest
16+
*/
17+
private $request;
18+
19+
/**
20+
* @param RequestInterface $request
21+
*/
22+
public function __construct(
23+
RequestInterface $request
24+
) {
25+
$this->request = $request;
26+
}
27+
28+
/**
29+
* If no scope is provided and there is a Store header, ensure the correct store code is used
30+
*
31+
* @param Store $subject
32+
* @param null $scopeId
33+
* @return array
34+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
35+
*/
36+
public function beforeGetScope(Store $subject, $scopeId = null)
37+
{
38+
$storeCode = $this->request->getHeader('Store');
39+
40+
if ($scopeId === null && $storeCode) {
41+
return [$storeCode];
42+
}
43+
}
44+
}

app/code/Magento/StoreGraphQl/etc/graphql/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@
2323
</argument>
2424
</arguments>
2525
</type>
26+
<type name="Magento\Store\Model\Resolver\Store">
27+
<plugin name="graphQlLocalizationStoreResolver" type="Magento\StoreGraphQl\Plugin\StoreResolver" />
28+
</type>
2629
</config>

0 commit comments

Comments
 (0)