string format (??) confusion

BlitzMax Forums/BlitzMax Programming/string format (??) confusion

plash(Posted 2008) [#1]
The following url
ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=WORDHERE&langpair=el%7Cen

(when opened in Firefox [and IE?]) responds "{"responseData": {"translatedText":"Hello"}, "responseDetails": null, "responseStatus": 200}", which is correct.

However, this code
Local url:String = "ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=WORDHERE&langpair=el%7Cen"

Local stream:TStream = OpenStream("http::" + url)
Assert stream, "Couldn't establish connection"

Print LoadText(stream)

stream.Close()

prints "{"responseData": {"translatedText":""}, "responseDetails": null, "responseStatus": 200}".

What exactly is going on here?

EDIT: Argh!! bloody forums, replace "WORDHERE" with the Greek word for hello (get it here: www.ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hello&langpair=en%7Cel ).


plash(Posted 2008) [#2]
This also returns a blank translation:



Azathoth(Posted 2008) [#3]
What should the result be? I'm getting:
{"responseData": {"translatedText":"WORDHERE"}, "responseDetails": null, "responseStatus": 200}


plash(Posted 2008) [#4]
EDIT: Argh!! bloody forums, replace "WORDHERE" with the Greek word for hello (get it here: www.ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hello&langpair=en%7Cel ).
;)


Azathoth(Posted 2008) [#5]
I can't even get it to work through the browser.

ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%93%B5%B9%AC&langpair=el%7Cen

returns that same thing the code returns


plash(Posted 2008) [#6]
Ah, so UrlEncoding got me nowhere.. (what does this ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=WORDHERE&langpair=el%7Cen do for you?)


Azathoth(Posted 2008) [#7]
Through the browser I get
{"responseData": {"translatedText":"Ge??"}, "responseDetails": null, "responseStatus": 200}

With the Blitzmax code I get
{"responseData": {"translatedText":""}, "responseDetails": null, "responseStatus": 200}


plash(Posted 2008) [#8]
What browser are you using?

Are you sure the link you tried (in the browser) has '&langpair=el%7Cen' at the end? ('%7C' = '|', el|en = Greek to English)


Azathoth(Posted 2008) [#9]
IE7.

I'm using the address
http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=Γειά&langpair=el%7Cen

Edit: That aaeu thing is what the forum put in.


plash(Posted 2008) [#10]
Weird..

Anyhow, does no one know how to achieve this??


plash(Posted 2008) [#11]
(Continuing from the discussion here)

You need to convert your text to UTF-8
Saved the url as 'url.txt' under "UTF-8", "Unicode", "Unicode big endian" and "ANSI" (all from notepad), then loaded it back using 'url = LoadText("url.txt")'. That would be the only way I know how to convert text in BlitzMax.. and it still returned nothing (except the 'ANSI' version, that was "Ge??").

and the escape it ;-)
'and then escape it'?

According to the docs, you really need to set the referrer
The referrer is the link I was just at right? what if I haven't been to a link yet?

My current jibberish:



Brucey(Posted 2008) [#12]
That would be the only way I know how to convert text in BlitzMax

You wanna learn how UTF-8 works then :-)

By default Max strings are encoding in ISO 8859-1. Which is fine if you are using English, since it falls below ASCII 128. After that, you'll need to convert your ISO 8859-1 into UTF-8... and also url-encode the resultant conversion, since it is unlikely now to be valid ASCII (in the sense of what a URL expects to be valid)

Now, since it's you... ;-)
Function _textConvertMaxToUTF8:String(text:String)
	If Not text Then
		Return ""
	End If
	
	Local l:Int = text.length
	If l = 0 Then
		Return ""
	End If
	
	Local count:Int = 0
	Local s:Byte[] = New Byte[l * 3] ' max possible is 3 x original size.
	
	For Local i:Int = 0 Until l
		Local char:Int = text[i]

		If char < 128 Then
			s[count] = char
			count:+ 1
			Continue
		Else If char<2048
			s[count] = char/64 | 192
			count:+ 1
			s[count] = char Mod 64 | 128
			count:+ 1
			Continue
		Else
			s[count] =  char/4096 | 224
			count:+ 1
			s[count] = char/64 Mod 64 | 128
			count:+ 1
			s[count] = char Mod 64 | 128
			count:+ 1
			Continue
		EndIf
		
	Next

	Return String.fromBytes(s, count)
End Function


That will encode your string into UTF-8.

Note, you *only* want to encode the text you are translating..

Next up, you now run that through everyone's favourite url-encoder :
curl.Escape()


So, an example might look like :
Local trans:String = "<hello world in greek>"
trans = curl.escape(_textConvertMaxToUTF8(trans))

Local s:String = "ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + trans + "&langpair=el%7Cen"

curl.setOptString(CURLOPT_URL, s)


As for the referrer : "Applications MUST always include a valid and accurate http referer header in their requests."
I have no idea what it is meant to be... perhaps the address of your module page? *shrug*

:o)


plash(Posted 2008) [#13]
I have no idea what it is meant to be... perhaps the address of your module page? *shrug*
Works for me! :)

{"responseData": {"translatedText":"Hello"}, "responseDetails": null, "responseStatus": 200}


Thanks, so much.