Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
975 views
in Technique[技术] by (71.8m points)

magento2 - Magento 2 observer not using correct locale for translation

I have added an observer using the code below, it triggers correctly but the translated text is English. It is supposed to be Danish. When I call the same code from other places it uses the correct locale.

What am I missing? Do I need to set the locale explicitly when using observers?

events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="email_order_set_template_vars_before">
        <observer name="add_DeliveryTime_variable_to_Order"
                  instance="ZitecCheckoutObserverDeliveryTime" />
    </event>

</config>

DeliveryTime.php

class DeliveryTime implements ObserverInterface
{

    /**
     * @var C5OrderService
     */
    private $c5OrderService;

    public function __construct(C5OrderService $c5OrderService) {
        $this->c5OrderService = $c5OrderService;
    }

    /**
     *
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        $transport = $observer->getEvent()->getTransport();
        /** @var Order $order */
        $order = $transport->getOrder();

        $transport['DeliveryTime'] = $this->c5OrderService->getMaximumDeliveryTimeText($order);
    }
}

C5OrderService.php

class C5OrderService {
        public function getMaximumDeliveryTimeText($order){
            return __('10 working days');
        }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...