Sign in Join
Wiki copytemporary

Using Google Translate

English · Français · Português

Overview

There are at least two way to access Google Translate:

This is a usage example of the former.

For information on the latter please consult: Google Translate Getting Started

Info on the params

According to an answer on Stack Overflow (What is the meaning of Google Translate query params?) here's a list of the params to the HTTP GET call:

Here are some value for dt:

Contacting Google

This function will return the JSON response.

pascal
uses
  {...}, fpjson, fphttpclient, opensslsockets, {...}

function CallGoogleTranslate(AURL: String): TJSONStringType;
var
  client: TFPHTTPClient;
  doc: TStringList;
begin
  Result:= EmptyStr;
  doc:=TStringList.Create;
  client:=TFPHTTPClient.Create(nil);
  try
    client.Get(AURL,doc);
    Result:=doc.Text;
  finally
    doc.Free;
    client.Free;
  end;
end;

Parsing the JSON Array based response

pascal
uses
  {...}, fpjson, jsonparser, HTTPDefs, {...}

const
  cArrayShortLanguages: Array [0..7] of String = (
    'auto',
    'en',
    'pt',
    'pl',
    'fr',
    'es',
    'it',
    'ru'
  );

procedure ParseArraysTranslate;
var
  URL: String;
  Index: integer;
  strResponse: TJSONStringType;
  jdResponse, jdTranslation, jdTranslationArray: TJSONData;
  jaTranslation, jaTranslationArray: TJSONArray;
begin

  URL:='https://translate.googleapis.com/translate_a/single?client=gtx'
    +'&q='+HTTPEncode({** PUT TEXT TO TRANSLATE HERE **})
    +'&sl='+cArrayShortLanguages[0] // Auto Detect
    +'&tl='+cArrayShortLanguages[1] // English
    +'&dt=t'
    +'&ie=UTF-8&oe=UTF-8'
    ;

  strResponse:= CallGoogleTranslate(URL);
  try
    jdResponse:= GetJSON(strResponse);

    jdTranslation:= jdResponse.FindPath('[0]');
    if (jdTranslation <> nil) and (jdTranslation.JSONType = jtArray) then
    begin
      jaTranslation:= TJSONArray(jdTranslation);
      for index:= 0 to Pred(jaTranslation.Count) do
      begin
        jdTranslationArray:= jaTranslation[Index];
        if (jdTranslationArray <> nil) and (jdTranslationArray.JSONType = jtArray) then
        begin
          jaTranslationArray:= TJSONArray(jdTranslationArray);
          WriteLN(Trim(jaTranslationArray[0].AsString));
        end;
      end;
    end;
  finally
    jdResponse.Free;
  end;
end;

Parsing the JSON Object based response

pascal
uses
  {...}, fpjson, jsonparser, HTTPDefs, {...}

const
  cArrayShortLanguages: Array [0..7] of String = (
    'auto',
    'en',
    'pt',
    'pl',
    'fr',
    'es',
    'it',
    'ru'
  );
  cJSONSentences = 'sentences';
  cJSONTranslation = 'trans';
  cJSONSource = 'src';

procedure ParseObjectTranslate;
var
  URL: String;
  Index: integer;
  strResponse: TJSONStringType;
  jdResponse: TJSONData;
  joTranslation, joSentence: TJSONObject;
  jaSentencesArray: TJSONArray;
begin
  Application.ProcessMessages;
    URL:='https://translate.googleapis.com/translate_a/single?client=gtx'
      +'&q='+HTTPEncode({** PUT TEXT TO TRANSLATE HERE **})
      +'&sl='+cArrayShortLanguages[0] // Auto Detect
      +'&tl='+cArrayShortLanguages[1] // English
      +'&dt=t&dj=1' // dj=1 makes the response be a JSON Object
      +'&ie=UTF-8&oe=UTF-8'
      ;

    strResponse:= CallGoogleTranslate(URL);
    try
      jdResponse:= GetJSON(strResponse);

      if (jdResponse <> nil) and (jdResponse.JSONType = jtObject) then
      begin
        joTranslation:= TJSONObject(jdResponse);
        jaSentencesArray:= TJSONArray(joTranslation.FindPath(cJSONSentences));
        for Index:=0 to Pred(jaSentencesArray.Count) do
        begin
          joSentence:= TJSONObject(jaSentencesArray[Index]);
          WriteLN(Trim(joSentence.Get(cJSONTranslation,'')));
        end;
      end;
    finally
      jdResponse.Free;
    end;
end;

External Links

Test Google Translate - A GitHub repository which demonstrates this example.


From the Free Pascal wiki, page "Using Google Translate", as it stood on 2021-12-16 after a change by Gcarreno, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.