原文 V8 o7 i+ I) Q
http://www.dotnetcurry.com/ShowArticle.aspx?ID=357&AspxAutoDetectCookieSupport=1
4 E* j* }5 G6 O' x% ~" b
. z9 y E2 N4 o5 P2 d7 S3 {$ {& ?/ x; u3 j! i A+ [& [
[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#5 d% H( Q; f' C3 q7 d
private void btnTranslate_Click(object sender, EventArgs e)& @7 o& [) Y6 b( f$ U& ~$ Z
{ _* x/ k: O# n h; B
string strTranslatedText = null;( Z$ J4 ]; L& V" @' T$ c2 {) v
try $ v$ {5 f! _& @' M" L. G" P
{% B+ S+ \6 }) P1 U
TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient();8 m# `+ e G C0 y8 v
client = new TranslatorService.LanguageServiceClient();4 c) N. f) N: W8 }0 b3 n
strTranslatedText = client.Translate("your App ID", txtTraslateFrom.Text, "", "en");; V% @' d! @4 [$ x \
txtTranslatedText.Text = strTranslatedText;0 }; {7 `8 {# J8 @3 h" v# t
}
" u/ `6 g( P& J8 y" ]; Z catch (Exception ex) 5 j# f1 \* w g# @8 s3 Y2 V
{
% G/ |/ G: F6 K8 d$ o MessageBox.Show(ex.Message);
1 r$ L( U$ N2 x0 [, i: \5 Q$ H* v0 L9 [ }' @9 j9 B ~$ L8 F) r
} ~ A6 ~0 g/ P+ ^+ L, j
: E/ }' E1 _% H5 i: K0 k
VB.NET n; v" c- P& w
Private Sub btnTranslate_Click(ByVal sender As Object, ByVal e As EventArgs)
9 t! `3 P% t4 H4 u Dim strTranslatedText As String = Nothing
6 H; X% k& K0 D7 K; w- V* z Try
$ j7 K2 O' F" B Dim client As New TranslatorService.LanguageServiceClient()
7 J5 h" z9 ^% ? client = New TranslatorService.LanguageServiceClient()
; s8 b+ d( y" \7 @0 E7 O strTranslatedText = client.Translate("your App ID", txtTraslateFrom.Text, "", "en")1 H/ g* j5 d5 {; n
txtTranslatedText.Text = strTranslatedText4 _) u+ R: c9 }
Catch ex As Exception
- [1 j: P0 C4 W+ E1 Q2 K8 P MessageBox.Show(ex.Message)' e- L9 q; }- t# r$ {
End Try
9 e8 u7 U3 _# CEnd Sub
. B2 y" s# Y0 Z, p% I o& U
6 a u. X' N% L" x4 a0 P( u[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. |