2
0
forked from Wavyzz/dolibarr

New: Add an autotranslator tool that use google to build/update language files for a new language.

This commit is contained in:
Laurent Destailleur
2009-01-18 18:38:47 +00:00
parent 67d03aac16
commit 1476fac25c
5 changed files with 284 additions and 16 deletions

View File

@@ -39,7 +39,10 @@ For users:
For translators:
- Added ca_ES language files
- Added autotranslator tool. A tool to build/update automatically
languages files using Google API for a new language. Wonderfull to start a
new translation.
For developers:
- Removed som deprecated files.
- Renamed all function dolibarr_xxx into dol_xxx to have same prefix everywhere.

View File

@@ -18,13 +18,13 @@
*/
/**
\file dev/skeletons/skeleton_page.php
\ingroup mymodule othermodule1 othermodule2
\brief This file is an example of a php page
\version $Id$
\author Put author name here
\remarks Put here some comments
*/
* \file dev/skeletons/skeleton_page.php
* \ingroup mymodule othermodule1 othermodule2
* \brief This file is an example of a php page
* \version $Id$
* \author Put author name here
* \remarks Put here some comments
*/
require("./pre.inc.php");
require_once(DOL_DOCUMENT_ROOT."/../dev/skeletons/skeleton_class.class.php");

View File

@@ -18,17 +18,17 @@
*/
/**
\file dev/skeletons/skeleton_script.php
\ingroup mymodule othermodule1 othermodule2
\brief This file is an example for a command line script
\version $Id$
\author Put author name here
\remarks Put here some comments
*/
* \file dev/skeletons/skeleton_script.php
* \ingroup mymodule othermodule1 othermodule2
* \brief This file is an example for a command line script
* \version $Id$
* \author Put author name here
* \remarks Put here some comments
*/
// Test if batch mode
$sapi_type = php_sapi_name();
$script_file=__FILE__;
$script_file=__FILE__;
if (eregi('([^\\\/]+)$',$script_file,$reg)) $script_file=$reg[1];
$path=eregi_replace($script_file,'',$_SERVER["PHP_SELF"]);

View File

@@ -0,0 +1,77 @@
<?PHP
/* Copyright (C) 2009 Laurent Destailleur <eldy@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**
* \file dev/translation/autotranslator.php
* \ingroup mymodule othermodule1 othermodule2
* \brief This file is an example for a command line script
* \version $Id$
* \author Put author name here
* \remarks Put here some comments
*/
// Test if batch mode
$sapi_type = php_sapi_name();
$script_file=__FILE__;
if (eregi('([^\\\/]+)$',$script_file,$reg)) $script_file=$reg[1];
$path=eregi_replace($script_file,'',$_SERVER["PHP_SELF"]);
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";
exit;
}
// Include Dolibarr environment
require_once($path."../../htdocs/master.inc.php");
// After this $db is an opened handler to database. We close it at end of file.
// Load main language strings
$langs->load("main");
// Global variables
$version='$Revision$';
$error=0;
// -------------------- START OF YOUR CODE HERE --------------------
@set_time_limit(0);
print "***** ".$script_file." (".$version.") *****\n";
// Check parameters
if (! isset($argv[2])) {
print "Usage: ".$script_file." lang_code_src lang_code_dest\n";
print "Example: ".$script_file." en_US pt_PT\n";
exit;
}
// Show parameters
print 'Argument 1='.$argv[1]."\n";
print 'Argument 2='.$argv[2]."\n";
print 'Files will be generated/updated in directory '.DOL_DOCUMENT_ROOT."/langs\n";
// Examples for manipulating class skeleton_class
require_once(DOL_DOCUMENT_ROOT."/../dev/translation/langAutoParser.class.php");
$langParser = new langAutoParser($argv[2],$argv[1],DOL_DOCUMENT_ROOT.'/langs');
// -------------------- END OF YOUR CODE --------------------
$db->close();
return $error;
?>

View File

@@ -0,0 +1,188 @@
<?php
/**
* This software is licensed under GPL license agreement
* This is a language automatic translator parser for Dolibarr
* This script uses google language ajax api as the translator engine
* The main translator function can be found at:
*
* http://www.plentyofcode.com/2008/10/google-translation-api-translate-on.html
* Hope you make a good use of it :)
*/
class langAutoParser {
private $translatedFiles = array();
private $destLang = string;
private $refLang = string;
private $langDir = string;
const DIR_SEPARATOR = '/';
function __construct($destLang,$refLang,$langDir){
// Set enviorment variables
$this->destLang = $destLang;
$this->refLang = $refLang;
$this->langDir = $langDir.self::DIR_SEPARATOR;
$this->time = date('Y-m-d H:i:s');
// Translate
ini_set('default_charset','ISO-8859-1');
$this->parseRefLangTranslationFiles();
}
private function parseRefLangTranslationFiles(){
$files = $this->getTranslationFilesArray($this->refLang);
$counter = 1;
foreach($files as $file) {
$counter++;
$fileContent = null;
$this->translatedFiles = array();
$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);
print "Processing file " . $file . "<br>\n";
// Check destination file presence
if ( ! file_exists( $destPath ) ){
// No file presente generate file
echo "File not found: " . $file . "<br>\n";
echo "Generating file " . $file . "<br>\n";
$this->createTranslationFile($destPath);
}
// Translate lines
$fileContentDest = file($destPath,FILE_IGNORE_NEW_LINES |
FILE_SKIP_EMPTY_LINES);
foreach($fileContent as $line){
$key = $this->getLineKey($line);
$value = $this->getLineValue($line);
$this->translateFileLine($fileContentDest,$file,$key,$value);
}
$this->updateTranslationFile($destPath,$file);
#if ($counter ==3) die('fim');
}
}
private function updateTranslationFile($destPath,$file){
if (count($this->translatedFiles[$file])>0){
$fp = fopen($destPath, 'a');
fwrite($fp, "\r\n");
fwrite($fp, "\r\n");
fwrite($fp, "// Date " . $this->time . "\r\n");
fwrite($fp, "// START - Lines generated via parser\r\n");
fwrite($fp, "// Reference language: {$this->refLang}\r\n");
foreach( $this->translatedFiles[$file] as $line) {
fwrite($fp, $line . "\r\n");
}
fwrite($fp, "// Date " . $this->time . "\r\n");
fwrite($fp, "// STOP - Lines generated via parser\r\n");
fclose($fp);
}
return;
}
private function createTranslationFile($path){
$fp = fopen($path, 'w+');
fwrite($fp, "/*\r\n");
fwrite($fp, " * Lince Translation File\r\n");
fwrite($fp, " * Filename: {$file}\r\n");
fwrite($fp, " * Language code: {$this->destLang}\r\n");
fwrite($fp, " * Automatic generated via autotranslator tool\r\n");
fwrite($fp, " * Generation date " . $this->time. "\r\n");
fwrite($fp, " */\r\n");
fclose($fp);
return;
}
private function translateFileLine($content,$file,$key,$value){
foreach( $content as $line ) {
$destKey = $this->getLineKey($line);
$destValue = $this->getLineValue($line);
// If translated return
if ( $destKey == $key ) { return; }
}
// If not translated then translate
$this->translatedFiles[$file][] = $key . '=' .
utf8_decode($this->translateTexts(array($value),substr($this->refLang,0,2),substr($this->destLang,0,2)));
}
private function getLineKey($line){
$key = preg_match('/^(.*)=/',$line,$matches);
return trim( $matches[1] );
}
private function getLineValue($line){
$value = preg_match('/=(.*)$/',$line,$matches);
return trim( $matches[1] );
}
private function getTranslationFilesArray($lang){
$dir = new DirectoryIterator($this->langDir.$lang);
while($dir->valid()) {
if(!$dir->isDot() && $dir->isFile() && ! eregi('^\.',$dir->getFilename())) {
$files[] = $dir->getFilename();
}
$dir->next();
}
return $files;
}
private function translateTexts($src_texts = array(), $src_lang,
$dest_lang){
//setting language pair
$lang_pair = $src_lang.'|'.$dest_lang;
$src_texts_query = "";
foreach ($src_texts as $src_text){
$src_texts_query .= "&q=".urlencode($src_text);
}
$url =
"http://ajax.googleapis.com/ajax/services/language/translate?v=1.0".$src_texts_query."&langpair=".urlencode($lang_pair);
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://www.YOURWEBSITE.com");
$body = curl_exec($ch);
curl_close($ch);
// now, process the JSON string
$json = json_decode($body, true);
if ($json['responseStatus'] != 200){
return false;
}
$results = $json['responseData'];
$return_array = array();
foreach ($results as $result){
if ($result['responseStatus'] == 200){
$return_array[] = $result['responseData']['translatedText'];
} else {
$return_array[] = false;
}
}
//return translated text
#return $return_array;
return $json['responseData']['translatedText'];
}
}
?>