How do I add text content in a phtml file for translation

How do I add text content in a phtml file for translation

July 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How do I add text content in a phtml file for translation

Adding Translatable Text Content to a .phtml File in Magento

When it comes to developing multilingual eCommerce sites using Magento, you often need to add text content to your PHP HTML (.phtml) files that can be easily translated into different languages. In this post, we’ll guide you through the process of adding translatable content to a .phtml file located within your theme’s folder.

Understanding Magento’s Translation Mechanism

Magento uses the concept of translateable strings to handle text that needs to be presented in multiple languages. These translateable strings are stored in the translate.csv files located under app/design/frontend/<vendor>/<theme>/i18n/en_US/.

Each line in this file contains a unique key for the string, followed by its English value, and optionally, values for other supported languages. When Magento encounters a call to __(), it looks up the provided key in the appropriate translation CSV file and returns the corresponding translated text.

Adding Translatable Content to Your .phtml File

Let’s say you have the following .phtml file that needs translatable content:

<!-- app/design/frontend/<vendor>/<theme>/template/catalog/category/left_nav.phtml -->
<div class="block block-layered-nav">
    <div class="block-content">
        <h2 class="block-title"><?php echo $this->__('Faceted Navigation'); ?></h2>
        <?php /* ... rest of the file ... */ ?>
    </div>
</div>

You can add translatable content to this file by using Magento’s __() function. Here’s how you do it:

<div class="block block-layered-nav">
    <div class="block-content">
        <h2 class="block-title"><?php echo $this->__('Faceted Navigation'); ?></h2>
        <?php /* ... rest of the file ... */ ?>
        <!-- Add translatable content here -->
        <p><?php echo $this->__('Find your perfect products by applying filters on this side.'); ?></p>
    </div>
</div>

In the example above, we’ve added a new paragraph with translatable text. The __() function takes the unique key for the string as its argument.

Creating the Translation CSV File

After adding the translatable content to your .phtml file, you need to create or update the appropriate translation CSV file in your theme’s i18n folder:

<!-- app/design/frontend/<vendor>/<theme>/i18n/en_US/translate.csv -->
"facetedNavigation.paragraph","Find your perfect products by applying filters on this side."

In this case, we’ve created a new translation key called facetedNavigation.paragraph and assigned the corresponding English text to it.

Updating Your Translation CSV Files

If you have multiple languages installed and want to translate this string into other languages as well, you can add additional columns in your CSV file:

<!-- app/design/frontend/<vendor>/<theme>/i18n/de_DE/translate.csv -->
"facetedNavigation.paragraph","Findet Ihre perfekten Produkte durch die Anwendung von Filtern auf dieser Seite."

Remember to update the language code (in this case, de_DE for German) in the file path accordingly.

Conclusion

Adding translatable content to your .phtml files is a crucial step when developing multilingual Magento sites. By using Magento’s built-in __() function and creating translation CSV files, you can ensure that your site’s text content can be easily translated into multiple languages without hardcoding strings directly in your PHP code.

For more advanced multilingual development techniques and best practices, we recommend exploring our comprehensive guide on Magento 2 Multilanguage Development at Laravel Company.

Catalog Database API Themes

Our network