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
(Unicode C) 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.
#include <C_CkJsonObjectW.h> #include <C_CkHttpW.h> #include <C_CkHttpResponseW.h> void ChilkatSample(void) { HCkJsonObjectW jsonToken; BOOL success; HCkHttpW http; HCkJsonObjectW json; HCkHttpResponseW resp; HCkJsonObjectW jResp; int respStatusCode; const wchar_t *translatedText; int i; int n; // 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 = CkJsonObjectW_Create(); success = CkJsonObjectW_LoadFile(jsonToken,L"qa_data/tokens/_googleTranslate.json"); if (success != TRUE) { wprintf(L"Failed to load _googleTranslate.json\n"); CkJsonObjectW_Dispose(jsonToken); return; } http = CkHttpW_Create(); CkHttpW_putAuthToken(http,CkJsonObjectW_stringOf(jsonToken,L"access_token")); // The following JSON is sent in the request body. json = CkJsonObjectW_Create(); // The following code creates the JSON request body. CkJsonObjectW_UpdateString(json,L"q",L"The quick brown fox jumped over the lazy dog."); CkJsonObjectW_UpdateString(json,L"source",L"en"); CkJsonObjectW_UpdateString(json,L"target",L"es"); CkJsonObjectW_UpdateString(json,L"format",L"text"); CkJsonObjectW_putEmitCompact(json,FALSE); wprintf(L"%s\n",CkJsonObjectW_emit(json)); resp = CkHttpW_PostJson3(http,L"https://translation.googleapis.com/language/translate/v2",L"application/json",json); if (CkHttpW_getLastMethodSuccess(http) == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkJsonObjectW_Dispose(jsonToken); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); return; } jResp = CkJsonObjectW_Create(); CkHttpResponseW_GetBodyJson(resp,jResp); CkJsonObjectW_putEmitCompact(jResp,FALSE); wprintf(L"Response Body:\n"); wprintf(L"%s\n",CkJsonObjectW_emit(jResp)); respStatusCode = CkHttpResponseW_getStatusCode(resp); wprintf(L"Response Status Code = %d\n",respStatusCode); if (respStatusCode >= 400) { wprintf(L"Response Header:\n"); wprintf(L"%s\n",CkHttpResponseW_header(resp)); wprintf(L"Failed.\n"); CkHttpResponseW_Dispose(resp); CkJsonObjectW_Dispose(jsonToken); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkJsonObjectW_Dispose(jResp); return; } CkHttpResponseW_Dispose(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." // } // ] // } // } i = 0; n = CkJsonObjectW_SizeOfArray(jResp,L"data.translations"); while (i < n) { CkJsonObjectW_putI(jResp,i); translatedText = CkJsonObjectW_stringOf(jResp,L"data.translations[i].translatedText"); wprintf(L"%s\n",translatedText); i = i + 1; } CkJsonObjectW_Dispose(jsonToken); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkJsonObjectW_Dispose(jResp); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.