Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.HttpResponse,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  jsonToken: TJsonObject;
  http: THttp;
  json: TJsonObject;
  resp: THttpResponse;
  jResp: TJsonObject;
  respStatusCode: Integer;
  translatedText: string;
  i: Integer;
  n: Integer;

begin
  success := False;

  //  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 := TJsonObject.Create;
  success := jsonToken.LoadFile('qa_data/tokens/_googleTranslate.json');
  if (success <> True) then
    begin
      WriteLn('Failed to load _googleTranslate.json');
      Exit;
    end;

  http := THttp.Create;

  http.AuthToken := jsonToken.StringOf('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 := TJsonObject.Create;
  //  The following code creates the JSON request body.
  json.UpdateString('q','The quick brown fox jumped over the lazy dog.');
  json.UpdateString('source','en');
  json.UpdateString('target','es');
  json.UpdateString('format','text');

  json.EmitCompact := False;
  WriteLn(json.Emit());

  resp := THttpResponse.Create;
  success := http.HttpJson('POST','https://translation.googleapis.com/language/translate/v2',json,'application/json',resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  jResp := TJsonObject.Create;
  resp.GetBodyJson(jResp);
  jResp.EmitCompact := False;

  WriteLn('Response Body:');
  WriteLn(jResp.Emit());

  respStatusCode := resp.StatusCode;
  WriteLn('Response Status Code = ' + respStatusCode);
  if (respStatusCode >= 400) then
    begin
      WriteLn('Response Header:');
      WriteLn(resp.Header);
      WriteLn('Failed.');
      Exit;
    end;

  //  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."
  //        }
  //      ]
  //    }
  //  }

  i := 0;
  n := jResp.SizeOfArray('data.translations');
  while i < n do
    begin
      jResp.I := i;
      translatedText := jResp.StringOf('data.translations[i].translatedText');
      WriteLn(translatedText);
      i := i + 1;
    end;



  jsonToken.Free;
  http.Free;
  json.Free;
  resp.Free;
  jResp.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.