Excel-Web Queries to Babelfish

S

Sam

Hi

I have tried an example that has been floating around this newsgroup

It seems to work except for the fact that it seems that the text sent back
to me is truncated??

Anyone seen this problem???

Thanks

Code
------

Enum TranslateLanguages
Eng_Fren = 1
Eng_Ger = 2
Eng_Ita = 3
Eng_Port = 4
Eng_Span = 5
Fren_Eng = 6
Fren_Ger = 7
Ger_Eng = 8
Ger_Fren = 9
Ita_Eng = 10
Port_Eng = 11
Span_Eng = 12
End Enum

Public Function gsTranslateText(rsTextToTranslate, rMode As
TranslateLanguages) As String
Dim xml As XMLHTTP30
Dim abytPostData() As Byte
Dim sMode As String
Dim sResponse As String
Dim nStartPos As Integer
Dim nEndPos As Integer


Select Case rMode
Case Eng_Fren
sMode = "en_fr"
Case Eng_Ger
sMode = "en_de"
Case Eng_Ita
sMode = "en_it"
Case Eng_Port
sMode = "en_pt"
Case Eng_Span
sMode = "en_es"
Case Fren_Eng
sMode = "fr_en"
Case Fren_Ger
sMode = "fr_de"
Case Ger_Eng
sMode = "de_en"
Case Ger_Fren
sMode = "de_fr"
Case Ita_Eng
sMode = "it_en"
Case Port_Eng
sMode = "pt_en"
Case Span_Eng
sMode = "es_en"
End Select

abytPostData = StrConv("doit=done&intl=1" _
& "&tt=urltext&lp=" & sMode & "&urltext=" _
& rsTextToTranslate, vbFromUnicode)

Set xml = New XMLHTTP30
With xml
.open "POST", "http://babelfish.altavista.com/babelfish/tr"
.setRequestHeader "Content-Type",
"application/x-www-form-urlencoded"
.send abytPostData
Do While .readyState <> 4
DoEvents
Loop
sResponse = .responseText

End With

'/ find translation
nStartPos = InStr(1, sResponse, "lang=" & _
Right$(sMode, 2), vbTextCompare)
If nStartPos Then
nStartPos = nStartPos + 8
nEndPos = InStr(nStartPos, sResponse, _
"</div>", vbTextCompare) - 1
If nEndPos >= nStartPos Then gsTranslateText = _
Mid$(sResponse, nStartPos, nEndPos - _
nStartPos + 1)
End If

Set xml = Nothing
End Function

Text Back
---------



<html><head><title>AltaVista - Babel Fish Translation - Translated
Text</title>
<meta name="description" content="AltaVista Babel Fish provides the online
text and web page language translation!">
<meta name="keywords" content="translation transl
 
T

Tim Williams

What do you get if you just do

Msgbox .responseText

once the request completes? Is that truncated or is it happening in your
downstream code ?


Tim
 
N

NickHK

Sam,
"lang=" is not (currently anyway) in the source returned from BabelFish
There's probably a better way, but this get the returned text
<Code>
'First find the 2nd occurrence of "class=s"
nStartPos = InStr(1, sResponse, "class=s", vbTextCompare)
nStartPos = InStr(nStartPos + 1, sResponse, "class=s", vbTextCompare)

If nStartPos > 0 Then
nStartPos = nStartPos + 33

nEndPos = InStr(nStartPos, sResponse, "</div>", vbTextCompare) - 1

If nEndPos > nStartPos Then
gsTranslateText = Mid$(sResponse, nStartPos, nEndPos - nStartPos
+ 1)
End If
End If
</Code>

NickHK
 
S

Sam

Hi thanks for your reply

The text I get back in
sResponse = .responseText

Is at the end of my post - It looks like I am only getting only part of the
data???
 
Top