1

I'm having a hard time converting Chinese words to English automatically. Where did I go wrong?

If I convert Filipino words to English everything is OK, but not with Chinese words. It will still result a string but not readable chinese word.

procedure TForm1.btn2Click(Sender: TObject);
var
 sTemp : AnsiString;
begin
 try

      // filipino to english  SUCCESS
      sTemp := TranslateText('maganda', 'fil', '    en');
      memo2.Lines.Add(sTemp);


      // chinese to english  FAILED
      sTemp := TranslateText('美丽', 'zh', '  en');
      memo2.Lines.Add(sTemp);

 except
    on E:Exception do
    begin
        ShowMessage(E.Message);
    end;
 end;

end;


function TranslateText(const AText, SourceLng, DestLng : AnsiString) : AnsiString;
var
   XmlDoc : OleVariant;
   Node   : OleVariant;
begin
  Result:=WinInet_HttpGet(Format('http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=%s&text=%s&from=%s&to=%s',['73C8F474CA4D1202AD60747126813B731199ECEA',AText,SourceLng,DestLng]));
  XmlDoc:= CreateOleObject('Msxml2.DOMDocument.6.0');
  try
    XmlDoc.Async := False;
    XmlDoc.LoadXML(Result);
    if (XmlDoc.parseError.errorCode <> 0) then
     raise Exception.CreateFmt('Error in Xml Data %s',[XmlDoc.parseError]);
    Node:= XmlDoc.documentElement;
    if not VarIsClear(Node) then
     Result:=XmlDoc.Text;
  finally
     XmlDoc:=Unassigned;
  end;
end;
Ago
  • 755
  • 7
  • 28
  • I don't know microsoft translator... but I am sure if you use AnsiString for Chinese, it's not going to work. You need either use utf-8 string or the Unicode String provided by Delphi... You can test s : AnsiString; s := '美丽'; ShowMessage(s); and you will see that the Chinese words became garbage... – Huy Pham Jun 06 '17 at 02:10
  • You need to encode your text correctly. ANSI isn't going to get it done. Confounded by URL encoding also. – David Heffernan Jun 06 '17 at 06:52
  • I've tested the code using XE7 and still not working. Can you recommend other way to translate chinese to english? My last resort is to manually convert the resources using google translate, 1 line of chinese sentence at a time. – Ago Jun 07 '17 at 02:00
  • It will be just the same in XE7 because you use AnsiString. Do you know about encodings and Unicode? If you don't why not? You have zero hope of success until you understand that. – David Heffernan Jun 07 '17 at 18:48

0 Answers0