How to Get all Translation Labels from PHP and JS files of Magento 2 Extension

To write a generator label function with XML file using:

File directory: xampp/htdocs/translation/translatelabel.php

The following steps are the shortest process for you to get all translation labels from PHP and JS files of Magento 2 Extension.

  $searchExtensions = array('xml', 'xml');

  $strTranslateFunc = 'title=';

  $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}\"(.+?)\"|\/{$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);

By using these steps you can easily handle the translation labels from PHP and JS files of Magento 2 Extension.Each store has a translate labels from PHP and JS files of Magento 2 with many features.


Posted

in

by

Comments

Leave a Reply

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