私はBingのAPI、より正確には翻訳部分を使用していますが、言語の自動検出という1つのことを除いて、すべてうまく機能します。そんなことがあるものか?
誰かが見る必要がある場合に備えて、私のコードは正常に動作しています:
function HTTPEncode(const AStr: string): string;
const
NoConversion = ['A'..'Z', 'a'..'z', '*', '@', '.', '_', '-'];
var
i: integer;
begin
Result := '';
for i := 1 to Length(AStr) do
begin
if CharInSet(AStr[i],NoConversion) then
Result := Result + AStr[i]
else
Result := Result + Format('%%%.2x',[ord(AStr[i])]);
end;
end;
function GetTranslation(text, fromLang, toLang: string): string;
var
xmldoc: TXMLDocument;
inode,mnode,rnode,irnode: IXMLNode;
j: integer;
uri: string;
idhttp:TIdHttp;
begin
Result := '';
idhttp:=TIdHttp.Create(nil);
xmldoc := TXMLDocument.Create(application);
try
xmldoc.LoadFromXML(idhttp.Get('http://api.search.live.net/xml.aspx?Appid=' + AppID + '&query='+HTTPEncode(text)+
'&sources=translation'+
'&Translation.SourceLanguage=' + fromLang +
'&Translation.TargetLanguage=' + toLang));
finally
idhttp.Free;
end;
try
inode := xmldoc.ChildNodes.FindNode('SearchResponse');
if Assigned(inode) then
begin
uri := 'http://schemas.microsoft.com/LiveSearch/2008/04/XML/translation';
mnode := inode.ChildNodes.FindNode('Translation',uri);
if Assigned(mnode) then
begin
rnode := mnode.ChildNodes.FindNode('Results',uri);
if Assigned(rnode) then
begin
irnode := rnode.ChildNodes.FindNode('TranslationResult',uri);
if Assigned(irnode) then
Result := irnode.ChildNodes.FindNode('TranslatedTerm',uri).NodeValue;
end;
end;
end;
finally
xmldoc.Free;
end;
end;
begin
ShowMessage(GetTranslation('Hello!','en','de'));
end;
自動検出機能を使用してhttp://www.microsofttranslator.com/からのパケットを追跡したところ、結果は 'from=;' でした。一方、ソース言語が英語の場合は「from=en;」になります。ソース言語として「」も送信しようとしましたが、うまくいきませんでした。結果はありません。
自動検出を使用するにはどうすればよいですか?