Some times it's funny to make a time to play with automation. But wait...
I've started at this forum yesterday and I didn't say hello. Where are my manners? Let me present myself, I'm crisboot the new kid of the block.
What can I say? I'm a programmer, and eventually I had problems to sleep at night... (like the major part of the nerds, dreamers, and environmentalists...)
As I said, I was there amazed from the new 1.5 release and I've discovered that the spanish language pack was for 1.4.9.3, I've tryed to install it anyways...
but sadly it doesn't fit... I said to myself with determination: "I can do it... with a dirty script in less than an hour..."
As an old boy my first option was bash. I thought I've have to compare the folders of the different versions.
Code: Select all
diff -rq /opencart1.4.x/update/catalog/language/english/ /opencart1.5.x/update/catalog/language/english/
As a completly newbie to the platform I took one as test case, to see if only the internalization values were different and sadly I saw that the indexes also changed...
I've remembered a cool project on Google Translation API for PHP (http://code.google.com/p/gtranslate-api-php/).
That was when I came with the idea for this script:
Code: Select all
<?php
/**
* AutoTranslator
* @author crisboot
* @license GPL v3
* It requires GTranslate
* http://blog.josedasilva.net/google-translate-api-php-library/
*/
$dirIn = "/var/www/opencart/upload/catalog/language/english/";
$dirOut = "/var/www/opencart/upload/catalog/language/spanish/";
require("GTranslate.php");
error_reporting(E_ALL);
ini_set('display_error',1);
// file_array() by Jamon Holmgren. Exclude files by putting them in the $exclude
// string separated by pipes. Returns an array with filenames as strings.
function file_array($path, $exclude = ".|..", $recursive = true) {
$path = rtrim($path, "/") . "/";
$folder_handle = opendir($path);
$exclude_array = explode("|", $exclude);
$result = array();
while(false !== ($filename = readdir($folder_handle))) {
if(!in_array(strtolower($filename), $exclude_array)) {
if(is_dir($path . $filename . "/")) {
// Need to include full "path" or it's an infinite loop
if($recursive) $result[] = file_array($path . $filename . "/", $exclude, true);
} else {
$result[] = $path . $filename;
}
}
}
return $result;
}
$aFiles = file_array($dirIn);
echo "<pre>";
print_r($aFiles);
echo "</pre>";
/*foreach($aFileas as $aFile => $i){
}*/
/**
* Example using RequestHTTP
*/
$translate_string = "Das ist wunderschön";
try{
$gt = new Gtranslate;
foreach($aFiles as $i => $aFolders){
foreach($aFolders as $j => $aFile){
echo "\n******************************************************************************************************\n";
echo "* ".$aFile." *\n";
echo "******************************************************************************************************\n";
sleep(10);
$itFile = $aFile;
$handle = @fopen($itFile, "r");
if ($handle) {
$newFile = "";
$ln= 0;
while (!feof($handle)) {
$lines[] = fgets($handle, 4096);
if(preg_match('/\$\_/',$lines[$ln])){
echo "We found one : ";
preg_match('/\'{1}[^\']*\'{1};/',$lines[$ln], $result);
//obtain the texto to translate
$toTranslate = preg_replace("/'|;/", "", $result[0]);
//translate it
$ouTraslated = $gt->english_to_spanish($toTranslate);
//echo $toTranslate." - ".htmlentities($gt->english_to_spanish($toTranslate))."<br />";
echo $toTranslate." - ".$ouTraslated."<br />\n";
//change the line
$lines[$ln] = preg_replace('/\'{1}[^\']*\'{1};/',"'".$ouTraslated."';",$lines[$ln]);
}
$newFile .= $lines[$ln];
$ln++;
}
fclose($handle);
echo "<<<<<<<<<<<<<<".$itFile;
//write the changes to a new file
$ourFileName = preg_replace('/english/','spanish',$itFile);
echo ">>>>>>>>>>>>>>".$ourFileName;
if($outFile = fopen($ourFileName, 'w')){
fwrite($outFile, $newFile);
}
fclose($outFile);
unset($lines);
}
} //foreach files
}//foreach folders
} catch (GTranslateException $ge)
{
echo $ge->getMessage();
}
?>
Thanks for you time