Mantaining a Delphi 2007 (server) software now I'm at the point of having to add a call to Google Translation API to translate some english text to italian. My first question is : which is the more recent version of Indy I should use with D2007 ?
Mantaining a Delphi 2007 (server) software now I'm at the point of having to add a call to Google Translation API to translate some english text to italian. My first question is : which is the more recent version of Indy I should use with D2007 ?
Because, till now, Indy gives me back a lot of wrong characters (? & = . #) in the JSON REST response, mixed into the italian text, and I fear this has something to do with D2007 and Indy version limits with Unicode strings. If I use the RESTDebugger or a browser to do the same call the result with translated text is much better (perfect using a browser, almost perfect with the RESTDebugger - strangely a ' isnt correctly escaped). I've googled a lot today, and read many tips, mostly from Remy, but I haven't found any real solution.
Because, till now, Indy gives me back a lot of wrong characters (? & = . #) in the JSON REST response, mixed into the italian text, and I fear this has something to do with D2007 and Indy version limits with Unicode strings. If I use the RESTDebugger or a browser to do the same call the result with translated text is much better (perfect using a browser, almost perfect with the RESTDebugger - strangely a ' isnt correctly escaped). I've googled a lot today, and read many tips, mostly from Remy, but I haven't found any real solution.
Use UTF8 encoding where possible? Use TBytesStream and/or TMemoryStream where possible.
ReplyDeleteThanks Eli! Indeed, I found one Remy's SO answer suggesting the use of this code with pre-2009 Delphi versions:
ReplyDeletevar
LEncoding, LAnsiEncoding: IIdTextEncoding;
...
LEncoding := IndyTextEncoding_UTF8;
LAnsiEncoding := CharsetToEncoding('ISO-8859-1'); // or 'KOI8-R', etc
...
ARequestInfo.Params.Add(TIdURI.URLDecode(s, LEncoding, LAnsiEncoding));
After having upgraded Indy to latest (2018) version from my very old one now I was able to use Remy's code and the situation is slightly better , having added a call to URLDecode to the translated italian text. But still I have some wrong chars into the text: maybe I have to add same URLDecode call to english text before add it as POST parameter ?
Ok, problem fixed: it was a matter of characters like & not escaped in english text passed as parameter calling Google APIs (only a few, random?, accented chars still not correctly visualized on the translated text) . I confess that I never upgraded Indy on my old D2007 VM , fearing huge headaches with my , old too, not-unicode codebase: installation OK and a sensation of a solid library with Indy 10.6.2 installed.
ReplyDelete