Recent

Author Topic: LibreTranslate  (Read 429 times)

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1147
LibreTranslate
« on: January 24, 2023, 03:51:37 pm »
Ok, I just spend a few hours trying to communicate with this. It seems easy enough:

Code: Pascal  [Select][+][-]
  1. function TMainForm.Translate(s: string): string;
  2. var
  3.   client: TFPHttpClient;
  4.   response: TStringStream;
  5.   url, r: string;
  6. begin
  7.   client := TFPHttpClient.Create(nil);
  8.   response := TStringStream.Create('');
  9.   url := 'https://libretranslate.com/translate';
  10.   r := '{q: "' + s +
  11.     '", source: "nl", target: "en", format: "text", api_key: ""}';
  12.   client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');
  13.   client.AddHeader('Content-Type', 'application/json');
  14.   client.AddHeader('Accept', 'application/json');
  15.   client.AllowRedirect := true;
  16.   client.RequestBody := TRawByteStringStream.Create(r);
  17.   client.Post(url, response);
  18.   Result := response.DataString;
  19.   client.Free;
  20.   response.Free;
  21. end;

Ok, I use:
Code: Pascal  [Select][+][-]
  1. url := 'http://localhost:5000/translate';

I tried many variations, but none of them work. I always get a 400: BAD REQUEST; '{"error":"The browser (or proxy) sent a request that this server could not understand."}'

It is probably really easy, but I don't get it.

arneolav

  • Full Member
  • ***
  • Posts: 163
    • ElTranslador
Re: LibreTranslate
« Reply #1 on: January 24, 2023, 03:54:01 pm »
Have a look here, did help me:
https://wiki.freepascal.org/fphttpclient
Look for this: program dl_fphttp_d;
"it demonstrates almost everything" 
« Last Edit: January 24, 2023, 03:56:53 pm by arneolav »
Win XP, Win7, Win 10, Win 11, win64 , Lazarus 2.2.4
Delphi/DevExpress

PierceNg

  • Sr. Member
  • ****
  • Posts: 292
    • SamadhiWeb
Re: LibreTranslate
« Reply #2 on: January 25, 2023, 07:10:23 am »
Ok, I just spend a few hours trying to communicate with this. It seems easy enough:

Code: Pascal  [Select][+][-]
  1. function TMainForm.Translate(s: string): string;
  2.   r := '{q: "' + s +
  3.     '", source: "nl", target: "en", format: "text", api_key: ""}';

I tried many variations, but none of them work. I always get a 400: BAD REQUEST; '{"error":"The browser (or proxy) sent a request that this server could not understand."}'

Try double quoting the JSON keys.

When I do JSON.stringify in Javascript and print it out, the keys are quoted:

Code: Text  [Select][+][-]
  1. '{"q":"","source":"auto","target":"en","format":"text","api_key":""}'

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1147
Re: LibreTranslate
« Reply #3 on: January 25, 2023, 08:54:45 am »
When I do JSON.stringify in Javascript and print it out, the keys are quoted:

Code: Text  [Select][+][-]
  1. '{"q":"","source":"auto","target":"en","format":"text","api_key":""}'

Thanks, that was it! Obvious, in hindsight.
« Last Edit: January 26, 2023, 02:43:18 pm by SymbolicFrank »

wp

  • Hero Member
  • *****
  • Posts: 10489
Re: LibreTranslate
« Reply #4 on: January 25, 2023, 10:16:47 am »
It still is not working for me: "The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application."

And there is a memory leak because a stream is created for client.RequestBody which is not destroyed.

[EDIT]
It is working when I move to a different url (https://github.com/argosopentech/argos-translate):
Code: Pascal  [Select][+][-]
  1.   url := 'https://translate.argosopentech.com/translate';

Code: Pascal  [Select][+][-]
  1. uses
  2.   ..., fphttpclient, opensslsockets, fpjson, jsonparser;
  3.  
  4. function Translate(AText, SrcLang, DestLang: string): string;
  5. var
  6.   client: TFPHttpClient;
  7.   request: TRawByteStringStream;
  8.   response: TMemoryStream;
  9.   url, r: string;
  10.   json: TJSONData;
  11. begin
  12.   Result := '';
  13.  
  14.   url := 'https://translate.argosopentech.com/translate';
  15.   r := Format('{"q":"%s", "source":"%s", "target":"%s", "format":"text"}', [
  16.     AText, SrcLang, DestLang
  17.   ]);
  18.  
  19.   client := TFPHttpClient.Create(nil);
  20.   try
  21.     client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');
  22.     client.AddHeader('Content-Type', 'application/json');
  23.     client.AddHeader('Accept', 'application/json');
  24.     client.AllowRedirect := true;
  25.     request := TRawByteStringStream.Create(r);
  26.     response := TMemoryStream.Create;
  27.     try
  28.       client.RequestBody := request;
  29.       client.Post(url, response);
  30.       response.Position := 0;
  31.       json := GetJSON(response);
  32.       try
  33.         if json <> nil then
  34.           Result := json.FindPath('translatedText').AsString;
  35.       finally
  36.         json.Free;
  37.       end;
  38.     finally
  39.       response.Free;
  40.       request.Free;
  41.     end;
  42.   finally
  43.     client.Free;
  44.   end;
  45. end;
« Last Edit: January 25, 2023, 11:53:04 am by wp »

 

TinyPortal © 2005-2018