saeedgh
Total posts: 11
23 Oct 2021 04:16

Google Translate in amchart,

We tried to code Google Translate in amchart, but it does not function .

Can anyone help ?

The code does not return the trasnlated result .

The Code :

private function translate(e)
{
var result_lv:LoadVars = new LoadVars();
result_lv.onLoad = function () {
var str:String = unescape(this);
var txtContent;
var translatedText:String = str.split('":"')[1].split('"}, ')[0];
if(translatedText != undefined)
txtContent.text = translatedText.split('r').join('n').split('"').join("'");
return txtContent.text;
}
var lorem_lv:LoadVars = new LoadVars();
var from:String = "fa";
var to:String = "en"
lorem_lv.v = "1.0";
lorem_lv.format = "text";
lorem_lv.q = e;
lorem_lv.langpair = from + "|" + to;
lorem_lv.sendAndLoad(" http://ajax.googleapis.com/ajax/services/language/translate ", result_lv, "POST");
}

Download The Code :

google-translate

Last Modified: 28 Oct 2021


saeedgh
Total posts: 11
23 Oct 2021 04:48

This is ActionScript 2 code .

it can be used in amcharts for Mighty Analytics Component.


saeedgh
Total posts: 11
23 Oct 2021 04:57

We want to use this code in the legend and in all other title Strings in the amcharts .


saeedgh
Total posts: 11
23 Oct 2021 05:07

The Code should be changed to:

POST method should be changed to GET method:

lorem_lv.sendAndLoad("  http://ajax.googleapis.com/ajax/services/language/translate  ", result_lv, "GET");

saeedgh
Total posts: 11
23 Oct 2021 05:11

The Fixed File Is Attached Again :

google-translate-002


saeedgh
Total posts: 11
23 Oct 2021 05:15

Sample

LoadVars.sendAndLoad( )

Method Code Explained :

https://flylib.com/books/en/4.13.1.377/1/


saeedgh
Total posts: 11
27 Oct 2021 21:32

We have found a working PHP function that return the translated result properly :

https://github.com/statickidz/php-google-translate-free/blob/1.1.1/src/GoogleTranslate.php

Now we are trying to load the PHP function result in the Action Script 2 for Adobe Flash Professional CS6.

here is some explanation about how this is possible :

https://stackoverflow.com/questions/3133266/flash-calling-a-php-function

================================================

A Good and Complete Book about PHP for Flash :

Foundation PHP for Flash 1st Edition :

https://www.amazon.com/Foundation-PHP-Flash-Steve-Webster/dp/1903450160

Some Other Good and Complete Books about PHP for Flash :

Foundation PHP 5 for Flash 1st Edition :

https://www.amazon.com/Foundation-PHP-Flash-David-Powers/dp/B0096EPX7Q

Advanced PHP for Flash 1st ed. Edition :

https://www.amazon.com/Advanced-PHP-Flash-Steve-Webster/dp/1590591879

================================================

We have rewritten the action script function that now use the PHP GoogleTranslate function .

But it does not return the Translated result .

Please can someone help ?

Thanks

Here is the code that we have written :

Our function is written using the sample code on the below address :

https://stackoverflow.com/questions/2947142/easy-way-to-bring-a-php-variable-on-flash-with-as2

but it does not return the translated result . when I set the return String value to e variable the function work . but when I set the return to $output variable it does not function .

Action Script 2 Code :

function googletranslate(e:String):String
{
/*LoadVars send example*/
// init LoadVars Object
var lv:LoadVars = new LoadVars();
// set Variables
lv.sVar1 = "fa";
lv.sVar2 = "en";
lv.sVar3 = e;
// define onLoad Callback
lv.onLoad = onLoadCallBack;
// send and load variables
lv.sendAndLoad("google-translate-result.php?", lv, "POST");
var $output;
// onLoad Callback
function onLoadCallBack(succes)
{
// if succes
if(succes)
{
// trace variables
$output = this.lVarresult;
}
else
{
// loading failed
$output = "Loading Error!!";
}
}
return $output;
}

=============================================================

Edit :

The PHP (google-translate-result.php) Code is changed :

$_POST changed to $_GET and now the result php code is working itself through using this url :

Server_URL/google-translate-result.php?sVar1=fa&sVar2=en&sVar3=%DA%A9%D8%AA%D8%A7%D8%A8%20%D8%AA%D8%B3%D8%AA

but still the ActionScript does not return the Translated Result .

=============================================================

PHP Code (google-translate-result.php) :

<?php
require_once('GoogleTranslate.php');
// get variables
$var1 = $_GET['sVar1'];
$var2 = $_GET['sVar2'];
$var3 = $_GET['sVar3'];
$result = GoogleTranslate::translate($var1,$var2,$var3);
// send variables
echo "&lVarresult=$result&";
?>

PHP Code (GoogleTranslate.php) :

<?php
/**
* GoogleTranslate.class.php
*
* Class to talk with Google Translator for free.
*
* @package PHP Google Translate Free;
* @category Translation
* @author Adrián Barrio Andrés
* @author Paris N. Baltazar Salguero <This email address is being protected from spambots. You need JavaScript enabled to view it.
				document.getElementById('cloak471856904f8009b9fe667cda06d7714a').innerHTML = '';
				var prefix = 'ma' + 'il' + 'to';
				var path = 'hr' + 'ef' + '=';
				var addy471856904f8009b9fe667cda06d7714a = 'sieg.sb' + '@';
				addy471856904f8009b9fe667cda06d7714a = addy471856904f8009b9fe667cda06d7714a + 'gmail' + '.' + 'com';
				var addy_text471856904f8009b9fe667cda06d7714a = 'sieg.sb' + '@' + 'gmail' + '.' + 'com';document.getElementById('cloak471856904f8009b9fe667cda06d7714a').innerHTML += ''+addy_text471856904f8009b9fe667cda06d7714a+'';
		>
* @copyright 2016 Adrián Barrio Andrés
* @license  https://opensource.org/licenses/GPL-3.0  GNU General Public License 3.0
* @version 2.0
* @link  https://statickidz.com/ 
*/
/**
* Main class GoogleTranslate
*
* @package GoogleTranslate
*
*/
class GoogleTranslate
{
/**
* Retrieves the translation of a text
*
* @param string $source
* Original language of the text on notation xx. For example: es, en, it, fr...
* @param string $target
* Language to which you want to translate the text in format xx. For example: es, en, it, fr...
* @param string $text
* Text that you want to translate
*
* @return string a simple string with the translation of the text in the target language
*/
public static function translate($source, $target, $text)
{
// Request translation
$response = self::requestTranslation($source, $target, $text);
// Get translation text
// $response = self::getStringBetween("onmouseout=\"this.style.backgroundColor='#fff'\">", "</span></div>", strval($response));
// Clean translation
$translation = self::getSentencesFromJSON($response);
return $translation;
}
/**
* Internal function to make the request to the translator service
*
* @internal
*
* @param string $source
* Original language taken from the 'translate' function
* @param string $target
* Target language taken from the ' translate' function
* @param string $text
* Text to translate taken from the 'translate' function
*
* @return object[] The response of the translation service in JSON format
*/
protected static function requestTranslation($source, $target, $text)
{
// Google translate URL
$url = " https://translate.google.com/translate_a/single?client=at& ;dt=t&dt=ld&dt=qca&dt=rm&dt=bd&dj=1&hl=es-ES&ie=UTF-8&oe=UTF-8&inputm=2&otf=2&iid=1dd3b944-fa62-4b55-b330-74909a99969e";
$fields = array(
'sl' => urlencode($source),
'tl' => urlencode($target),
'q' => urlencode($text)
);
// URL-ify the data for the POST
$fields_string = "";
foreach ($fields as $key => $value) {
$fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string, '&');
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1');
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
return $result;
}
/**
* Dump of the JSON's response in an array
*
* @param string $json
* The JSON object returned by the request function
*
* @return string A single string with the translation
*/
protected static function getSentencesFromJSON($json)
{
$sentencesArray = json_decode($json, true);
$sentences = "";
foreach ($sentencesArray["sentences"] as $s) {
$sentences .= isset($s["trans"]) ? $s["trans"] : '';
}
return $sentences;
}
}

Thanks


saeedgh
Total posts: 11
28 Oct 2021 11:38

The previous Reply has been changed and improved .


saeedgh
Total posts: 11
28 Oct 2021 18:24

Action Script 2 Code changed to:

function googletranslate(e:String):String
{
/*LoadVars send example*/
// init LoadVars Object
var lv:LoadVars = new LoadVars();
var result_lv:LoadVars = new LoadVars();
// set Variables
lv.sVar1 = "fa";
lv.sVar2 = "en";
lv.sVar3 = e;
// send and load variables
lv.sendAndLoad("google-translate-result.php?", result_lv, "POST");
return result_lv.lVarresult;
}

======================================================================================

According to the Mentioned Ebooks , PHP Code (google-translate-result.php) chanded To :

<?php
require_once('GoogleTranslate.php');
// get variables
$var1 = $_GET['sVar1'];
$var2 = $_GET['sVar2'];
$var3 = $_GET['sVar3'];
$result = GoogleTranslate::translate($var1,$var2,$var3);
// send variables
print "&lVarresult=".urlencode($result)."&";
exit;
?>

======================================================================================

The PHP Code (GoogleTranslate.php) Remained with no change.

======================================================================================


saeedgh
Total posts: 11
28 Oct 2021 18:26

but still the translated result did not returned.


saeedgh
Total posts: 11
28 Oct 2021 19:20
Powered by Cobalt