DEV Community

Cover image for Translation Labels from PHP and JS files of Magento 2 Extension
HariBabu G
HariBabu G

Posted on • Originally published at webnexs.com

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; 

              } 

          }
Enter fullscreen mode Exit fullscreen mode

}

}
Enter fullscreen mode Exit fullscreen mode

}

$fp = fopen($csvFile, 'w');
Enter fullscreen mode Exit fullscreen mode

foreach ($arrItems as $_item) {

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

}

fclose($fp);

That's all about it

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.

magento 2 multi-vendor marketplace extension

Discussion (0)