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
(PureBasic) 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.
IncludeFile "CkHttpResponse.pb" IncludeFile "CkHttp.pb" IncludeFile "CkJsonObject.pb" Procedure ChilkatExample() ; 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 jsonToken.i = CkJsonObject::ckCreate() If jsonToken.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i = CkJsonObject::ckLoadFile(jsonToken,"qa_data/tokens/_googleTranslate.json") If success <> 1 Debug "Failed to load _googleTranslate.json" CkJsonObject::ckDispose(jsonToken) ProcedureReturn EndIf http.i = CkHttp::ckCreate() If http.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkHttp::setCkAuthToken(http, CkJsonObject::ckStringOf(jsonToken,"access_token")) ; The following JSON is sent in the request body. json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; The following code creates the JSON request body. CkJsonObject::ckUpdateString(json,"q","The quick brown fox jumped over the lazy dog.") CkJsonObject::ckUpdateString(json,"source","en") CkJsonObject::ckUpdateString(json,"target","es") CkJsonObject::ckUpdateString(json,"format","text") CkJsonObject::setCkEmitCompact(json, 0) Debug CkJsonObject::ckEmit(json) resp.i = CkHttp::ckPostJson3(http,"https://translation.googleapis.com/language/translate/v2","application/json",json) If CkHttp::ckLastMethodSuccess(http) = 0 Debug CkHttp::ckLastErrorText(http) CkJsonObject::ckDispose(jsonToken) CkHttp::ckDispose(http) CkJsonObject::ckDispose(json) ProcedureReturn EndIf jResp.i = CkJsonObject::ckCreate() If jResp.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkHttpResponse::ckGetBodyJson(resp,jResp) CkJsonObject::setCkEmitCompact(jResp, 0) Debug "Response Body:" Debug CkJsonObject::ckEmit(jResp) respStatusCode.i = CkHttpResponse::ckStatusCode(resp) Debug "Response Status Code = " + Str(respStatusCode) If respStatusCode >= 400 Debug "Response Header:" Debug CkHttpResponse::ckHeader(resp) Debug "Failed." CkHttpResponse::ckDispose(resp) CkJsonObject::ckDispose(jsonToken) CkHttp::ckDispose(http) CkJsonObject::ckDispose(json) CkJsonObject::ckDispose(jResp) ProcedureReturn EndIf CkHttpResponse::ckDispose(resp) ; Sample JSON response: ; (Sample code for parsing the JSON response is shown below) ; { ; "data": { ; "translations": [ ; { ; "translatedText": "El zorro rpida salt sobre el perro perezoso." ; } ; ] ; } ; } translatedText.s i.i = 0 n.i = CkJsonObject::ckSizeOfArray(jResp,"data.translations") While i < n CkJsonObject::setCkI(jResp, i) translatedText = CkJsonObject::ckStringOf(jResp,"data.translations[i].translatedText") Debug translatedText i = i + 1 Wend CkJsonObject::ckDispose(jsonToken) CkHttp::ckDispose(http) CkJsonObject::ckDispose(json) CkJsonObject::ckDispose(jResp) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.