Prepare to use Google Api v2

This commit is contained in:
Laurent Destailleur 2012-01-14 13:27:32 +01:00
parent 59619517e3
commit 9ea69cffb8
2 changed files with 48 additions and 34 deletions

View File

@ -1,20 +1,31 @@
<?php <?php
/* Copyright (C) 2009-2012 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, see <http://www.gnu.org/licenses/>.
*/
/** /**
* This software is licensed under GPL license agreement * \file dev/translation/langAutoParser.class.php
* This is a language automatic translator parser for Dolibarr * \ingroup dev
* This script uses google language ajax api as the translator engine * \brief This file is an example for a command line script
* 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 :)
*
* http://code.google.com/intl/fr/apis/ajaxlanguage/documentation/#SupportedPairs
*/ */
/** /**
* Class to parse language files and translate them * Class to parse language files and translate them
* This is a language automatic translator parser for Dolibarr
*/ */
class langAutoParser class autoTranslator
{ {
private $translatedFiles = array(); private $translatedFiles = array();
private $destLang = ''; private $destLang = '';
@ -24,11 +35,13 @@ class langAutoParser
private $time; private $time;
private $time_end; private $time_end;
private $outputpagecode = 'UTF-8'; private $outputpagecode = 'UTF-8';
private $apikey;
//private $outputpagecode = 'ISO-8859-1'; //private $outputpagecode = 'ISO-8859-1';
const DIR_SEPARATOR = '/'; const DIR_SEPARATOR = '/';
function __construct($destLang,$refLang,$langDir,$limittofile){ function __construct($destLang,$refLang,$langDir,$limittofile,$apikey)
{
// Set enviorment variables // Set enviorment variables
$this->destLang = $destLang; $this->destLang = $destLang;
@ -36,6 +49,7 @@ class langAutoParser
$this->langDir = $langDir.self::DIR_SEPARATOR; $this->langDir = $langDir.self::DIR_SEPARATOR;
$this->time = date('Y-m-d H:i:s'); $this->time = date('Y-m-d H:i:s');
$this->limittofile = $limittofile; $this->limittofile = $limittofile;
$this->apikey = $apikey;
// Translate // Translate
//ini_set('default_charset','UTF-8'); //ini_set('default_charset','UTF-8');
@ -273,18 +287,16 @@ class langAutoParser
//setting language pair //setting language pair
$lang_pair = $src_lang.'|'.$dest_lang; $lang_pair = $src_lang.'|'.$dest_lang;
$src_texts_query = "";
$src_text_to_translate=preg_replace('/%s/','SSSSS',join('',$src_texts)); $src_text_to_translate=preg_replace('/%s/','SSSSS',join('',$src_texts));
$src_text_to_translate=preg_replace('/'.preg_quote('\n\n').'/',' NNNNN ',$src_text_to_translate); $src_text_to_translate=preg_replace('/'.preg_quote('\n\n').'/',' NNNNN ',$src_text_to_translate);
$src_texts_query .= "&q=".urlencode($src_text_to_translate); // Define GET URL v1
$url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=".urlencode($src_text_to_translate)."&langpair=".urlencode($lang_pair);
// Define GET URL v2
// Example: "https://www.googleapis.com/language/translate/v2?key=".$this->apikey."&q=".urlencode($src_text_to_translate)."&source=".$src_lang."&target=".$dest_lang
$url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0".$src_texts_query."&langpair=".urlencode($lang_pair); // Send request
print "Url to translate: ".$url."\n";
// sendRequest
// note how referer is set manually
//print "Url to translate: ".$url."\n";
if (! function_exists("curl_init")) if (! function_exists("curl_init"))
{ {

View File

@ -1,6 +1,6 @@
#!/usr/bin/php #!/usr/bin/php
<?php <?php
/* Copyright (C) 2009 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2009-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -18,10 +18,12 @@
/** /**
* \file dev/translation/autotranslator.php * \file dev/translation/autotranslator.php
* \ingroup mymodule othermodule1 othermodule2 * \ingroup dev
* \brief This file is an example for a command line script * \brief This script uses google language ajax api as the translator engine
* \author Put author name here * The main translator function can be found at:
* \remarks Put here some comments * http://code.google.com/intl/fr/apis/language/translate/overview.html
* http://translate.google.com/translate_tools
* https://code.google.com/apis/console
*/ */
$sapi_type = php_sapi_name(); $sapi_type = php_sapi_name();
@ -85,9 +87,9 @@ if ($argv[2] != 'all')
} }
} }
require_once(DOL_DOCUMENT_ROOT."/../dev/translation/langAutoParser.class.php"); require_once(DOL_DOCUMENT_ROOT."/../dev/translation/autotranslator.class.php");
$langParser = new langAutoParser($argv[2],$argv[1],$dir,$file); $langParser = new autoTranslator($argv[2],$argv[1],$dir,$file,$apikey);
print "***** Finished *****\n"; print "***** Finished *****\n";