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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.