0

We tried to code Google Translate in action script for Flash Professional CS6, 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, "GET");
}

Code Explained :

Sample

LoadVars.sendAndLoad( )

Method Code Explained :

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

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

Edit 1:

I edited my code :

I added the return code to the main function , so the main function has output .

but still the function does not return any transalted content .

The improved code :

private function translate(e)
{
var result_lv:LoadVars = new LoadVars();
var txtContent;
result_lv.onLoad = function () {
var str:String = unescape(this);
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, "GET");
return txtContent.text;
}

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

Edit 2:

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 :

Flash calling a PHP function

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

Edit 3:

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

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

Edit 4:

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

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

Edit 5:

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

See the answers below .

But it does not return the Translated result .

Please can someone help ?

Thanks

Saeed
  • 1
  • 6
  • Do you try to do this GET-request in browser or in Postman? Maybe this API is not available anymore or you need a token from Google to use it? – Mikhail Semikolenov Oct 27 '21 at 08:26
  • I try to use This GET request in an SWF object loaded in the browser . The API is v1 of Google Translate which does not require a key from google . but version 2 need key from google. – Saeed Oct 27 '21 at 12:32
  • for example This Google Translate PHP Class work in a web browser : https://github.com/statickidz/php-google-translate-free/blob/1.1.1/src/GoogleTranslate.php I am using Waterfox G3 and Xampp 1.7.1 and PHP 5.2.9 to test this script and it works fine . But here we need the action script to work Please . Thanks – Saeed Oct 27 '21 at 12:33
  • Maybe We can use this tools mentioned on the following page to load the php function results in action script : https://stackoverflow.com/questions/3133266/flash-calling-a-php-function – Saeed Oct 27 '21 at 12:58
  • We are trying to call the working php and curl function in action script . – Saeed Oct 27 '21 at 13:26
  • I think you should edit question and describe more verbose about what are you trying to do. E.g what is your web server where PHP script is running? Can you see a request from flash application in web server or php script logs? What you see in debugger in flash? – Mikhail Semikolenov Oct 27 '21 at 13:38

3 Answers3

0

In the PHP-script that you've mentioned in comments, authors used the link https://translate.google.com/translate_a/single?... with some client authenticaton data like iid and also set User-Agent to Android phone:

curl_setopt($ch, CURLOPT_USERAGENT, 'AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1');

I think this script is trying to masquerade request like it was sent from Android Google Translate App.

But in your AcrionScript code you are using another URL:

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

This simple-HTTP Google Translate API v1 is very old and already not available.

You can check it yourself by following this link - it's responds 404.

0

Here is the code that we have written :

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

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 <sieg.sb@gmail.com>
 * @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;
    }
}
Saeed
  • 1
  • 6
  • **(1)** Try renaming `$output` to just `output`... **(2)** Why not just `return this.lVarresult;` if the **lVarresult** is correct? **(3)** Maybe don't trap your `onLoadCallBack` function inside another function (`function googletranslate`), keep them as separate functions... – VC.One Oct 29 '21 at 05:28
  • Thanks . I changed the ActionScript 2 Code in a new answer . – Saeed Oct 29 '21 at 13:23
-1

According to the Comments of the previous answer 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;
}
Saeed
  • 1
  • 6