Post by crisboot » Fri Jun 24, 2011 9:17 pm

Automation does not resolve all our problems, however sometimes it's a good approach when you are trying to find a solution.
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/
This helped me to have a general idea of the conflicts, 67 between new files and modifications.
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();
 }

?>
As you can see this was my very dirty first approach, to start solving the issue. It works, but you have to take care about the sleeps, to avoid the abuse of service. Obviously, the generated files still need a human revision...

Thanks for you time :D

User avatar
Newbie

Posts

Joined
Fri Jun 24, 2011 11:57 am
Location - Rosario, Santa Fe, Argentina

Post by Qphoria » Fri Jun 24, 2011 9:57 pm

I started trying this a while back.... but gave up when I realized I only spoke one language. Great job tho! this will be a major help.

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by OSWorX » Wed Jun 29, 2011 4:49 am

Be aware, The G.Translate project is currently deprecated and will be closed completely on December 1, 2011.
Reade here: http://code.google.com/intl/en/apis/lan ... rview.html

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Guru Member

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria
Who is online

Users browsing this forum: No registered users and 7 guests