[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Displaying all characters in translated text

Posted on 2011-10-02
9
Medium Priority
?
361 Views
Last Modified: 2012-05-12
Hi
After some useful help from you experts I am close to getting my flash traslator to work the way I need it to (latest code is shown here). One issue remains and that is I am losing upper case letters and some other extended characters. By using the trace option I can see that what is returned from Google Translate is correct:

Esta parte es sólo un texto al azar para ver cómo se las arregla el traductor con palabras como: alternador, motor, transmisión y tyres.And esta parte es para probar cómo "las marcas de expresión" y el trabajo "apóstrofe" ¿Qué piensa usted?

But then what ends up in my textbox is:

sta parte es sólo un texto al azar para ver cómo se las arregla el traductor con palabras como: alternador, motor, transmisión y los neumáticos. Y esta parte es para poner a prueba cómo las "marcas del habla" y trabajar "apóstrofe"! ¿Qué piensa usted?

The original text was:

"This part is just some random text to see how the translator copes with words like: alternator, engine, transmission and tyres.

And this part is to test how "speech marks" and 'apostrophes' work!

What do you think?"


..so I am also losing line breaks. The textbox is set as standard Arial font, classic text, dynamic text, and if just as an experiment, I paste in the version with capitals and other characters it shows fine.

Your help will again be appreciated!
Tom
on (release) {
	var lorem_lv:LoadVars = new LoadVars();
	txtLang.text = "Current language code is: " + lstLang.getSelectedItem().data;
	strLang = lstLang.getSelectedItem().data;
	btnReset._visible = true
	
	if (strLang == null) {
		strLang = "en";
		txtLang.text = lstLang.getSelectedItem().data;
	}
	lorem_lv.onData = function(src:String):Void  {
		clip(src);
	}
	strSource = escape(txtContent.text);
	lorem_lv.load("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + strSource + "&langpair=en%7C" + strLang);

	function clip(str:String):Void {
		find_me = "translatedText";
		str = str.substr(str.indexOf(find_me)+ 17, str.length);
		str = str.substr(0, str.indexOf("},")-1);
		str = unescape(str);
		
		//get rid of any ampersands
		find_me = "\\u0026";
		pos = str.indexOf(find_me);
		while (pos >= 0) {
			str = str.substr (0, pos) + "&" + str.substr(pos + find_me.length, str.length);
			pos = str.indexOf(find_me);
		}
		trace(str);
		txtContent.htmlText = str;
	}
}

Open in new window

0
Comment
Question by:tomdenton
  • 5
  • 4
9 Comments
 
LVL 29

Expert Comment

by:dgofman
ID: 36901462
Because previous answers was not present a full solution, experts just fixed some problems in your code :)

      var result_lv:LoadVars = new LoadVars();
    result_lv.onLoad = function(success:Boolean) {
        if (success) {
                  var str:String = unescape(this);
                  var translatedText:String = str.split('":"')[1].split('"}, ')[0];
                  if(translatedText != undefind)
                        trace(translatedText)
        } else {
            trace("Error");
        }
    };
    var lorem_lv:LoadVars = new LoadVars();
      var from:String = "en";
      var to:String = "es";
      lorem_lv.v = "1.0";
      lorem_lv.q = "This part is just a random text to see how he manages the translator with words like";
      lorem_lv.langpair = from + "|" + to;
    lorem_lv.sendAndLoad("http://ajax.googleapis.com/ajax/services/language/translate", result_lv, "POST");
0
 

Author Comment

by:tomdenton
ID: 36902790
I did rush at a previous 'answer' sorry... :)

Many thanks for this it is much tidier and easier to follow.

I have now added: 'txtContent.htmlText = translatedText' after 'trace(translatedText) to put the result in a textbox and it works fine (I also set the text property anti alias to: Use sysem fonts, which helped)

What is now the best way of getting the original English text from the text box to send it to the translator? I tried changing the 'lorem_lv.q = "This part is just...";' to 'lorem_lv.q = escape(txtContent.text);' but I just get a blank result
0
 
LVL 29

Expert Comment

by:dgofman
ID: 36908284
Why do you want to use escape function. LoadVars will escape automatically.
Give me work scenario or key words what is not working for you
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tomdenton
ID: 36908376
The text I have in the textbox to start with is:

This will test how  'apostrophes' work!

And this to check line breaks.


If i use: 'lorem_lv.q = txtContent.text;' then the result I get in trace and back into the textbox is:

Esta parte es para poner a prueba el trabajo como \u0026quot;apóstrofe\u0026quot;! Y esto para comprobar los saltos de línea.

The apostrophes seem to be a problem and I do not get the line break. For this reason I tried (due to now knowing enough!!): 'lorem_lv.q = escape(txtContent.text);' then I get nothing back in the box.
0
 
LVL 29

Expert Comment

by:dgofman
ID: 36908606
Is this what you want?>

_root.createTextField("from_txt", _root.getNextHighestDepth(), 0, 0, 500, 50);
var from_txt:TextField = _root["from_txt"];
from_txt.multiline = true;
from_txt.text = "This will test how  'apostrophes' work!\n\nAnd this to check line breaks.";

_root.createTextField("to_txt", _root.getNextHighestDepth(), 0, 80, 500, 50);
var to_txt:TextField = _root["to_txt"];
to_txt.multiline = true;

var result_lv:LoadVars = new LoadVars();
result_lv.onLoad = function(success:Boolean) {
      if (success) {
                    var str:String = unescape(this);
                    var translatedText:String = str.split('":"')[1].split('"}, ')[0];
                    if(translatedText != undefind)
                              to_txt.text = translatedText.split('\\r').join('\n');
      } else {
            trace("Error");
      }
};
var lorem_lv:LoadVars = new LoadVars();
var from:String = "en";
var to:String = "es";
lorem_lv.v = "1.0";
lorem_lv.format = "text";
lorem_lv.q = from_txt.text;
lorem_lv.langpair = from + "|" + to;
lorem_lv.sendAndLoad("http://ajax.googleapis.com/ajax/services/language/translate", result_lv, "POST");
0
 
LVL 29

Expert Comment

by:dgofman
ID: 36908608
Esto pondrá a prueba el trabajo como 'apóstrofes'!

Y esto para verificar los saltos de línea.
0
 

Author Comment

by:tomdenton
ID: 36908661
Yes this is almost spot on :)

One small issue is that I now get the following back slashes:


Esto pondrá a prueba el trabajo como \"apóstrofe\"!

Y esto para comprobar los saltos de línea.
0
 
LVL 29

Accepted Solution

by:
dgofman earned 2000 total points
ID: 36911542
Replace in this case

to_txt.text = translatedText.split('\\r').join('\n').split('\\"').join("'");
0
 

Author Closing Comment

by:tomdenton
ID: 36912528
You're the man. Thanks for your help and patience with an actionscript novice!.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.
Suggested Courses
Course of the Month19 days, 18 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question