1. In .ptml files

  • Use the function  __()

__(‘<your_string>’)

  • For example:
  • If your string contains a variable,you can use::
  • In this example, the ‘Hello %s’ string is added to the dictionary when the i18n tool is run.

2. In email template

If your theme contains custom email template, use the {{trans}} directive.

3. Strings added in UI components’ templates

  • A string is added in the scope of an HTML element:
  • A string is added with no binding to an HTML element:

4.Strings added in UI components configuration files

  • Use the translate attribute in .xml files:

5.Strings added in .js files

  • Define
  • Use the $.mage.__(”) function when adding a string:
  • If your string contains a variable:

The  5 steps I mention above is the shortest process for you to Translate a String by Code in Magento 2. With this guide, you can manage the String by Code in Magento 2 easily. Every store has a String by Code in Magento 2 with many attributes.