Skip to content

Commit 9f53f68

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

File tree

4 files changed

+146
-2
lines changed

4 files changed

+146
-2
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Magento\GraphQl\Plugin;
4+
5+
use Magento\Framework\App\Request\Http as HttpRequest;
6+
use Magento\Framework\App\RequestInterface;
7+
use Magento\Framework\Exception\NoSuchEntityException;
8+
use Magento\Store\Api\Data\StoreInterface;
9+
use Magento\Store\Api\StoreRepositoryInterface;
10+
use Magento\Store\Model\StoreIsInactiveException;
11+
12+
/**
13+
* Load translations for GraphQL requests
14+
*/
15+
class StoreResolver
16+
{
17+
/**
18+
* @var RequestInterface|HttpRequest
19+
*/
20+
private $request;
21+
22+
/**
23+
* @var StoreRepositoryInterface
24+
*/
25+
private $storeRepository;
26+
27+
/**
28+
* @param RequestInterface $request
29+
* @param StoreRepositoryInterface $storeRepository
30+
*/
31+
public function __construct(
32+
RequestInterface $request,
33+
StoreRepositoryInterface $storeRepository
34+
) {
35+
$this->request = $request;
36+
$this->storeRepository = $storeRepository;
37+
}
38+
39+
/**
40+
* Use the 'Store' header to determine the current store
41+
*
42+
* @param \Magento\Store\Model\StoreResolver $subject
43+
* @param callable $proceed
44+
* @return mixed
45+
*/
46+
public function aroundGetCurrentStoreId(\Magento\Store\Model\StoreResolver $subject, callable $proceed)
47+
{
48+
$storeCode = $this->request->getHeader('Store');
49+
50+
if ($storeCode) {
51+
try {
52+
$store = $this->getRequestedStoreByCode($storeCode);
53+
54+
if ($store) {
55+
return $store;
56+
}
57+
} catch (NoSuchEntityException $e) {
58+
return $proceed();
59+
}
60+
}
61+
62+
return $proceed();
63+
}
64+
65+
/**
66+
* Retrieve active store by code
67+
*
68+
* @param string $storeCode
69+
* @return StoreInterface
70+
* @throws NoSuchEntityException
71+
*/
72+
private function getRequestedStoreByCode($storeCode) : StoreInterface
73+
{
74+
try {
75+
$store = $this->storeRepository->getActiveStoreByCode($storeCode);
76+
} catch (StoreIsInactiveException $e) {
77+
throw new NoSuchEntityException(__('Requested store is inactive'));
78+
}
79+
80+
return $store;
81+
}
82+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Magento\GraphQl\Plugin;
4+
5+
use Magento\Framework\App\AreaInterface;
6+
use Magento\Framework\App\AreaList;
7+
use Magento\Framework\App\ObjectManager;
8+
use Magento\Framework\App\State;
9+
use Magento\Store\Model\StoreManagerInterface;
10+
11+
/**
12+
* Load translations for GraphQL requests
13+
*/
14+
class TranslationLoader
15+
{
16+
/**
17+
* @var AreaList
18+
*/
19+
private $areaList;
20+
21+
/**
22+
* @var State
23+
*/
24+
private $appState;
25+
26+
/**
27+
* @param AreaList $areaList
28+
* @param State $appState
29+
*/
30+
public function __construct(
31+
AreaList $areaList,
32+
State $appState
33+
) {
34+
$this->areaList = $areaList;
35+
$this->appState = $appState;
36+
}
37+
38+
/**
39+
* Before rendering any string ensure the translation aspect of area is loaded
40+
*/
41+
public function beforeRender(\Magento\Framework\Phrase $subject)
42+
{
43+
$area = $this->areaList->getArea($this->appState->getAreaCode());
44+
$area->load(AreaInterface::PART_TRANSLATE);
45+
}
46+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,10 @@
4141
</argument>
4242
</arguments>
4343
</type>
44+
<type name="Magento\Framework\Phrase">
45+
<plugin name="graphQlLocalizationPhrasePlugin" type="Magento\GraphQl\Plugin\TranslationLoader" />
46+
</type>
47+
<type name="Magento\Store\Model\StoreResolver">
48+
<plugin name="graphQlLocalizationStoreResolver" type="Magento\GraphQl\Plugin\StoreResolver" />
49+
</type>
4450
</config>

lib/internal/Magento/Framework/Phrase/__.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
declare(strict_types=1);
78

9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\Phrase;
11+
812
/**
913
* Create value-object \Magento\Framework\Phrase
1014
*
1115
* @SuppressWarnings(PHPMD.ShortMethodName)
1216
* phpcs:disable Squiz.Functions.GlobalFunction
1317
* @param array $argc
14-
* @return \Magento\Framework\Phrase
18+
* @return Phrase
1519
*/
1620
function __(...$argc)
1721
{
@@ -20,5 +24,11 @@ function __(...$argc)
2024
$argc = $argc[0];
2125
}
2226

23-
return new \Magento\Framework\Phrase($text, $argc);
27+
return ObjectManager::getInstance()->create(
28+
Phrase::class,
29+
[
30+
'text' => $text,
31+
'arguments' => $argc
32+
]
33+
);
2434
}

0 commit comments

Comments
 (0)