INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

API Functions

How to use the Google Translator with API calls by mgagnon
Posted: 11 Aug 04

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


Back to Microsoft: Visual FoxPro FAQ Index
Back to Microsoft: Visual FoxPro Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close