冒险解谜游戏中文网 ChinaAVG

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

作者: shane007    时间: 2010-6-10 11:50
标题: BING Desktop Translator using C# and VB.NET
原文
( ]/ C; ?3 d7 U% Uhttp://www.dotnetcurry.com/ShowArticle.aspx?ID=357&AspxAutoDetectCookieSupport=1
" d5 l3 h( E* x% O. ?9 J7 [$ S8 k/ `; t  O! P6 F9 R! T
& ]4 R5 p7 M# |; s& @
[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#4 N- Y: Z5 s" c
private void btnTranslate_Click(object sender, EventArgs e)0 c& _1 |. w7 C/ }' F( h  t, K
{5 m+ a& Z! |, q& j0 q3 f9 i
    string strTranslatedText = null;
& U* k( O, S' N4 w        try 1 |* M5 B0 u0 g, H1 z& Q0 N
        {! j: m) d. Q' o
         TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient();
& ^  @, y! }2 C* v         client = new TranslatorService.LanguageServiceClient();/ c" h# f0 }! b4 T  J7 G1 p
         strTranslatedText = client.Translate("your App ID", txtTraslateFrom.Text, "", "en");% i4 n# t) L1 k1 @/ K( w- B
         txtTranslatedText.Text = strTranslatedText;. j( F$ l" [' L) h. _, G
        }4 O' M& Y# U6 l1 o$ H: c
        catch (Exception ex) 4 b5 B" O1 F+ n+ ~$ L4 g
        {+ C% x+ O% @1 n9 F( F7 d; U: T
         MessageBox.Show(ex.Message);$ O. x! @0 A* u& o1 i( @
        }, n. l8 b7 \, N+ n; [
}8 ~3 F& ?( q2 T' n
* p. z5 A! b2 i, U3 `
VB.NET* S7 L- w5 ^6 }& [. F
Private Sub btnTranslate_Click(ByVal sender As Object, ByVal e As EventArgs)0 y: k4 ^- e$ h
      Dim strTranslatedText As String = Nothing
% e& A9 ~, Y9 Y6 |4 j2 h9 l$ {            Try' _% W6 ?/ U. s6 h7 m
             Dim client As New TranslatorService.LanguageServiceClient()
" V$ v) T2 B4 A( k             client = New TranslatorService.LanguageServiceClient()+ f+ e2 [; ~3 I1 E" \( o. y+ U
             strTranslatedText = client.Translate("your App ID", txtTraslateFrom.Text, "", "en")
" A7 v. v  p/ L             txtTranslatedText.Text = strTranslatedText) W! ]+ g1 n( o
            Catch ex As Exception
% B" _, P6 Y$ _% X) W0 \             MessageBox.Show(ex.Message)7 I3 J" [: b3 {; W9 L8 w) F1 b% f8 j
            End Try4 [8 q% A6 D0 v
End Sub  c. a) ?7 N% R) S7 m% @5 D8 Y
+ \$ K1 {8 [9 O" P# v8 c0 d9 b
[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