Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get translatable strings for Magento modules
<?php
/**
* Get translatable strings for Magento modules
*
* Tested on Mac & Linux
*
* Usage:
*
* 1. Change the $path variable to where your module is located
* 2. php TranslateMagentoModule.php
*
* To save the result without copy-paste run php TranslateMagentoModule.php > /path/to/locale/myLanguageFile.csv
*/
// Surpress errors
error_reporting(0);
ini_set('display_errors', 'off');
// Where the module is located
$path = '/path/to/module/';
// Setup the search patterns
$patterns = array(
'/__\(\'(.*)\'\)/i',
'/__\("(.*)"\)/i'
);
// Get a list of all PHP files in that directory
@exec('find ' . $path . ' | grep php', $fileList);
// Setup data storage arrayu
$csv = array();
// Loop through all files
foreach ( $fileList as $file )
{
// Open the file
$data = file_get_contents($file);
// Make sure we've got some data
if ( $data )
{
// Process each pattern
foreach ( $patterns as $pattern )
{
// Match all __('') strings
preg_match_all($pattern, $data, $matches);
// Check if we've got some results
if ( count($matches[1]) > 0 ) {
// Loop through the result
foreach ( $matches[1] as $match ) {
// Add line to csv array
$csv[] = '"' . $match . '","' . $match . '"';
}
}
}
}
}
// Return the output
print implode("\n", $csv);
@ksz2013

This comment has been minimized.

Copy link

@ksz2013 ksz2013 commented Feb 17, 2015

$patterns = array(
'/('(.*?)')/i',
'/
("(.*?)")/i'
);

Use this for pattern, otherwise if more translations are in one row, won't find it correctly.

@daancart2quote

This comment has been minimized.

Copy link

@daancart2quote daancart2quote commented Jan 20, 2016

I use:

$patterns = array(
    "/__\\(\\s*\\'(.*?)(?=(?<!\\\\)\\')/i",
    "/__\\(\\s*\\\"(.*?)(?=(?<!\\\\)\\\")/i",
);

It allows for multiple translations in a row and uses a lookahead, so that it only includes text between the first and the next delimiter.
(It also allows escaped delimiters to exist in the translated string)

@gulshankumar

This comment has been minimized.

Copy link

@gulshankumar gulshankumar commented Apr 29, 2017

Very useful script. I have done some modification according to my use. I just added function to list files using php and added code to write output to csv file using fputcsv that takes care of quotes. Also used suggested patten in above comment.

<?php
// Surpress errors
error_reporting(0);
ini_set('display_errors', 'off');
define('MAGENTO_ROOT', getcwd());
define('DS', DIRECTORY_SEPARATOR);
// Where the module is located
$codePool = 'community';
$moduleName = '';
$outputCSV = 'Namespace_ModuleName.csv';
$pathToModule = MAGENTO_ROOT . DS . "app" . DS . "code" . DS . $codePool . DS . "Namespace" . DS . "ModuleName" . DS;
// Setup the search patterns

$patterns = array(
    "/__\\(\\s*\\'(.*?)(?=(?<!\\\\)\\')/i",
    "/__\\(\\s*\\\"(.*?)(?=(?<!\\\\)\\\")/i",
);

function listFolderFiles($dir) {
    $fileList = array();
    $ffs = scandir($dir);
    foreach ($ffs as $ff) {
        if ($ff != '.' && $ff != '..') {
            if (is_dir($dir . DIRECTORY_SEPARATOR . $ff)) {
                $fileList = array_merge($fileList, listFolderFiles($dir . DIRECTORY_SEPARATOR . $ff));
            } else {
                $fileList[] = $dir . DIRECTORY_SEPARATOR . $ff;
            }
        }
    }
    return $fileList;
}

// Get a list of all PHP files in that directory
$fileList = listFolderFiles($pathToModule);
// Setup data storage arrayu
$csv = array();
// Loop through all files
foreach ($fileList as $file) {
    // Open the file
    $data = file_get_contents($file);
    // Make sure we've got some data
    if ($data) {
        // Process each pattern
        foreach ($patterns as $pattern) {
            // Match all __('') strings
            preg_match_all($pattern, $data, $matches);
            // Check if we've got some results
            if (count($matches[1]) > 0) {
                // Loop through the result
                foreach ($matches[1] as $match) {
                    // Add line to csv array
                    $csv[$match] = array($match, $match);
                }
            }
        }
    }
}

//output to csv file
$fp = fopen($outputCSV, 'w');

foreach ($csv as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
echo "Output written to $outputCSV file";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment