冒险解谜游戏中文网 ChinaAVG

标题: BING Desktop Translator using C# and VB.NET [打印本页]

作者: shane007    时间: 2010-6-10 11:50
标题: BING Desktop Translator using C# and VB.NET
原文
8 A& O0 T8 ^( z  e% w6 lhttp://www.dotnetcurry.com/ShowArticle.aspx?ID=357&AspxAutoDetectCookieSupport=1
$ B9 x6 T# M& F' X, v6 L. r* h3 W, O3 N, G( o8 J4 Z! q- O6 N

% H, r& M2 K# G2 m' o  N# \[align=justify]In this article we will see how we can create a simple Desktop Translator which uses the Bing Translator API. Next time when you want to translate a text, you do not have to open a new web browser and head to Bing or Google Translator. Just use this simple Translator I demonstrate here.[align=justify]Let’s get started:[align=justify]Step 1: Start Visual studio and create a new Windows Forms Application[align=justify]Step 2: Add 2 TextBoxes and name them ‘txtTraslateFrom’ and ‘txtTranslatedText’ and set their ‘MultiLine’ property to ‘True’[align=justify]Step 3: Add a Button to the form and name it ‘btnTranslate’[align=justify]Step 4: Right Click the project and add a ‘Service Reference’ to http://api.microsofttranslator.com/V1/SOAP.svc with Namespace as TranslatorService[align=justify]Step 5: To use Bing Translator Web Service you will need an AppID. Go to http://www.bing.com/developer and create a new AppID for our Desktop Translator.[align=justify]Step 6: Add the following code to the Button’s Click EventC## u3 ?* J; ]8 ?4 W' m
private void btnTranslate_Click(object sender, EventArgs e)9 a; R: n/ F! M# }* E! e
{
) R( q  |  O7 p* l/ Y) Y! q    string strTranslatedText = null;5 z; G5 k+ b) E$ k4 \
        try 3 ]' i7 x- f6 v, T: p, }; F
        {: p  q% c0 c) w" Y0 d2 G! w' s+ \2 n. k
         TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient();* e" D  q; j& Z$ A+ Z, a
         client = new TranslatorService.LanguageServiceClient();* O9 q6 X( g. o* S+ u
         strTranslatedText = client.Translate("your App ID", txtTraslateFrom.Text, "", "en");' G" _0 ?1 J; }9 Q1 f* M0 X% J4 P
         txtTranslatedText.Text = strTranslatedText;: K" W  N) x5 o& d
        }
. E, J: e2 N( y/ n! |        catch (Exception ex) " ?) ~2 q$ C3 t( r
        {% i* F$ C  H7 K5 G
         MessageBox.Show(ex.Message);9 r. D& K6 h4 I3 `, ~6 ~
        }
, C1 V" K5 d; }8 R; g* i$ L}: g5 t8 g/ i1 `
2 M5 B% H6 t3 O' f: Q0 A
VB.NET' q$ I, N$ D# c# b) H
Private Sub btnTranslate_Click(ByVal sender As Object, ByVal e As EventArgs)
) `4 I# V+ t( l      Dim strTranslatedText As String = Nothing
3 P+ L% v- d; N- D            Try
! H# ^+ R0 J0 _8 H2 b2 _2 e. {             Dim client As New TranslatorService.LanguageServiceClient()3 f9 Y  B+ X# {
             client = New TranslatorService.LanguageServiceClient()7 h, g) ?1 B  E0 q% R% l: m9 I& k8 \9 p
             strTranslatedText = client.Translate("your App ID", txtTraslateFrom.Text, "", "en"): @0 f- n" o; H. H' l: }# p3 B# J3 B
             txtTranslatedText.Text = strTranslatedText
$ Z& [# r% X/ h3 m& p            Catch ex As Exception1 F# S3 m+ @; o
             MessageBox.Show(ex.Message)
+ }) O: d5 u1 h. ^8 A" k+ b2 M            End Try
( N* W6 w6 ], U- O3 z' D+ |End Sub; r  w3 w/ u$ U. K, O
! J& \: m! _  f) l3 O  l9 G
[align=justify]The ‘Translate’ method takes in 4 parameters - ‘appid, text, from and to’.  In the above code we are calling the Translate method with the AppID we created in Step 5 and also passing the Text from Textbox. We pass an empty string in the ‘to’ parameter so that Bing detects the language automatically. We also set the output language to ‘en’ (English).[align=justify] [align=justify]That’s it! Our desktop Bing Translator is ready. Below screenshot shows a sampe translation from German to English.
作者: boldness    时间: 2010-6-11 22:43
似乎不支持中文.. 遗憾




欢迎光临 冒险解谜游戏中文网 ChinaAVG (https://chinaavg.com/) Powered by Discuz! X3.2