pebriana

How to use the Google Translator with API calls

May 11th, 2012
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. M.cPhrase = CDOW(DATE())
  2. MessageBox(Translate(M.cPhrase))
  3.  
  4.  
  5. *** Traduction au travers de l'outil GOOGLE
  6. FUNCTION Translate
  7.  
  8. LPARAMETERS cPhrase, cType
  9.  
  10. IF NOT EMPTY(M.cPhrase)
  11.    DO CASE
  12.       CASE VARTYPE(M.cType) <> 'C'
  13.            M.cType = "en%7Cfr"
  14.  
  15.       CASE M.cType = "fr|en"
  16.            M.cType = "fr%7Cen"
  17.    
  18.       CASE M.cType = "en|fr"
  19.            M.cType = "en%7Cfr"
  20.    
  21.       OTHERWISE
  22.            M.cType = "en%7Cfr"
  23.    ENDCASE
  24. ENDIF
  25.    M.cPhrase = ReadURL("http://translate.google.com/translate_t?text=" + ;
  26.                        STRTRAN(M.cPhrase, ' ', '+') + "&langpair=" + M.cType + ;
  27.                       "&hl=fr&ie=UTF8")
  28.    IF NOT EMPTY(M.cPhrase)
  29.       M.cPhrase = STREXTRACT(M.cPhrase, "<textarea name=q rows=5 cols=45 wrap=PHYSICAL>", ;
  30.                                         "</textarea>", 1, 1)
  31.    ENDIF
  32.            
  33.  
  34. RETURN M.cPhrase
  35.  
  36.  
  37.  
  38. *---------------------------------------------------------------- ReadURL
  39.  
  40. FUNCTION ReadURL
  41.  
  42. LPARAMETERS pcUrlName, lQuiet
  43.  
  44. DECLARE INTEGER InternetOpen IN wininet.DLL STRING sAgent, ;
  45.         INTEGER lAccessType, STRING sProxyName, ;
  46.         STRING sProxyBypass, INTEGER lFlags
  47.  
  48. DECLARE INTEGER InternetOpenUrl IN wininet.DLL ;
  49.         INTEGER hInternetSession, STRING sUrl, STRING sHeaders, ;
  50.         INTEGER lHeadersLength, INTEGER lFlags, INTEGER lContext
  51.  
  52. DECLARE INTEGER InternetReadFile IN wininet.DLL INTEGER hfile, ;
  53.         STRING @sBuffer, INTEGER lNumberofBytesToRead, INTEGER @lBytesRead
  54.  
  55. DECLARE SHORT InternetCloseHandle IN wininet.DLL INTEGER hInst
  56.  
  57. #DEFINE INTERNET_OPEN_TYPE_PRECONFIG 0
  58. #DEFINE INTERNET_OPEN_TYPE_DIRECT 1
  59. #DEFINE INTERNET_OPEN_TYPE_PROXY 3
  60. #DEFINE SYNCHRONOUS 0
  61. #DEFINE INTERNET_FLAG_RELOAD 2147483648
  62.  
  63. LOCAL lsAgent, lhInternetSession, lhUrlFile, llOk, lnOk, lcRetVal,lcReadBuffer, lnBytesRead
  64.  
  65. * what application is using Internet services?
  66. lsAgent = "VFP 6.0"
  67. lcRetVal = ''
  68.  
  69. lhInternetSession = InternetOpen( lsAgent, INTERNET_OPEN_TYPE_PRECONFIG, ;
  70.       '', '', SYNCHRONOUS)
  71.  
  72. IF lhInternetSession = 0
  73.    IF NOT lQuiet
  74.       MessageBox("La session Internet ne peut pas être établie !")
  75.    ENDIF
  76.    RETURN lcRetVal
  77. ENDIF
  78.  
  79. lhUrlFile = InternetOpenUrl( lhInternetSession, pcUrlName, '', 0, ;
  80.                              INTERNET_FLAG_RELOAD, 0)
  81.  
  82. IF lhUrlFile = 0
  83.    IF NOT lQuiet
  84.       MessageBox("L'URL <" + pcUrlName + "> n'est pas accessible !")
  85.    ENDIF
  86.    RETURN lcRetVal
  87. ENDIF
  88.  
  89. llOk = .t.
  90.  
  91. DO WHILE llOK
  92.    * set aside a big buffer
  93.    lsReadBuffer = SPACE(65535)
  94.    lnBytesRead = 0
  95.    lnOK = InternetReadFile( lhUrlFile, @lsReadBuffer, LEN(lsReadBuffer),@lnBytesRead)
  96.  
  97.    if ( lnBytesRead  <> 0 )
  98.       lcRetVal = lcRetVal + left( lsReadBuffer, lnBytesRead )
  99.    endif
  100.  
  101.    * error trap - either a read failure or read past eof()
  102.    llOk = ( lnOK = 1 ) and ( lnBytesRead <> 0 )
  103. ENDDO
  104.  
  105. * close all the handles we opened
  106. InternetCloseHandle( lhUrlFile )
  107. InternetCloseHandle( lhInternetSession )
  108.  
  109. * return the URL contents
  110. RETURN lcRetV
  111. *** Traduction au travers de l'outil GOOGLE
  112. FUNCTION Translate
  113.  
  114. LPARAMETERS cPhrase, cType
  115.  
  116. IF NOT EMPTY(M.cPhrase)
  117.    DO CASE
  118.       CASE VARTYPE(M.cType) <> 'C'
  119.            M.cType = "en%7Cfr"
  120.  
  121.       CASE M.cType = "fr|en"
  122.            M.cType = "fr%7Cen"
  123.    
  124.       CASE M.cType = "en|fr"
  125.            M.cType = "en%7Cfr"
  126.    
  127.       OTHERWISE
  128.            M.cType = "en%7Cfr"
  129.    ENDCASE
  130. ENDIF
  131.    M.cPhrase = ReadURL("http://translate.google.com/translate_t?text=" + ;
  132.                        STRTRAN(M.cPhrase, ' ', '+') + "&langpair=" + M.cType + ;
  133.                       "&hl=fr&ie=UTF8")
  134.    IF NOT EMPTY(M.cPhrase)
  135.       M.cPhrase = STREXTRACT(M.cPhrase, "<textarea name=q rows=5 cols=45 wrap=PHYSICAL>", ;
  136.                                         "</textarea>", 1, 1)
  137.    ENDIF
  138.            
  139.  
  140. RETURN M.cPhrase
  141.  
  142.  
  143.  
  144. *---------------------------------------------------------------- ReadURL
  145.  
  146. FUNCTION ReadURL
  147.  
  148. LPARAMETERS pcUrlName, lQuiet
  149.  
  150. DECLARE INTEGER InternetOpen IN wininet.DLL STRING sAgent, ;
  151.         INTEGER lAccessType, STRING sProxyName, ;
  152.         STRING sProxyBypass, INTEGER lFlags
  153.  
  154. DECLARE INTEGER InternetOpenUrl IN wininet.DLL ;
  155.         INTEGER hInternetSession, STRING sUrl, STRING sHeaders, ;
  156.         INTEGER lHeadersLength, INTEGER lFlags, INTEGER lContext
  157.  
  158. DECLARE INTEGER InternetReadFile IN wininet.DLL INTEGER hfile, ;
  159.         STRING @sBuffer, INTEGER lNumberofBytesToRead, INTEGER @lBytesRead
  160.  
  161. DECLARE SHORT InternetCloseHandle IN wininet.DLL INTEGER hInst
  162.  
  163. #DEFINE INTERNET_OPEN_TYPE_PRECONFIG 0
  164. #DEFINE INTERNET_OPEN_TYPE_DIRECT 1
  165. #DEFINE INTERNET_OPEN_TYPE_PROXY 3
  166. #DEFINE SYNCHRONOUS 0
  167. #DEFINE INTERNET_FLAG_RELOAD 2147483648
  168.  
  169. LOCAL lsAgent, lhInternetSession, lhUrlFile, llOk, lnOk, lcRetVal,lcReadBuffer, lnBytesRead
  170.  
  171. * what application is using Internet services?
  172. lsAgent = "VFP 6.0"
  173. lcRetVal = ''
  174.  
  175. lhInternetSession = InternetOpen( lsAgent, INTERNET_OPEN_TYPE_PRECONFIG, ;
  176.       '', '', SYNCHRONOUS)
  177.  
  178. IF lhInternetSession = 0
  179.    IF NOT lQuiet
  180.       MessageBox("La session Internet ne peut pas être établie !")
  181.    ENDIF
  182.    RETURN lcRetVal
  183. ENDIF
  184.  
  185. lhUrlFile = InternetOpenUrl( lhInternetSession, pcUrlName, '', 0, ;
  186.                              INTERNET_FLAG_RELOAD, 0)
  187.  
  188. IF lhUrlFile = 0
  189.    IF NOT lQuiet
  190.       MessageBox("L'URL <" + pcUrlName + "> n'est pas accessible !")
  191.    ENDIF
  192.    RETURN lcRetVal
  193. ENDIF
  194.  
  195. llOk = .t.
  196.  
  197. DO WHILE llOK
  198.    * set aside a big buffer
  199.    lsReadBuffer = SPACE(65535)
  200.    lnBytesRead = 0
  201.    lnOK = InternetReadFile( lhUrlFile, @lsReadBuffer, LEN(lsReadBuffer),@lnBytesRead)
  202.  
  203.    if ( lnBytesRead  <> 0 )
  204.       lcRetVal = lcRetVal + left( lsReadBuffer, lnBytesRead )
  205.    endif
  206.  
  207.    * error trap - either a read failure or read past eof()
  208.    llOk = ( lnOK = 1 ) and ( lnBytesRead <> 0 )
  209. ENDDO
  210.  
  211. * close all the handles we opened
  212. InternetCloseHandle( lhUrlFile )
  213. InternetCloseHandle( lhInternetSession )
  214.  
  215. * return the URL contents
  216. RETURN lcRetV
  217. *---------------------------------------------------------------- ReadURL
  218.  
  219. FUNCTION ReadURL
  220.  
  221. LPARAMETERS pcUrlName, lQuiet
  222.  
  223. DECLARE INTEGER InternetOpen IN wininet.DLL STRING sAgent, ;
  224.         INTEGER lAccessType, STRING sProxyName, ;
  225.         STRING sProxyBypass, INTEGER lFlags
  226.  
  227. DECLARE INTEGER InternetOpenUrl IN wininet.DLL ;
  228.         INTEGER hInternetSession, STRING sUrl, STRING sHeaders, ;
  229.         INTEGER lHeadersLength, INTEGER lFlags, INTEGER lContext
  230.  
  231. DECLARE INTEGER InternetReadFile IN wininet.DLL INTEGER hfile, ;
  232.         STRING @sBuffer, INTEGER lNumberofBytesToRead, INTEGER @lBytesRead
  233.  
  234. DECLARE SHORT InternetCloseHandle IN wininet.DLL INTEGER hInst
  235.  
  236. #DEFINE INTERNET_OPEN_TYPE_PRECONFIG 0
  237. #DEFINE INTERNET_OPEN_TYPE_DIRECT 1
  238. #DEFINE INTERNET_OPEN_TYPE_PROXY 3
  239. #DEFINE SYNCHRONOUS 0
  240. #DEFINE INTERNET_FLAG_RELOAD 2147483648
  241.  
  242. LOCAL lsAgent, lhInternetSession, lhUrlFile, llOk, lnOk, lcRetVal,lcReadBuffer, lnBytesRead
  243.  
  244. * what application is using Internet services?
  245. lsAgent = "VFP 6.0"
  246. lcRetVal = ''
  247.  
  248. lhInternetSession = InternetOpen( lsAgent, INTERNET_OPEN_TYPE_PRECONFIG, ;
  249.       '', '', SYNCHRONOUS)
  250.  
  251. IF lhInternetSession = 0
  252.    IF NOT lQuiet
  253.       MessageBox("La session Internet ne peut pas être établie !")
  254.    ENDIF
  255.    RETURN lcRetVal
  256. ENDIF
  257.  
  258. lhUrlFile = InternetOpenUrl( lhInternetSession, pcUrlName, '', 0, ;
  259.                              INTERNET_FLAG_RELOAD, 0)
  260.  
  261. IF lhUrlFile = 0
  262.    IF NOT lQuiet
  263.       MessageBox("L'URL <" + pcUrlName + "> n'est pas accessible !")
  264.    ENDIF
  265.    RETURN lcRetVal
  266. ENDIF
  267.  
  268. llOk = .t.
  269.  
  270. DO WHILE llOK
  271.    * set aside a big buffer
  272.    lsReadBuffer = SPACE(65535)
  273.    lnBytesRead = 0
  274.    lnOK = InternetReadFile( lhUrlFile, @lsReadBuffer, LEN(lsReadBuffer),@lnBytesRead)
  275.  
  276.    if ( lnBytesRead  <> 0 )
  277.       lcRetVal = lcRetVal + left( lsReadBuffer, lnBytesRead )
  278.    endif
  279.  
  280.    * error trap - either a read failure or read past eof()
  281.    llOk = ( lnOK = 1 ) and ( lnBytesRead <> 0 )
  282. ENDDO
  283.  
  284. * close all the handles we opened
  285. InternetCloseHandle( lhUrlFile )
  286. InternetCloseHandle( lhInternetSession )
  287.  
  288. * return the URL contents
  289. RETURN lcRetV
  290. * what application is using Internet services?
  291. lsAgent = "VFP 6.0"
  292. lcRetVal = ''
  293.  
  294. lhInternetSession = InternetOpen( lsAgent, INTERNET_OPEN_TYPE_PRECONFIG, ;
  295.       '', '', SYNCHRONOUS)
  296.  
  297. IF lhInternetSession = 0
  298.    IF NOT lQuiet
  299.       MessageBox("La session Internet ne peut pas être établie !")
  300.    ENDIF
  301.    RETURN lcRetVal
  302. ENDIF
  303.  
  304. lhUrlFile = InternetOpenUrl( lhInternetSession, pcUrlName, '', 0, ;
  305.                              INTERNET_FLAG_RELOAD, 0)
  306.  
  307. IF lhUrlFile = 0
  308.    IF NOT lQuiet
  309.       MessageBox("L'URL <" + pcUrlName + "> n'est pas accessible !")
  310.    ENDIF
  311.    RETURN lcRetVal
  312. ENDIF
  313.  
  314. llOk = .t.
  315.  
  316. DO WHILE llOK
  317.    * set aside a big buffer
  318.    lsReadBuffer = SPACE(65535)
  319.    lnBytesRead = 0
  320.    lnOK = InternetReadFile( lhUrlFile, @lsReadBuffer, LEN(lsReadBuffer),@lnBytesRead)
  321.  
  322.    if ( lnBytesRead  <> 0 )
  323.       lcRetVal = lcRetVal + left( lsReadBuffer, lnBytesRead )
  324.    endif
  325.  
  326.    * error trap - either a read failure or read past eof()
  327.    llOk = ( lnOK = 1 ) and ( lnBytesRead <> 0 )
  328. ENDDO
  329.  
  330. * close all the handles we opened
  331. InternetCloseHandle( lhUrlFile )
  332. InternetCloseHandle( lhInternetSession )
  333.  
  334. * return the URL contents
  335. RETURN lcRetV
  336. * close all the handles we opened
  337. InternetCloseHandle( lhUrlFile )
  338. InternetCloseHandle( lhInternetSession )
  339.  
  340. * return the URL contents
  341. RETURN lcRetV
  342. * return the URL contents
  343. RETURN lcRetVal
Add Comment
Please, Sign In to add comment