Recent

Author Topic: google translate  (Read 553 times)

ravkepar

  • New member
  • *
  • Posts: 9
google translate
« on: November 27, 2025, 11:14:19 pm »
I made an subtitle tool, but i want to add a translate module in the program to translate subtitles - that are inside a stringgrid - between several langages.
I think the easiest way is using google translate, but it's hard to know how to do this.
Has anybody got an idea how to do this?

paweld

  • Hero Member
  • *****
  • Posts: 1560
Best regards / Pozdrawiam
paweld

ravkepar

  • New member
  • *
  • Posts: 9
Re: google translate
« Reply #2 on: November 29, 2025, 04:41:52 pm »
Tx. This should be standard in Lazarus. ;)

Thaddy

  • Hero Member
  • *****
  • Posts: 18676
  • Jungle wars. And failing health it seems.
Re: google translate
« Reply #3 on: November 29, 2025, 04:59:01 pm »
The google api's ARE standard in Freepascal.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

jcmontherock

  • Sr. Member
  • ****
  • Posts: 335
Re: google translate
« Reply #4 on: November 29, 2025, 05:13:21 pm »
I am using this procedure, translating one StringGrid cell to another one. Language from and to are in a combobox: cbboxArrayFrom and cboxArrayTo:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnAskTranslateClick(Sender: TObject);
  2. var
  3.   URL:          String;
  4.   Index:        integer;
  5.   strResponse:  TJSONStringType;
  6.   jdResponse,   jdTranslation, jdTranslationArray: TJSONData;
  7.   jaTranslation, jaTranslationArray: TJSONArray;
  8.   sTextFrom:  String;
  9.   sTextTo:    String;
  10. begin
  11.   sTextFrom := '';
  12.   sTextFrom := StringGrid1.Cells[_OrigText, StringGrid1.Row];
  13.   Application.ProcessMessages;
  14.   try
  15.     if Length(sTextFrom) = 0 then
  16.     begin
  17.       Memo1.Append('Need something to translate');
  18.       exit;
  19.     end;
  20.  
  21.     if (cboxArrayTo.ItemIndex <= 0) and (cboxArrayFrom.ItemIndex <= 0) then
  22.     begin
  23.       Memo1.Append('Cannot have language detection on both sides');
  24.       exit;
  25.     end;
  26.  
  27.     URL:='https://translate.googleapis.com/translate_a/single?client=gtx'
  28.       +'&q='+HTTPEncode(sTextFrom)
  29.       +'&sl='+cArrayShortLanguages[cboxArrayFrom.ItemIndex]
  30.       +'&tl='+cArrayShortLanguages[cboxArrayTo.ItemIndex]
  31.       +'&dt=t'
  32.       +'&ie=UTF-8&oe=UTF-8'
  33.       ;
  34.     //Memo1.Append(URL);
  35.     strResponse:= CallGoogleTranslate(URL);
  36.     try
  37.       jdResponse:= GetJSON(strResponse);
  38.       //memArraysTo.Append(jdResponse.FormatJSON); exit;
  39.       jdTranslation:= jdResponse.FindPath('[0]');
  40.       if (jdTranslation <> nil) and (jdTranslation.JSONType = jtArray) then
  41.       begin
  42.         jaTranslation:= TJSONArray(jdTranslation);
  43.         for index:= 0 to Pred(jaTranslation.Count) do
  44.         begin
  45.           jdTranslationArray:= jaTranslation[Index];
  46.           if (jdTranslationArray <> nil) and (jdTranslationArray.JSONType = jtArray) then
  47.           begin
  48.             jaTranslationArray:= TJSONArray(jdTranslationArray);
  49.             StringGrid1.BeginUpdate;
  50.             StringGrid1.Cells[_TRANSLATION, StringGrid1.Row] := Trim(jaTranslationArray[0].AsString);
  51.             sTextTo := StringGrid1.Cells[_TRANSLATION, StringGrid1.Row];
  52.             StringGrid1ValidateEntry(Sender, _TRANSLATION, StringGrid1.Row, '', sTextTo);
  53.             StringGrid1.EndUpdate;
  54.           end;
  55.         end;
  56.       end;
  57.     finally
  58.       jdResponse.Free;
  59.     end;
  60.   finally
  61.     Application.ProcessMessages;
  62.   end;
  63. end;
  64.  
Windows 11 UTF8-64 - Lazarus 4.4-64 - FPC 3.2.2

 

TinyPortal © 2005-2018