原文- e a, p% V* W! G! x. y3 y* e
http://www.dotnetcurry.com/ShowArticle.aspx?ID=357&AspxAutoDetectCookieSupport=14 r' B( o9 L$ s9 P
* p7 Q. z( ^ i: U& \, l9 A2 ?, B) Y* B2 O: |
[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#
& f) M: X! Q# K" X7 c c, F/ G" w! ]private void btnTranslate_Click(object sender, EventArgs e)
% c8 `. c+ ^) g0 p{4 [) M" A- s8 k6 y1 u
string strTranslatedText = null;
* }3 j* L3 A, Y( Q try
9 c& o1 v) c( ?; Q$ w2 x {4 T6 [* m3 o: B9 J7 n, i: \3 `* J2 R
TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient();
& X5 x0 |# M4 w client = new TranslatorService.LanguageServiceClient();
8 R8 `5 }4 H6 ] A/ V strTranslatedText = client.Translate("your App ID", txtTraslateFrom.Text, "", "en");
% L% R( c( i ] f; ]( i txtTranslatedText.Text = strTranslatedText;
5 d2 L, t( s8 z4 A9 T* d }1 B3 \) |# t$ G
catch (Exception ex)
3 R" a7 P7 V4 p1 y {* H9 m {
+ [, T$ l. O. U3 p MessageBox.Show(ex.Message);
2 V+ R. d0 k9 e2 t- v F. { }7 R) T2 h3 L( `
}, C9 n0 R2 `; d* u# S& s
: d* g2 Z9 N: E( {" QVB.NET: V" T. t1 S7 h% f3 T" K; ~
Private Sub btnTranslate_Click(ByVal sender As Object, ByVal e As EventArgs)
+ p# ^. k6 c7 m$ I; H3 b$ M/ ~: z Dim strTranslatedText As String = Nothing& I2 Y' ]8 o# c% u" W
Try/ [- q {( k, C1 R% w8 a
Dim client As New TranslatorService.LanguageServiceClient()( a# H+ d: z& _) x
client = New TranslatorService.LanguageServiceClient()
& J3 t1 `5 v- O& Q+ R, X; E strTranslatedText = client.Translate("your App ID", txtTraslateFrom.Text, "", "en")
' o! d2 N" G7 w7 @6 r txtTranslatedText.Text = strTranslatedText
) F/ E" `3 R5 Y Catch ex As Exception0 N' J5 P" o& {
MessageBox.Show(ex.Message)
, P. ^; @3 q' q9 r5 o End Try- R& K7 U J, m% k
End Sub' b) O5 n* A! V$ k
+ ?" g3 Z' d. o5 v: W0 x
[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. |