string format (??) confusion

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

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 ).

This also returns a blank translation:
SuperStrict

Import brl.retro

Local url:String = "ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + UrlEncode("WORDHERE") + "&langpair=el%7Cen"

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

Print LoadText(stream)

stream.Close()

Function UrlEncode:String(url:String) 
  Local buffer:String
  Local Char:Int
  Local i:Int
	
	For i = 1 To url.Length
	  Char = Asc(Mid(url, i, 1)) 
		
		If(Char >= 48 And Char <= 57) Or (Char >= 65 And Char <= 90) Or (Char >= 97 And Char <= 122) Or Char = 43 Or Char = 45 Or Char = 46 Or Char = 95
			buffer = buffer + Chr(Char) 
			
		Else
			buffer = buffer + "%" + Right(Hex(Char), 2) 
			
		End If
		
	Next
	
   Return buffer

End Function


What should the result be? I'm getting:
{"responseData": {"translatedText":"WORDHERE"}, "responseDetails": null, "responseStatus": 200}

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 ).
;)

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

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?)

Through the browser I get
{"responseData": {"translatedText":"Ge??"}, "responseDetails": null, "responseStatus": 200}

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

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)

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.

Weird..

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

(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:
SuperStrict

Framework BaH.libcurl
Import BRL.StandardIO

Local curl:TCurlEasy = TCurlEasy.Create()

curl.setOptInt(CURLOPT_FOLLOWLOCATION, 1)
curl.setWriteString()' use the internal string  to store the content

curl.setProgressCallback(progressCallback) ' set the progress callback function

'curl.setOptString(CURLOPT_URL, "blitzmax.com")
curl.setOptString(CURLOPT_URL, LoadText("url.txt"))
'curl.setOptString(CURLOPT_URL, "ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=Hello&langpair=en%7Cel")

'What is this supposed to be, since I'm not a browser, and I most certainly haven't gone to any other links yet
curl.setOptString(CURLOPT_REFERER, "www.google.com")

Local res:Int = curl.perform()

curl.cleanup()

Print curl.toString()

Function progressCallback:Int(data:Object, dltotal:Double, dlnow:Double, ultotal:Double, ulnow:Double)
	Print " ++++ " + dlnow + " bytes"
	Return 0	
End Function


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)

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.