• 在 Delphi 中使用微软全文翻译的小例子


    使用帮助
    需要先去申请一个 AppID: http://www.bing.com/toolbox/bingdeveloper/
    使用帮助在: http://msdn.microsoft.com/en-us/library/dd576287.aspx

    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;
  • 相关阅读:
    poj 2229 Sumsets
    HDU- 2063 过山车
    编写一个简易购物车,实现向购物车内添加商品,移除指定商品及清空购物车功能。
    编写一个实现页面计数,要求当刷新页面时,不增加计数
    4-1:编写一个简单的留言簿,写入留言提交后显示留言内容。
    HDU-3706 Second My Problem First
    HDU-1896 Stones
    4-16 表单提交信息和获取。
    HDU-1873 看病要排队
    HDU-1509 Windows Message Queue
  • 原文地址:https://www.cnblogs.com/zyb2016/p/5685339.html
Copyright © 2020-2023  润新知