I am trying to programmatically create the shipment and I succeeded in this operation. To do this I create a new CLI Command which I obviously run by command line.
I have only 1 storeview and the default locale is Italian.
The problem is about sending the email to the customer because it is always sent in English.
I try different ways to send the shipment email and the last is using the class
Magento\Sales\Model\Order\Email\Sender\ShipmentSender
in this way
$this->shipmentSender->send($shipment);
after had set
$this->state->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);
I also tried the class
Magento\Shipping\Model\ShipmentNotifier
in this way
$this->shipmentNotifier->notify($shipment);
I also try to embed the code inside a statement of EnvironmentEmulation using
Magento\Store\Model\App\Emulation
using the following code
$this->emulation->startEnvironmentEmulation($order->getStoreId());
// send shipping confirmation e-mail to customer
$this->shipmentNotifier->notify($shipment);
$this->emulation->stopEnvironmentEmulation();
I don't know if this is a bug, as many others I found or if I'm wrong.
Note: if I create and send the shipment from the backend the email is in the correct language.
EDIT:
I try executing the code by external php script using ObjectManager with the following code:
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$orderId = "000000009";
$order = $obj->get('Magento\Sales\Model\OrderRepository')->get($orderId);
$notifier = $obj->get('Magento\Sales\Model\Order\Shipment\NotifierInterface');
foreach($order->getShipmentsCollection() as $ship) {
$notifier->notify($order,$ship);
}
?>
...but I get the same result: the shipment email is sent in English and not Italian.
Please, help me!
Thank you.