uses MsXML;
{函数}
function Translate(AAppID: string; AText: string; InLanguage: string=’en’; OutLanguage: string=’zh-CHS’): string;
const
BaseUrl = ‘http://api.microsofttranslator.com/V2/http.svc/Translate?appId=%s&text=%s&from=%s&to=%s’;
var
Url: string;
req: IXMLHTTPRequest;
begin
Url := Format(BaseUrl, [AAppID, AText, InLanguage, OutLanguage]);
req := CoXMLHTTP.Create;
req.open(‘Get’, Url, False, EmptyParam, EmptyParam);
req.send(EmptyParam);
Result := req.responseText;
Result := Copy(Result, 68+1, Length(Result)-68-9); //去掉前后的标签
end;
{调用测试}
procedure TForm1.Button1Click(Sender: TObject);
const
myAppId = ’65FCA293BDB85C98D16A567C3FECE22272B6****’; //这是我申请的 AppID, 隐藏了后四位
begin
Memo2.Text := Translate(myAppId, Memo1.Text);
end;
效果图:
使用 Indy:
uses IdHTTP;
function Translate2(AAppID: string; AText: string; InLanguage: string=’en’; OutLanguage: string=’zh-CHS’): string;
const
BaseUrl = ‘http://api.microsofttranslator.com/V2/http.svc/Translate?appId=%s&text=%s&from=%s&to=%s’;
var
Url: string;
stream: TStringStream;
idHttpObj: TIdHTTP;
begin
stream := TStringStream.Create;
idHttpObj := TIdHTTP.Create(nil);
Url := Format(BaseUrl, [AAppID, Trim(AText), InLanguage, OutLanguage]);
idHttpObj.Get(Url, stream);
Result := stream.DataString;
Result := Copy(Result, 68+1, Length(Result)-68-9); //去掉前后的标签
idHttpObj.Free;
stream.Free;
end;
如无特别说明,本博客文章皆为原创。转载请说明,来自吵吵博客。
原文链接:http://chaochaoblog.com/archives/955
吵吵微信朋友圈,请付款实名加入:
好久没来,还是看不懂,路过……
好久不见!
常来关注贵博客
欢迎!欢迎!
老朽看不懂,来支持博主啦。
支持一下!!!!
帮主是做程序的吗
帮主?什么帮主?丐帮?业余玩玩!
收藏了,谢谢
一看就是程序高手,仰望博主。
只能仰望 看的不是很懂