how to translate email template in magento?

how to translate email template in magento?

July 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
how to translate email template in magento?

Translating Custom Email Templates in Magento

As a senior developer working extensively with Magento e-commerce platforms, I often encounter clients who need to translate their custom email templates according to the customer’s country of residence. Let’s dive into how you can achieve this for your custom sales module.

Understanding Magento’s Translation System

Magento uses the concept of locales and translation files (.po and .mo) to manage translations. By default, it stores these files in app/locale/en_US/ for English and similar structures for other languages. When a customer visits your site, Magento detects their locale based on browser settings or store configuration.

Translating Your Custom Email Template

For your custom template located in your local module folder (mailtemplate/template1.html), you can follow these steps to translate it:

1. Create Translation Files

Create a new directory structure under app/locale that mirrors the language code. For example, if you want to add French support:

app/locale/fr_FR

Inside this directory, create another folder called template and then an email subfolder.

2. Prepare the Translation Files

In the email folder, place a copy of your original HTML file (template1.html) in each language-specific folder you want to translate.

For instance:

app/locale/fr_FR/template/email

Create a French version of the file and name it something like template1_fr.html.

3. Translate the Content

Open the translated file (template1_fr.html) and replace all the text strings you want to translate with placeholders wrapped in <translate> tags.

For example:

<translate>Dear {{customer.name}},</translate>

Your order has been successfully placed.

<translate>Thank you for your business!</translate>

4. Add Missing Country Code to Telephone

To add the country code according to the customer’s locale, you can use a helper function in your template file. In Magento 2, you can use the Magento\Directory\Model\Country model.

Create a PHP block in your template:

{{block class="Magento\Framework\View\Element\Template" name="country_code_block" template="Magento_Directory::helper/country.phtml"}}

Then, create the file app/code/Vendor/Module/view/frontend/templates/helper/country.phtml with this content:

<?php
/** @var \Magento\Directory\Model\Country $country */
$country = $block->getLayout()->createBlock(\Magento\Directory\Model\Country::class)->load($customerData->getCountryId());
echo '+'.$country->getPhoneCode();
?>

This code will fetch the country object based on the customer’s country ID and output the phone code prefix.

5. Load the Translated Template

To load this translated template, you need to modify your sales order email sender (send function in Magento_SalesSequence emails.xml). Replace the template reference with a dynamic one based on the customer’s locale:

<referenceBlock name="order_items">
    <action method="setTemplate">
        <argument name="template" xsi:type="string">{{var locale}}_template1.html</argument>
    </action>
</referenceBlock>

Replace {{var locale}} with a block that retrieves the customer’s locale.

Conclusion

Translating custom email templates in Magento requires a deep understanding of its translation system and some creative use of PHP and HTML. By following these steps, you can create localized versions of your custom emails, complete with country-specific greetings and telephone number formatting.

Remember to always clear your cache (bin/magento cache:flush) after making changes to the template files and configuration.

For more in-depth guides on Magento development practices, I recommend checking out our comprehensive tutorials at LaravelCompany.com.

Happy coding!

Extensions Themes Configuration Email

Our network