Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(DataFlex) Google Translate TextSee more Google Translate ExamplesDemonstrates how to use the Cloud Translation API to translate text from one spoken language to another. The example translates from English to Spanish.
Use ChilkatAx-win32.pkg Procedure Test Variant vJson Token Handle hoJsonToken Boolean iSuccess Handle hoHttp Variant vJson Handle hoJson Variant vResp Handle hoResp Variant vJResp Handle hoJResp Integer iRespStatusCode String sTranslatedText Integer i Integer n String sTemp1 Boolean bTemp1 // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // -------------------------------------------------------------------------------- // IMPORTANT: // Don't forget you need to 1st enable the Cloud Translation API in your Google Developers Console at https://console.cloud.google.com // -------------------------------------------------------------------------------- // It is assumed we previously obtained an OAuth2 access token. // This example loads the JSON access token file Get Create (RefClass(cComChilkatJsonObject)) To hoJsonToken If (Not(IsComObjectCreated(hoJsonToken))) Begin Send CreateComObject of hoJsonToken End Get ComLoadFile Of hoJsonToken "qa_data/tokens/_googleTranslate.json" To iSuccess If (iSuccess <> True) Begin Showln "Failed to load _googleTranslate.json" Procedure_Return End Get Create (RefClass(cComChilkatHttp)) To hoHttp If (Not(IsComObjectCreated(hoHttp))) Begin Send CreateComObject of hoHttp End Get ComStringOf Of hoJsonToken "access_token" To sTemp1 Set ComAuthToken Of hoHttp To sTemp1 // The following JSON is sent in the request body. // { // "q": "The quick brown fox jumped over the lazy dog.", // "source": "en", // "target": "es", // "format": "text" // } Get Create (RefClass(cComChilkatJsonObject)) To hoJson If (Not(IsComObjectCreated(hoJson))) Begin Send CreateComObject of hoJson End // The following code creates the JSON request body. Get ComUpdateString Of hoJson "q" "The quick brown fox jumped over the lazy dog." To iSuccess Get ComUpdateString Of hoJson "source" "en" To iSuccess Get ComUpdateString Of hoJson "target" "es" To iSuccess Get ComUpdateString Of hoJson "format" "text" To iSuccess Set ComEmitCompact Of hoJson To False Get ComEmit Of hoJson To sTemp1 Showln sTemp1 Get pvComObject of hoJson to vJson Get ComPostJson3 Of hoHttp "https://translation.googleapis.com/language/translate/v2" "application/json" vJson To vResp If (IsComObject(vResp)) Begin Get Create (RefClass(cComChilkatHttpResponse)) To hoResp Set pvComObject Of hoResp To vResp End Get ComLastMethodSuccess Of hoHttp To bTemp1 If (bTemp1 = False) Begin Get ComLastErrorText Of hoHttp To sTemp1 Showln sTemp1 Procedure_Return End Get Create (RefClass(cComChilkatJsonObject)) To hoJResp If (Not(IsComObjectCreated(hoJResp))) Begin Send CreateComObject of hoJResp End Get pvComObject of hoJResp to vJResp Get ComGetBodyJson Of hoResp vJResp To iSuccess Set ComEmitCompact Of hoJResp To False Showln "Response Body:" Get ComEmit Of hoJResp To sTemp1 Showln sTemp1 Get ComStatusCode Of hoResp To iRespStatusCode Showln "Response Status Code = " iRespStatusCode If (iRespStatusCode >= 400) Begin Showln "Response Header:" Get ComHeader Of hoResp To sTemp1 Showln sTemp1 Showln "Failed." Send Destroy of hoResp Procedure_Return End Send Destroy of hoResp // Sample JSON response: // (Sample code for parsing the JSON response is shown below) // { // "data": { // "translations": [ // { // "translatedText": "El zorro r�pida salt� sobre el perro perezoso." // } // ] // } // } Move 0 To i Get ComSizeOfArray Of hoJResp "data.translations" To n While (i < n) Set ComI Of hoJResp To i Get ComStringOf Of hoJResp "data.translations[i].translatedText" To sTranslatedText Showln sTranslatedText Move (i + 1) To i Loop End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.