PureBasic
PureBasic
Google Translate Text
See more Google Translate Examples
Demonstrates how to use the Cloud Translation API to translate text from one spoken language to another. The example translates from English to Spanish.Chilkat PureBasic Downloads
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"
Procedure ChilkatExample()
success.i = 0
; 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 = 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.
; {
; "q": "The quick brown fox jumped over the lazy dog.",
; "source": "en",
; "target": "es",
; "format": "text"
; }
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 = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckHttpJson(http,"POST","https://translation.googleapis.com/language/translate/v2",json,"application/json",resp)
If success = 0
Debug CkHttp::ckLastErrorText(http)
CkJsonObject::ckDispose(jsonToken)
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(json)
CkHttpResponse::ckDispose(resp)
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."
CkJsonObject::ckDispose(jsonToken)
CkHttp::ckDispose(http)
CkJsonObject::ckDispose(json)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(jResp)
ProcedureReturn
EndIf
; 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."
; }
; ]
; }
; }
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)
CkHttpResponse::ckDispose(resp)
CkJsonObject::ckDispose(jResp)
ProcedureReturn
EndProcedure