From: <te...@us...> - 2014-12-18 02:07:10
|
Revision: 7029 http://sourceforge.net/p/web-erp/reponame/7029 Author: tehonu Date: 2014-12-18 02:07:07 +0000 (Thu, 18 Dec 2014) Log Message: ----------- Function allowing automatic translations fron Google Translate API. To be used with multi-language descriptions. Modified Paths: -------------- trunk/SystemParameters.php trunk/sql/mysql/upgrade4.11-4.12.sql Added Paths: ----------- trunk/includes/GoogleTranslator.php Modified: trunk/SystemParameters.php =================================================================== --- trunk/SystemParameters.php 2014-12-17 09:11:47 UTC (rev 7028) +++ trunk/SystemParameters.php 2014-12-18 02:07:07 UTC (rev 7029) @@ -261,6 +261,9 @@ if ($_SESSION['AllowOrderLineItemNarrative'] != $_POST['X_AllowOrderLineItemNarrative'] ) { $sql[] = "UPDATE config SET confvalue = '". $_POST['X_AllowOrderLineItemNarrative']."' WHERE confname = 'AllowOrderLineItemNarrative'"; } + if ($_SESSION['GoogleTranslatorAPIKey'] != $_POST['X_GoogleTranslatorAPIKey'] ) { + $sql[] = "UPDATE config SET confvalue = '". $_POST['X_GoogleTranslatorAPIKey']."' WHERE confname = 'GoogleTranslatorAPIKey'"; + } if ($_SESSION['RequirePickingNote'] != $_POST['X_RequirePickingNote'] ) { $sql[] = "UPDATE config SET confvalue = '". $_POST['X_RequirePickingNote']."' WHERE confname = 'RequirePickingNote'"; } @@ -486,6 +489,11 @@ <td>' . _('Select the languages in which translations of the item description will be maintained. The default language is excluded.') . '</td> </tr>'; +// Google Translator API Key +echo '<tr style="outline: 1px solid"><td>' . _('Google Translator API Key') . ':</td> + <td><input type="text" name="X_GoogleTranslatorAPIKey" size="25" maxlength="50" value="' . $_SESSION['GoogleTranslatorAPIKey'] . '" /></td> + <td>' . _('Google Translator API Key to allow automatic translations. More info at https://cloud.google.com/translate/') . '</td></tr>'; + //'RequirePickingNote' echo '<tr style="outline: 1px solid"><td>' . _('A picking note must be produced before an order can be delivered') . ':</td> <td><select name="X_RequirePickingNote"> Added: trunk/includes/GoogleTranslator.php =================================================================== --- trunk/includes/GoogleTranslator.php (rev 0) +++ trunk/includes/GoogleTranslator.php 2014-12-18 02:07:07 UTC (rev 7029) @@ -0,0 +1,33 @@ +<?php + +// Detailed info on Google Translator API https://cloud.google.com/translate/ +// This webERP-style code is based on http://hayageek.com/google-translate-api-tutorial/ + +function translate_via_google_translator($text,$target,$source=false){ + $url = 'https://www.googleapis.com/language/translate/v2?key=' . $_SESSION['GoogleTranslatorAPIKey'] . '&q=' . rawurlencode($text); + $url .= '&target='.$target; + if($source){ + $url .= '&source='.$source; + } + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($ch); + curl_close($ch); + $obj =json_decode($response,true); //true converts stdClass to associative array. + if($obj != null){ + if(isset($obj['error'])){ + $TranslatedText = "ERROR: " . $obj['error']['message']; + } + else{ + $TranslatedText = $obj['data']['translations'][0]['translatedText']; + // if(isset($obj['data']['translations'][0]['detectedSourceLanguage'])) //this is set if only source is not available. + // echo "Detecte Source Languge : ".$obj['data']['translations'][0]['detectedSourceLanguage']."n"; + } + } + else{ + $TranslatedText = "UNKNOW ERROR"; + } + return $TranslatedText; +} + +?> \ No newline at end of file Modified: trunk/sql/mysql/upgrade4.11-4.12.sql =================================================================== --- trunk/sql/mysql/upgrade4.11-4.12.sql 2014-12-17 09:11:47 UTC (rev 7028) +++ trunk/sql/mysql/upgrade4.11-4.12.sql 2014-12-18 02:07:07 UTC (rev 7029) @@ -127,4 +127,6 @@ ADD CONSTRAINT `sampleresults_ibfk_1` FOREIGN KEY (`testid`) REFERENCES `qatests` (`testid`); +INSERT INTO `config` (`confname` ,`confvalue`) VALUES ('GoogleTranslatorAPIKey', ''); + UPDATE config SET confvalue='4.12' WHERE confname='VersionNumber'; |