Translate theme strings

This topic describes how to add theme strings so that the i18n tool can collect and add the strings to the dictionary.

Your custom theme may contain new strings that are not present in out-of-the-box Magento themes. To ensure your theme displays correctly with any language applied on a store view, verify the unique strings of your theme are added to the translation i18n tool when generating the dictionary. Then when a new language package is created and used to translate a store view, all theme strings are also translated.

Strings added in .phtml templates

To ensure that your new string is added to the dictionary and translated, use the __('<your_string>') method when outputting a string in a .phtml template.

For example:

### <?php echo __('Create Backup') ?>

If your string contains a variable, to add a placeholder for this variable in the dictionary, use syntax similar to the following:

### <?php echo __('Hello %1', $yourVariable) ?>

In this example, the ‘Hello %1’ string is added to the dictionary when the i18n tool is run.

Strings added in email templates

If your theme contains [custom email templates], their strings can be added to the dictionary as well. To add the email template strings to the dictionary, use the `` [directive].

Custom email templates [added using the Admin panel] are not stored in the file system, and their strings are not added to the dictionary.

Strings added in UI component templates

To ensure that the text you add in .html templates of UI components is added to the dictionary, mark the text using the i18n custom binding. The following code samples illustrate how to use custom bindings:

  • When a string is added in the scope of an HTML element:

      <span data-bind="i18n: 'Sign In'"></span>
    
  • When a string is added with no binding to an HTML element:

      <!-- ko i18n: 'You have no items in your shopping cart.' --><!-- /ko -->
    
  • When a string is added as an attribute of an HTML element:

      <input type="text" data-bind="attr: {placeholder: $t('First Name')}" />
    

Strings added in UI components configuration files

To ensure that the text you add in UI components configuration .xml files is added to the dictionary, use the translate attribute. Set the attribute to true for the corresponding element: translate=true

In this example, the Delete string is added to the dictionary when the i18n tool is run:

<item name="label" xsi:type="string" translate="true">Delete</item>

Translated strings that originate from .xml files will not render unless they are called with a __(<variable>) method. In this example, you would use a call similar to the following to display the translated Delete string.

__($this->config->getData('label'))

Strings added in .js files

To ensure that the text you add in a .js file is collected by the i18n tool and added to the dictionary:

  1. Link the mage/translate library:

     define (['jquery', 'mage/translate'], function ($) {...});
    
  2. Use the $.mage.__('') function when adding a string:

     $.mage.__('<string>');
    

    If your string contains a variable, to add a placeholder for this variable to the string stored in the dictionary, use the syntax similar to the following:

     $.mage.__('Hello %1').replace('%1', yourVariable);
    

In this example, the 'Hello %1' string is added to the dictionary when the i18n tool is run.

custom email templates]: /guides/v2.3/frontend-dev-guide/templates/template-email.html#customize-email-theme [directive]: /guides/v2.3/frontend-dev-guide/templates/template-email.html#localization [added using the Admin panel]: /guides/v2.3/frontend-dev-guide/templates/template-email.html#customize-email-admin