How to Find All Translation Labels from Magento 2 Extension PHP and JS Files

Here are the following steps which help you to find all translation labels from Magento 2 extension PHP and JS files:

Step 1: PHP files translate label configuration

Step 2: JS files translate label configuration

Step 3: Enable generator labels

 PHP files translate label configuration

We utilize default translate label structure of Magento 2.

Example:  echo __(‘Hello Everyone’);

JS files translate label configuration

We can utilize the following structure:

define(

 [    

  'mage/translate'

  ],

 function (__) {

getLabel: function(){

   alert(__('Hello Everyone'));

}

 }

);

 Enable generator labels

File directory:

xampp/htdocs/translation/translatelabel.php
$csvFile = 'translatephp.csv';

$searchExtensions = array('php', 'phtml', 'js');

  $strTranslateFunc = '__';

 $arrItems = array();

$sourcePath = 'your_folder_source';

  $di = new RecursiveDirectoryIterator($sourcePath);

 foreach (new RecursiveIteratorIterator($di) as $filename => $file)

   if (in_array( strtolower( pathinfo($file, PATHINFO_EXTENSION) ), $searchExtensions))

  {

$strContent = file_get_contents($filename);

$arrMatches = array();

 preg_match_all("/{$strTranslateFunc}\((?:(?:\"(?:\\\\\"|[^\"])+\")|(?:'(?:\\\'|[^'])+'))/is", $strContent, $arrMatches);

foreach($arrMatches as $matched) {

 foreach($matched as $match) {

 $_item = trim( str_replace("{$strTranslateFunc}(", '', $match) , "'\"");

    if ( ! in_array($_item, $arrItems))

 {

 $arrItems[] = $_item;

}

 }

 }

}

  }

 $fp = fopen($csvFile, 'w');

   foreach ($arrItems as $_item) {

 fputcsv($fp, array($_item, $_item));

 }

 fclose($fp);

I hope the following steps explain you to get all translation labels from Magento 2 extension PHP and JS files.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *