當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JSON 之 SuperObject(17): 实例 - 借用 Google 实现全文翻译
生活随笔
收集整理的這篇文章主要介紹了
JSON 之 SuperObject(17): 实例 - 借用 Google 实现全文翻译
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
調(diào)用 Google 翻譯的地址格式:
http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + 原始文本 + "&langpair=" + 原語言 + "%7C" + 目標(biāo)語言
返回的數(shù)據(jù)格式如下, 可以用 responseData.translatedText 簡(jiǎn)單獲取:
{"responseData" : {"translatedText" : "返回的文本"}, "responseDetails" : null, "responseStatus" : 200 }
本例效果圖:
代碼文件:
unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 = class(TForm)Memo1: TMemo;Memo2: TMemo;Button1: TButton;Button2: TButton;procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);end;varForm1: TForm1;implementation{$R *.dfm}uses MsXML, SuperObject;//字符串到 UTF8 編碼的函數(shù), 用于 Google 地址 function ToUTF8Encode(str: string): string; varb: Byte; beginfor b in BytesOf(UTF8Encode(str)) doResult := Format('%s%s%.2x', [Result, '%', b]); end;//翻譯函數(shù) function Translate(str, RequestLanguage, ResultLanguage: string): string; constBaseUrl = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='; varUrl: string;jo: ISuperObject;req: IXMLHTTPRequest; beginUrl := BaseUrl + ToUTF8Encode(str) + '&langpair=' + RequestLanguage + '%7C' + ResultLanguage;req := CoXMLHTTP.Create;req.open('Get', Url, False, EmptyParam, EmptyParam);req.send(EmptyParam);jo := SO(req.responseText);Result := jo.Format('%responseData.translatedText%'); end;//英譯漢 procedure TForm1.Button1Click(Sender: TObject); beginMemo2.Text := Translate(Memo1.Text, 'en', 'zh-cn'); end;//漢譯英 procedure TForm1.Button2Click(Sender: TObject); beginMemo1.Text := Translate(Memo2.Text, 'zh-cn', 'en'); end;end.
窗體文件:
object Form1: TForm1Left = 0Top = 0Caption = 'Form1'ClientHeight = 139ClientWidth = 459Color = clBtnFaceFont.Charset = DEFAULT_CHARSETFont.Color = clWindowTextFont.Height = -11Font.Name = 'Tahoma'Font.Style = []OldCreateOrder = FalsePixelsPerInch = 96TextHeight = 13object Memo1: TMemoLeft = 0Top = 0Width = 185Height = 139Align = alLeftLines.Strings = ('Memo1')TabOrder = 0ExplicitHeight = 202endobject Button1: TButtonLeft = 191Top = 24Width = 75Height = 25Caption = #33521#25991' -> '#20013#25991TabOrder = 1OnClick = Button1Clickendobject Memo2: TMemoLeft = 274Top = 0Width = 185Height = 139Align = alRightLines.Strings = ('Memo2')TabOrder = 2ExplicitLeft = 312ExplicitTop = -8ExplicitHeight = 202endobject Button2: TButtonLeft = 191Top = 72Width = 75Height = 25Caption = #33521#25991'
轉(zhuǎn)載于:https://www.cnblogs.com/del/archive/2009/10/28/1591380.html
總結(jié)
以上是生活随笔為你收集整理的JSON 之 SuperObject(17): 实例 - 借用 Google 实现全文翻译的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑很卡~~~~为什么???
- 下一篇: java多线程编程基础