Skip to content

Instantly share code, notes, and snippets.

@davorbudimcic
Created April 2, 2012 07:08
Show Gist options
  • Save davorbudimcic/2281359 to your computer and use it in GitHub Desktop.
Save davorbudimcic/2281359 to your computer and use it in GitHub Desktop.
Magento Translation strings Grabber
<?php
header('Content-type: text/plain');
/**
* Magento Translation strings Grabber
* davor.budimcic@inchoo.net
*
**/
// Array of directory paths, on which the search will be performed
// './app/code/local/your_namespace/your_module'
// './app/design/frontend/your_package/your_theme/template'
$directories = array(
'./app/code/local/your_namespace/your_module',
'./app/design/frontend/your_package/your_theme/template'
);
// Include stats at the end of file
// true | false
$stats = true;
// Echo original string in translation placeholder (edit mode)
// true | false
$edit_mode = true;
// TODO: add option to load Magento's csv's and remove duplicates in output
// Let's start... do not edit below this line
// ****************************************************************************
$start = microtime(true);
$rexp = '@__\([\s]*[\'"](.*?)[\'"](?:[\s]*,.*)*[\s]*\)@i';
// unescaped version: __\([\s]*['"](.*?)['"](?:[\s]*,.*)*[\s]*\)
foreach($directories as $_dir){
$paths[] = getFilesFromDir($_dir);
}
$paths = array_flat($paths);
$files_count = count($paths);
if (!isset($res)) {
$res = array();
}
foreach ($paths as $path) {
$curfile = file_get_contents($path);
preg_match_all($rexp, $curfile, $tmp_res);
$res = array_merge($res, $tmp_res[1]);
}
$strings_count = count($res);
$res = array_unique($res);
$unique_count = count($res);
foreach ($res as $item){
if (trim($item) != '') {
if ($edit_mode == true) {
echo '"'.$item.'","'.$item.'"'."\n";
}
else {
echo '"'.$item.'",""'."\n";
}
}
}
function getFilesFromDir($dir) {
$files = array();
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
if (is_dir($dir.'/'.$file)) {
$subdir = $dir.'/'.$file;
$files[] = getFilesFromDir($subdir);
}
else {
$files[] = $dir.'/'.$file;
}
}
}
closedir($handle);
}
return array_flat($files);
}
function array_flat($array) {
if (!is_array($array)) {
return FALSE;
}
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_flat($value));
}
else {
$result[$key] = $value;
}
}
return $result;
}
if ($stats == true) {
echo "\n// STATS //";
echo "\nScript execution time: ";
echo microtime(true)-$start;
echo "\nNumber of files: " . $files_count;
echo "\nTotal number of strings: " . $strings_count;
echo "\nAfter removing duplicates: " . $unique_count;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment