2 (40.73%) 55 votes

In this topic we will show you How to Translate a String by Code in Magento 2 through 5 steps:

  1. In .ptml files
  2. In email template
  3. Strings added in UI components’ templates
  4. Strings added in UI components configuration files
  5. Strings added in .js files

Hello everyone. In Magento 2 (even for the Magento 2.3), you can setup multi languages. But to translate the string in Magento 2, in the source code, you must use the function to translate .

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 (Magento 2.3). 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.

Thank you for reading this post and see you in other posts from Magestore!

Your Magento website needs a powerful Magento ERP for stock control.

DON'T MISS THE CHANCE!

magento erp

How to Run Enable/Disable Maintenance Mode in Magento 2 - Magento 2.3
How to Create Widget in Magento 2 - Magento 2.3