Created
December 14, 2011 10:29
-
-
Save robbanl/1476054 to your computer and use it in GitHub Desktop.
Get translatable strings for Magento modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
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
I use:
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)