Forum > Networking and Web Programming

LibreTranslate

(1/1)

SymbolicFrank:
Ok, I just spend a few hours trying to communicate with this. It seems easy enough:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TMainForm.Translate(s: string): string;var  client: TFPHttpClient;  response: TStringStream;  url, r: string;begin  client := TFPHttpClient.Create(nil);  response := TStringStream.Create('');  url := 'https://libretranslate.com/translate';  r := '{q: "' + s +    '", source: "nl", target: "en", format: "text", api_key: ""}';  client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');  client.AddHeader('Content-Type', 'application/json');  client.AddHeader('Accept', 'application/json');  client.AllowRedirect := true;  client.RequestBody := TRawByteStringStream.Create(r);  client.Post(url, response);  Result := response.DataString;  client.Free;  response.Free;end;
Ok, I use:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
Have a look here, did help me:
https://wiki.freepascal.org/fphttpclient
Look for this: program dl_fphttp_d;
"it demonstrates almost everything" 

PierceNg:

--- Quote from: SymbolicFrank 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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TMainForm.Translate(s: string): string;  r := '{q: "' + s +    '", 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."}'

--- End quote ---

Try double quoting the JSON keys.

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


--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---'{"q":"","source":"auto","target":"en","format":"text","api_key":""}'

SymbolicFrank:

--- Quote from: PierceNg on January 25, 2023, 07:10:23 am ---When I do JSON.stringify in Javascript and print it out, the keys are quoted:


--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---'{"q":"","source":"auto","target":"en","format":"text","api_key":""}'
--- End quote ---

Thanks, that was it! Obvious, in hindsight.

wp:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  url := 'https://translate.argosopentech.com/translate';

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses  ..., fphttpclient, opensslsockets, fpjson, jsonparser;  function Translate(AText, SrcLang, DestLang: string): string;var  client: TFPHttpClient;  request: TRawByteStringStream;  response: TMemoryStream;  url, r: string;  json: TJSONData;begin  Result := '';   url := 'https://translate.argosopentech.com/translate';  r := Format('{"q":"%s", "source":"%s", "target":"%s", "format":"text"}', [    AText, SrcLang, DestLang  ]);   client := TFPHttpClient.Create(nil);  try    client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');    client.AddHeader('Content-Type', 'application/json');    client.AddHeader('Accept', 'application/json');    client.AllowRedirect := true;    request := TRawByteStringStream.Create(r);    response := TMemoryStream.Create;    try      client.RequestBody := request;      client.Post(url, response);      response.Position := 0;      json := GetJSON(response);      try        if json <> nil then          Result := json.FindPath('translatedText').AsString;      finally        json.Free;      end;    finally      response.Free;      request.Free;    end;  finally    client.Free;  end;end;

Navigation

[0] Message Index

Go to full version