Skip to content

Commit c0d1177

Browse files
#26521 The country / region of mail sent from the admin panel is translated into the admin locale.
1 parent 2a4641b commit c0d1177

File tree

3 files changed

+71
-12
lines changed

3 files changed

+71
-12
lines changed

app/code/Magento/Customer/Block/Address/Renderer/DefaultRenderer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ public function renderArray($addressAttributes, $format = null)
172172
}
173173
$attributeCode = $attributeMetadata->getAttributeCode();
174174
if ($attributeCode == 'country_id' && isset($addressAttributes['country_id'])) {
175-
$data['country'] = $this->_countryFactory->create()->loadByCode(
176-
$addressAttributes['country_id']
177-
)->getName();
175+
$data['country'] = $this->_countryFactory->create()
176+
->loadByCode($addressAttributes['country_id'])
177+
->getName(isset($addressAttributes['locale']) ? $addressAttributes['locale'] : null);
178178
} elseif ($attributeCode == 'region' && isset($addressAttributes['region'])) {
179179
$data['region'] = (string)__($addressAttributes['region']);
180180
} elseif (isset($addressAttributes[$attributeCode])) {
@@ -198,6 +198,7 @@ public function renderArray($addressAttributes, $format = null)
198198
}
199199
}
200200
$format = $format !== null ? $format : $this->getFormatArray($addressAttributes);
201+
201202
return $this->filterManager->template($format, ['variables' => $data]);
202203
}
203204
}

app/code/Magento/Sales/Model/Order/Address/Renderer.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
namespace Magento\Sales\Model\Order\Address;
88

99
use Magento\Customer\Model\Address\Config as AddressConfig;
10+
use Magento\Directory\Helper\Data;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\App\ObjectManager;
1013
use Magento\Framework\Event\ManagerInterface as EventManager;
1114
use Magento\Sales\Model\Order\Address;
15+
use Magento\Store\Model\ScopeInterface;
1216

1317
/**
1418
* Class Renderer used for formatting an order address
@@ -27,18 +31,26 @@ class Renderer
2731
*/
2832
protected $eventManager;
2933

34+
/**
35+
* @var ScopeConfigInterface
36+
*/
37+
private $scopeConfig;
38+
3039
/**
3140
* Constructor
3241
*
3342
* @param AddressConfig $addressConfig
3443
* @param EventManager $eventManager
44+
* @param ScopeConfigInterface|null $scopeConfig
3545
*/
3646
public function __construct(
3747
AddressConfig $addressConfig,
38-
EventManager $eventManager
48+
EventManager $eventManager,
49+
?ScopeConfigInterface $scopeConfig = null
3950
) {
4051
$this->addressConfig = $addressConfig;
4152
$this->eventManager = $eventManager;
53+
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);
4254
}
4355

4456
/**
@@ -50,12 +62,27 @@ public function __construct(
5062
*/
5163
public function format(Address $address, $type)
5264
{
53-
$this->addressConfig->setStore($address->getOrder()->getStoreId());
65+
$storeId = $address->getOrder()->getStoreId();
66+
$this->addressConfig->setStore($storeId);
5467
$formatType = $this->addressConfig->getFormatByCode($type);
5568
if (!$formatType || !$formatType->getRenderer()) {
5669
return null;
5770
}
5871
$this->eventManager->dispatch('customer_address_format', ['type' => $formatType, 'address' => $address]);
59-
return $formatType->getRenderer()->renderArray($address->getData());
72+
$addressData = $address->getData();
73+
$addressData['locale'] = $this->getLocaleByStoreId((int) $storeId);
74+
75+
return $formatType->getRenderer()->renderArray($addressData);
76+
}
77+
78+
/**
79+
* Returns locale by storeId
80+
*
81+
* @param int $storeId
82+
* @return string
83+
*/
84+
private function getLocaleByStoreId(int $storeId): string
85+
{
86+
return $this->scopeConfig->getValue(Data::XML_PATH_DEFAULT_LOCALE, ScopeInterface::SCOPE_STORE, $storeId);
6087
}
6188
}

dev/tests/integration/testsuite/Magento/Sales/Model/Order/Address/RendererTest.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@
55
*/
66
namespace Magento\Sales\Model\Order\Address;
77

8-
use Magento\TestFramework\Helper\Bootstrap;
9-
use Magento\Framework\ObjectManagerInterface;
10-
use Magento\Sales\Model\Order\Address\Renderer as OrderAddressRenderer;
118
use Magento\Config\Model\ResourceModel\Config as ConfigResourceModel;
129
use Magento\Framework\App\Config;
13-
use Magento\Store\Model\Store;
14-
use Magento\Sales\Model\Order\Address as OrderAddress;
10+
use Magento\Framework\Locale\TranslatedLists;
11+
use Magento\Framework\ObjectManagerInterface;
1512
use Magento\Sales\Model\Order;
13+
use Magento\Sales\Model\Order\Address as OrderAddress;
14+
use Magento\Sales\Model\Order\Address\Renderer as OrderAddressRenderer;
15+
use Magento\Store\Model\Store;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use PHPUnit\Framework\TestCase;
1618

17-
class RendererTest extends \PHPUnit\Framework\TestCase
19+
/**
20+
* Test for \Magento\Sales\Model\Order\Address\Renderer.
21+
*/
22+
class RendererTest extends TestCase
1823
{
1924
/**
2025
* @var ObjectManagerInterface
@@ -103,4 +108,30 @@ public function testFormat()
103108
$this->assertEquals($addressTemplates['html'], $this->orderAddressRenderer->format($address, 'html'));
104109
$this->assertEquals($addressTemplates['pdf'], $this->orderAddressRenderer->format($address, 'pdf'));
105110
}
111+
112+
/**
113+
* Order country will be translated to locale on which was placed an order
114+
*
115+
* @magentoDbIsolation disabled
116+
* @magentoDataFixture Magento/Sales/_files/order_fixture_store.php
117+
*
118+
* @return void
119+
*/
120+
public function testRenderOrderAddressCountry(): void
121+
{
122+
/** @var TranslatedLists $localeResolver */
123+
$this->objectManager->create(TranslatedLists::class, ['locale' => 'ko_KR']);
124+
125+
/** @var Order $order */
126+
$order = $this->objectManager->create(Order::class)
127+
->loadByIncrementId('100000004');
128+
129+
/** @var OrderAddress $address */
130+
$address = $order->getBillingAddress();
131+
132+
$this->assertStringContainsString(
133+
'United States',
134+
$this->orderAddressRenderer->format($address, 'html')
135+
);
136+
}
106137
}

0 commit comments

Comments
 (0)