Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use the Google Translator with API calls

API Functions

How to use the Google Translator with API calls

by  Mike Gagnon  Posted    (Edited  )
The Google translator is a usefull tool that can be used to translate a phrase from English to French for example (Under certain circumstance depending on the OS, VFP will only return information based on either the Version of VFP or the language of the OS). The translator isn't perfect, but pretty close. The following example will translate the current day of the week written out (CDOW) into French. You can try with any short phrase you wish (Longer the phrase the more inacurate the translation becomes, this is a limitation of Google) and add other languages as required.

Author : Christophe CHENAVIER

Code:
M.cPhrase = CDOW(DATE())
MessageBox(Translate(M.cPhrase))
 
 
*** Traduction au travers de l'outil GOOGLE
FUNCTION Translate
 
LPARAMETERS cPhrase, cType
 
IF NOT EMPTY(M.cPhrase)
   DO CASE
      CASE VARTYPE(M.cType) <> 'C'
           M.cType = "en%7Cfr"
 
      CASE M.cType = "fr|en"
           M.cType = "fr%7Cen"
   
      CASE M.cType = "en|fr"
           M.cType = "en%7Cfr"
   
      OTHERWISE
           M.cType = "en%7Cfr"
   ENDCASE
ENDIF
   M.cPhrase = ReadURL("http://translate.google.com/translate_t?text=" + ;
                       STRTRAN(M.cPhrase, ' ', '+') + "&langpair=" + M.cType + ;
                      "&hl=fr&ie=UTF8")
   IF NOT EMPTY(M.cPhrase)
      M.cPhrase = STREXTRACT(M.cPhrase, "<textarea name=q rows=5 cols=45 wrap=PHYSICAL>", ;
                                        "</textarea>", 1, 1)
   ENDIF
           
 
RETURN M.cPhrase
 
 
 
*---------------------------------------------------------------- ReadURL
 
FUNCTION ReadURL
 
LPARAMETERS pcUrlName, lQuiet
 
DECLARE INTEGER InternetOpen IN wininet.DLL STRING sAgent, ;
        INTEGER lAccessType, STRING sProxyName, ;
        STRING sProxyBypass, INTEGER lFlags
 
DECLARE INTEGER InternetOpenUrl IN wininet.DLL ;
        INTEGER hInternetSession, STRING sUrl, STRING sHeaders, ;
        INTEGER lHeadersLength, INTEGER lFlags, INTEGER lContext
 
DECLARE INTEGER InternetReadFile IN wininet.DLL INTEGER hfile, ;
        STRING @sBuffer, INTEGER lNumberofBytesToRead, INTEGER @lBytesRead
 
DECLARE SHORT InternetCloseHandle IN wininet.DLL INTEGER hInst
 
#DEFINE INTERNET_OPEN_TYPE_PRECONFIG 0
#DEFINE INTERNET_OPEN_TYPE_DIRECT 1
#DEFINE INTERNET_OPEN_TYPE_PROXY 3
#DEFINE SYNCHRONOUS 0
#DEFINE INTERNET_FLAG_RELOAD 2147483648
 
LOCAL lsAgent, lhInternetSession, lhUrlFile, llOk, lnOk, lcRetVal,lcReadBuffer, lnBytesRead
 
* what application is using Internet services?
lsAgent = "VFP 6.0"
lcRetVal = ''
 
lhInternetSession = InternetOpen( lsAgent, INTERNET_OPEN_TYPE_PRECONFIG, ;
      '', '', SYNCHRONOUS)
 
IF lhInternetSession = 0
   IF NOT lQuiet
      MessageBox("La session Internet ne peut pas Otre Ttablie !")
   ENDIF
   RETURN lcRetVal
ENDIF
 
lhUrlFile = InternetOpenUrl( lhInternetSession, pcUrlName, '', 0, ;
                             INTERNET_FLAG_RELOAD, 0)
 
IF lhUrlFile = 0
   IF NOT lQuiet
      MessageBox("L'URL <" + pcUrlName + "> n'est pas accessible !")
   ENDIF
   RETURN lcRetVal
ENDIF
 
llOk = .t.
 
DO WHILE llOK
   * set aside a big buffer
   lsReadBuffer = SPACE(65535)
   lnBytesRead = 0
   lnOK = InternetReadFile( lhUrlFile, @lsReadBuffer, LEN(lsReadBuffer),@lnBytesRead)
 
   if ( lnBytesRead  <> 0 )
      lcRetVal = lcRetVal + left( lsReadBuffer, lnBytesRead )
   endif
 
   * error trap - either a read failure or read past eof()
   llOk = ( lnOK = 1 ) and ( lnBytesRead <> 0 )
ENDDO
 
* close all the handles we opened
InternetCloseHandle( lhUrlFile )
InternetCloseHandle( lhInternetSession )
 
* return the URL contents
RETURN lcRetVal

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top