原文' `; q. Q% @4 Y/ v5 v1 F0 J- {- a
http://www.dotnetcurry.com/ShowArticle.aspx?ID=357&AspxAutoDetectCookieSupport=1
# L$ e2 Q& D" g9 Z* {" _9 X2 ^( F* N7 F# H- m3 M# H, V
! c0 j) O U. g; W7 P9 e& c[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#3 W2 [. b. `& b8 z
private void btnTranslate_Click(object sender, EventArgs e)0 g9 ^: d3 U9 Q9 |" Y4 V, ]
{
# @; b- V1 o+ X string strTranslatedText = null;
7 Z; G G5 G% o$ d+ L try
0 }' K \3 r. A5 b {
/ E% A s6 p' v# T TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient();
6 F0 q0 g7 A. H9 l client = new TranslatorService.LanguageServiceClient();. |( R8 U: o' ]" l1 P( r9 b
strTranslatedText = client.Translate("your App ID", txtTraslateFrom.Text, "", "en");
5 l* q; u6 z* l1 n L Q& g3 X: G" o txtTranslatedText.Text = strTranslatedText;
/ j/ X& f D5 G( `9 M }& a6 N. H. ?) ~0 Y, q, M- [* F* ]
catch (Exception ex)
) f2 V3 v1 K A {, g, E' m, F. e! Q
MessageBox.Show(ex.Message);
9 q$ R4 z3 l+ @% C& Z( ~ }
, w6 p: S5 J2 M}
: G$ O& J6 k& m4 q8 `( G0 g5 v) u3 I % f I/ @, H: z7 y/ d
VB.NET
! b; `+ r8 B* C4 }6 ^Private Sub btnTranslate_Click(ByVal sender As Object, ByVal e As EventArgs)& c! f: b* |" l" c
Dim strTranslatedText As String = Nothing
: z5 B; ~" y7 L' N* O Try
/ [1 X' y1 f/ V) n( r) | Dim client As New TranslatorService.LanguageServiceClient() d9 }7 f8 j! U+ ~& ]/ Y5 y
client = New TranslatorService.LanguageServiceClient()7 e6 G) Q( O5 g' h
strTranslatedText = client.Translate("your App ID", txtTraslateFrom.Text, "", "en")
1 T" _4 f9 T5 U( K0 }9 G) L. y1 G txtTranslatedText.Text = strTranslatedText
' l2 Q' `9 Q9 B9 H' e) J Catch ex As Exception
$ ?5 M1 a) j9 I MessageBox.Show(ex.Message)! Y/ ]4 H9 l4 e6 J+ X
End Try/ x/ v w0 b7 O# c& o: e. y
End Sub
# [- Q% u) q. l9 F# M
. s9 b4 i$ {& D$ L: _[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. |