Enhance autotranslator to update lang files before a release.

This commit is contained in:
Laurent Destailleur 2010-07-16 17:26:29 +00:00
parent 5747e63c2b
commit b7df0faac4
2 changed files with 67 additions and 33 deletions

View File

@ -31,12 +31,13 @@ $path=dirname(__FILE__).'/';
// Test if batch mode // Test if batch mode
if (substr($sapi_type, 0, 3) == 'cgi') { if (substr($sapi_type, 0, 3) == 'cgi') {
echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n"; echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
exit; exit;
} }
// Include Dolibarr environment // Include Dolibarr environment
require_once($path."../../htdocs/master.inc.php"); require_once($path."../../htdocs/master.inc.php");
require_once($path."../../htdocs/lib/files.lib.php");
// After this $db is an opened handler to database. We close it at end of file. // After this $db is an opened handler to database. We close it at end of file.
// Load main language strings // Load main language strings
@ -54,7 +55,7 @@ $dir=DOL_DOCUMENT_ROOT."/langs";
// Check parameters // Check parameters
if (! isset($argv[2])) { if (! isset($argv[2])) {
print "Usage: ".$script_file." lang_code_src lang_code_dest [langfile.lang]\n"; print "Usage: ".$script_file." lang_code_src lang_code_dest|all [langfile.lang]\n";
print "Example: ".$script_file." en_US pt_PT\n"; print "Example: ".$script_file." en_US pt_PT\n";
print "Rem: lang_code to use can be found on http://www.google.com/language_tools\n"; print "Rem: lang_code to use can be found on http://www.google.com/language_tools\n";
exit; exit;
@ -71,8 +72,10 @@ if (isset($argv[3]))
} }
print 'Files will be generated/updated in directory '.$dir."\n"; print 'Files will be generated/updated in directory '.$dir."\n";
if (! is_dir($dir.'/'.$argv[2])) if ($argv[2] != 'all')
{ {
if (! is_dir($dir.'/'.$argv[2]))
{
print 'Create directory '.$dir.'/'.$argv[2]."\n"; print 'Create directory '.$dir.'/'.$argv[2]."\n";
$result=mkdir($dir.'/'.$argv[2]); $result=mkdir($dir.'/'.$argv[2]);
if (! $result) if (! $result)
@ -80,6 +83,7 @@ if (! is_dir($dir.'/'.$argv[2]))
$db->close(); $db->close();
return -1; return -1;
} }
}
} }
// Examples for manipulating class skeleton_class // Examples for manipulating class skeleton_class

View File

@ -50,20 +50,49 @@ class langAutoParser {
$fileContent = null; $fileContent = null;
$this->translatedFiles = array(); $this->translatedFiles = array();
$refPath = $this->langDir.$this->refLang.self::DIR_SEPARATOR.$file; $refPath = $this->langDir.$this->refLang.self::DIR_SEPARATOR.$file;
$destPath = $this->langDir.$this->destLang.self::DIR_SEPARATOR.$file; $fileContent = file($refPath,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
$fileContent = file($refPath,FILE_IGNORE_NEW_LINES |
FILE_SKIP_EMPTY_LINES);
print "Processing file " . $file . ", with ".sizeof($fileContent)." lines<br>\n"; print "Processing file " . $file . ", with ".sizeof($fileContent)." lines<br>\n";
// Define target dirs
$targetlangs=array($this->destLang);
if ($this->destLang == 'all')
{
$targetlangs=array();
// If we must process all languages
$arraytmp=dol_dir_list($this->langDir,'directories',0);
foreach($arraytmp as $dirtmp)
{
if ($dirtmp['name'] === $this->refLang) continue; // We discard source language
if (preg_match('/^en/i',$dirtmp['name'])) continue; // We discard en_* languages
if (preg_match('/^fr/i',$dirtmp['name'])) continue; // We discard fr_* languages
if (preg_match('/^es/i',$dirtmp['name'])) continue; // We discard es_* languages
if (preg_match('/es_CA/i',$dirtmp['name'])) continue; // We discard es_CA language
if (preg_match('/^\./i',$dirtmp['name'])) continue; // We discard files .*
if (preg_match('/^CVS/i',$dirtmp['name'])) continue; // We discard CVS
$targetlangs[]=$dirtmp['name'];
}
//var_dump($targetlangs);
}
// Process translation of source file for each target languages
foreach($targetlangs as $mydestLang)
{
$destPath = $this->langDir.$mydestLang.self::DIR_SEPARATOR.$file;
// Check destination file presence // Check destination file presence
if ( ! file_exists( $destPath ) ){ if ( ! file_exists( $destPath ) ){
// No file presente generate file // No file present, we generate file
echo "File not found: " . $file . "<br>\n"; echo "File not found: " . $destPath . ". We generate it.<br>\n";
echo "Generating file " . $file . "<br>\n"; $this->createTranslationFile($destPath,$mydestLang);
$this->createTranslationFile($destPath);
} }
else
{
echo "Updating file: " . $destPath . "<br>\n";
}
// Translate lines // Translate lines
$fileContentDest = file($destPath,FILE_IGNORE_NEW_LINES | $fileContentDest = file($destPath,FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
FILE_SKIP_EMPTY_LINES);
$newlines=0; $newlines=0;
foreach($fileContent as $line){ foreach($fileContent as $line){
$key = $this->getLineKey($line); $key = $this->getLineKey($line);
@ -80,6 +109,7 @@ FILE_SKIP_EMPTY_LINES);
#if ($counter ==3) die('fim'); #if ($counter ==3) die('fim');
} }
} }
}
private function updateTranslationFile($destPath,$file){ private function updateTranslationFile($destPath,$file){
@ -99,10 +129,10 @@ FILE_SKIP_EMPTY_LINES);
return; return;
} }
private function createTranslationFile($path){ private function createTranslationFile($path,$mydestlang){
$fp = fopen($path, 'w+'); $fp = fopen($path, 'w+');
fwrite($fp, "/*\r\n"); fwrite($fp, "/*\r\n");
fwrite($fp, " * Language code: {$this->destLang}\r\n"); fwrite($fp, " * Language code: {$mydestlang}\r\n");
fwrite($fp, " * Automatic generated via autotranslator.php tool\r\n"); fwrite($fp, " * Automatic generated via autotranslator.php tool\r\n");
fwrite($fp, " * Generation date " . $this->time. "\r\n"); fwrite($fp, " * Generation date " . $this->time. "\r\n");
fwrite($fp, " */\r\n"); fwrite($fp, " */\r\n");