当前位置: 代码迷 >> VFP >> 用vfp实现Google翻译解决方法
  详细解决方案

用vfp实现Google翻译解决方法

热度:2540   发布时间:2013-02-26 00:00:00.0
用vfp实现Google翻译
如何实现Google中译英?
英译中关键语句为:
STRTRAN(Gethttp('http://translate.google.cn/translate_a/t?client=t&text='+trans_str+'&sl=auto&tl=zh-CN&pc=0'),CHR(0)," ") 

按道理中译英关键语句为:
STRTRAN(Gethttp('http://translate.google.cn/translate_a/t?client=t&text='+trans_str+'&sl=zh-CN&tl=en&pc=0'),CHR(0)," ") 

中译英好像有乱码,可参考下面英文翻译为中文的代码:大家讨论讨论:如何实现Google中译英?

**第二个参数为1时,英文译中文,第二个参数为2时,英文译中文拼音
**张宏2010年3月6日
clear
? transentoch("Can you tell me who are you?",1)
? transentoch("My name is zhanghong.",1)
? transentoch("how old are you?",1)
 

PROCEDURE transentoch
parameters trans_str,type
IF VARTYPE(trans_str)<>"C"
  RETURN ""
ELSE
  PRIVATE paratemp
  paratemp=CHRTRANC(trans_str,"abcdefghijklmnopqrstuvwxyz"+UPPER("abcdefghijklmnopqrstuvwxyz")+" ,./?<>:;'!@#$%^&*()_-+=|\1234567890"+'"',"")
  trans_str=CHRTRANC(trans_str,paratemp,"")
  trans_str=ALLTRIM(trans_str)
  IF EMPTY(trans_str)
  RETURN ""
  ENDIF
ENDIF
private lcgettempstr
lcgettempstr = STRTRAN(Gethttp('http://translate.google.cn/translate_a/t?client=t&text='+trans_str+'&sl=auto&tl=zh-CN&pc=0'),CHR(0)," ") 
IF VARTYPE(type)<>"N"
  type=1
ELSE
  IF !INLIST(type,1,2)
  type=1
  endif
ENDIF
PRIVATE temp_ret_str
IF type=1
  temp_ret_str=SUBSTR(lcgettempstr,AT('“”',lcgettempstr,1)+4, AT('“”',lcgettempstr,2) -AT('“”',lcgettempstr,1)-4)
  IF EMPTY(temp_ret_str)
  temp_ret_str=SUBSTR(lcgettempstr,AT('"trans":"',lcgettempstr,1)+9, AT('","orig"',lcgettempstr,1) -AT('"trans":"',lcgettempstr,1)-9)
  endif
ELSE
  temp_ret_str=SUBSTR(lcgettempstr,AT('“”',lcgettempstr,3)+4, AT('“”',lcgettempstr,4) -AT('“”',lcgettempstr,3)-4)
  IF EMPTY(temp_ret_str)
  temp_ret_str=SUBSTR(lcgettempstr,AT('"translit":"',lcgettempstr,1)+12, AT('"}]',lcgettempstr,1) -AT('"translit":"',lcgettempstr,1)-12)
  endif
ENDIF
RETURN temp_ret_str
endproc
procedure Gethttp
parameters lcurl
declare integer InternetOpen in wininet string, integer, string, string, string
declare integer InternetCloseHandle in wininet integer
declare integer InternetOpenUrl in wininet integer, string, string, integer, integer, integer
declare integer InternetReadFile in wininet integer, string @, integer, integer @
private hopen,lcgestr
lcgestr = ""
hopen = internetopen("vfp 6.0",1,0,0,0)
if hopen = 0
messagebox("本机 Dll 函数库无效!",16,"信息提示")
else
private hfile
hfile = InternetOpenUrl(hopen,lcurl,"",0,16,0)
if hfile <> 0
private lntotalbytesread,lnbytesread,lcbuffer
lntotalbytesread = 0
do while .t.
lcbuffer = replicate(CHR(0),4096)
lnbytesread = 0
if InternetReadFile(hfile,@lcbuffer,4096,@lnbytesread) = 1
lcgestr = lcgestr+lcbuffer
if lnbytesread = 0
exit
endif
lntotalbytesread = lntotalbytesread+lnbytesread
else
exit
endif
enddo
= InternetCloseHandle(hfile)
else
messagebox("获取网络信息失败!",48,"信息提示")
endif
= InternetCloseHandle(hopen)
endif
clear dlls
return lcgestr

------解决方案--------------------------------------------------------
VB code
Clear? transentoch("I love you",1)     && 英译汉? transentoch("我爱你",2)        && 汉译英Procedure transentoch    Parameters trans_str,Type    If Vartype(trans_str)<>"C"        Return ""    Else    Endif    If Vartype(Type)<>"N"        Type=1    Else        If !Inlist(Type,1,2)            Type=1        Endif    Endif    Private paratemp    If Type=1        paratemp=Chrtranc(Strconv(trans_str,2),"abcdefghijklmnopqrstuvwxyz"+Upper("abcdefghijklmnopqrstuvwxyz")+" ,./?<>:;'!@#$%^&*()_-+=|\1234567890"+'"',"")        trans_str=Chrtranc(trans_str,paratemp,"")    Else        trans_str=Chrtranc(Strconv(trans_str,2),"abcdefghijklmnopqrstuvwxyz"+Upper("abcdefghijklmnopqrstuvwxyz")+'@#^&*()_-+=|\<>"',"")    Endif    trans_str=Alltrim(trans_str)    If Empty(trans_str)        Return ""    Endif    Private lcgettempstr    If Type=1        lcgettempstr = Strtran(Gethttp('http://translate.google.cn/translate_a/t?client=t&text='+trans_str+'&sl=auto&tl=zh-CN&pc=0'),Chr(0)," ")        Private temp_ret_str        If Type=1            temp_ret_str=Substr(lcgettempstr,At('“”',lcgettempstr,1)+4, At('“”',lcgettempstr,2) -At('“”',lcgettempstr,1)-4)            If Empty(temp_ret_str)                temp_ret_str=Substr(lcgettempstr,At('"trans":"',lcgettempstr,1)+9, At('","orig"',lcgettempstr,1) -At('"trans":"',lcgettempstr,1)-9)            Endif        Else            temp_ret_str=Substr(lcgettempstr,At('“”',lcgettempstr,3)+4, At('“”',lcgettempstr,4) -At('“”',lcgettempstr,3)-4)            If Empty(temp_ret_str)                temp_ret_str=Substr(lcgettempstr,At('"translit":"',lcgettempstr,1)+12, At('"}]',lcgettempstr,1) -At('"translit":"',lcgettempstr,1)-12)            Endif        Endif        Return temp_ret_str    Else        *lcgettempstr = Strtran(Gethttp('http://translate.google.cn/translate_a/t?client=t&text='+trans_str+'&sl=zh-CN&tl=en&pc=0'),Chr(0)," ")        Local lcRemoteUrl,lcRemoteFile,lcLocalhtm,lcLocalFile        Declare Integer DeleteUrlCacheEntry In Wininet.Dll String szUrl        Declare Integer URLDownloadToFile In urlmon.Dll Integer pCaller,String szURL,String szFileName,Integer dwReserved,Integer lpfnCB        *--下载网页        lcRemoteUrl='http://translate.google.cn/translate_a/t?client=t&text='+trans_str+'&sl=zh-CN&tl=en&pc=0'        lcLocalhtm="c:\zhtoen.txt"        =DeleteUrlCacheEntry(lcRemoteUrl)    &&清理缓存,这时实时刷新的数据时很有用。        If URLDownloadToFile(0,lcRemoteUrl,lcLocalhtm,0,0)=0            lcZhToEnStr=Filetostr(lcLocalhtm)            Erase (lcLocalhtm)            Return Strextract(lcZhToEnStr,["],["],5)        Else            Messagebox("获取网络信息失败!",48,"信息提示")            Return ""        Endif    EndifEndprocProcedure Gethttp    Parameters lcurl    Declare Integer InternetOpen In wininet String, Integer, String, String, String    Declare Integer InternetCloseHandle In wininet Integer    Declare Integer InternetOpenUrl In wininet Integer, String, String, Integer, Integer, Integer    Declare Integer InternetReadFile In wininet Integer, String @, Integer, Integer @    Private hopen,lcgestr    lcgestr = ""    hopen = internetopen("vfp 6.0",1,0,0,0)    If hopen = 0        Messagebox("本机 Dll 函数库无效!",16,"信息提示")    Else        Private hfile        hfile = InternetOpenUrl(hopen,lcurl,"",0,16,0)        If hfile <> 0            Private lntotalbytesread,lnbytesread,lcbuffer            lntotalbytesread = 0            Do While .T.                lcbuffer = Replicate(Chr(0),4096)                lnbytesread = 0                If InternetReadFile(hfile,@lcbuffer,4096,@lnbytesread) = 1                    lcgestr = lcgestr+lcbuffer                    If lnbytesread = 0                        Exit                    Endif                    lntotalbytesread = lntotalbytesread+lnbytesread                Else                    Exit                Endif            Enddo            = InternetCloseHandle(hfile)        Else            Messagebox("获取网络信息失败!",48,"信息提示")        Endif        = InternetCloseHandle(hopen)    Endif    Clear Dlls    Return lcgestrEndproc
  相关解决方案