Skip to content

Can't translate email templates sent via a CLI command  #38202

Open
@ioweb-gr

Description

@ioweb-gr

Preconditions and environment

  • 2.4.1

Steps to reproduce

Here's a sample service class that sends an email using a template

<?php
/*
 * Copyright (c) 2022. IOWEB TECHNOLOGIES
 */

namespace Ioweb\Base\Service;

use Exception;
use Magento\Framework\App\Area;
use Magento\Framework\Escaper;
use Magento\Framework\Mail\MailMessageInterfaceFactory;
use Magento\Framework\Mail\MessageInterfaceFactory;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Mail\TransportInterfaceFactory;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Store\Model\StoreManagerInterface;

class MailWithTemplate
{
    protected $escaper;
    protected $inlineTranslation;
    protected $mailTransportFactory;
    protected $messageInterfaceFactory;
    protected $storeManager;
    protected $transportBuilder;

    public function __construct(
        StateInterface            $inlineTranslation,
        Escaper                   $escaper,
        TransportBuilder          $transportBuilder,
        MessageInterfaceFactory   $messageInterfaceFactory,
        TransportInterfaceFactory $mailTransportFactory,
        StoreManagerInterface     $storeManager
    )
    {
        $this->inlineTranslation = $inlineTranslation;
        $this->escaper = $escaper;
        $this->transportBuilder = $transportBuilder;
        $this->messageInterfaceFactory = $messageInterfaceFactory;
        $this->mailTransportFactory = $mailTransportFactory;
        $this->storeManager = $storeManager;
    }

    /**
     * @param array $from [name, email]
     * @param $to
     * @param $templateIdentifier
     * @param $vars
     * @return void
     */
    public function execute($from, $to, $templateIdentifier, $vars, $storeId = null, $area = Area::AREA_FRONTEND)
    {
        try {
            $this->inlineTranslation->suspend();
            $this->transportBuilder->setTemplateIdentifier($templateIdentifier);
            $this->transportBuilder->setTemplateOptions(
                [
                    'area' => !empty($area) ? $area : Area::AREA_FRONTEND,
                    'store' => !empty($storeId) ? $storeId : $this->storeManager->getStore()->getId(),
                ]
            );
            $this->transportBuilder->setTemplateVars($vars);
            $this->transportBuilder
                ->setFromByScope($from)
                ->addTo($to, $to);

            $transport = $this->transportBuilder->getTransport();
            $transport->sendMessage();
            $this->inlineTranslation->resume();
        } catch (Exception $e) {
            return;
        }
    }
}

I'm now trying in a CLI command to send an email template using this service class for storeId 5 which is using Greek locale.

I've added the proper translation files but the message keeps sending in english.

Header / Footer are loaded from english store

Here's my email template

<!-- Email subject -->
<!--@subject {{trans "Enjoy Your Exclusive Black Friday Discount!"}}  @-->

{{template config_path="design/email/header_template"}}

<!-- Header -->
<h1 style="text-align: center">{{trans 'Black Friday Special!'}}</h1>

<!-- Main Content -->
<div>
    <div class="content io-content">
        <p>{{trans 'We are thrilled to offer you an exclusive Black Friday discount for your recent order %increment_id! Use the coupon code below to enjoy special savings on your next purchase.' increment_id=$increment_id}}</p>

        <!-- Coupon Code -->
        <p><strong>{{trans 'Coupon Code:'}}</strong> {{var coupon_code}}</p>

        <!-- Expiration Date -->
        <p><strong>{{trans 'Expires On:'}}</strong> {{var expiration_date|raw}}</p>
    </div>
</div>

<!-- Footer -->
{{template config_path="design/email/footer_template"}}

Expected result

Message is sent translated

Actual result

Message is using default English locale

Additional information

No response

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: FrameworkComponent: TranslationIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P3May be fixed according to the position in the backlog.Progress: ready for devReported on 2.4.1Indicates original Magento version for the Issue report.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchTriage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions