Quantcast Xây dựng ứng dụng BING Translator sử dụng C# và VB.NET

Xây dựng ứng dụng BING Translator sử dụng C# và VB.NET

by Neon Quach 28. July 2009 01:54

Sau sự ra đời của Bing là Bing API, cung cấp 1 thư viện cho các lập trình viên muốn tận dụng API Framework, trong bài viết này mình sử dụng Bing API 2.0, xây dựng ứng dụng BING Translator sử dụng C# and VB.NET.

Đầu tiên tạo 1 project C# hay VB.NET, design 2 textboxes, 1 cái dùng để nhập ngôn ngữ cần dịch và cái khác để hiển thị ngôn ngữ sau khi dịch, và 1 nút để thực thi lệnh.

Tạo 1 application ID: http://www.bing.com/developers/createapp.aspx



Từ Project chọn add web reference...

http://api.microsofttranslator.com/V1/Soap.svc (default namespace)

Thực thi sự kiện click cho button: C#

private void buttonTranslate_Click(object sender, EventArgs e)
{
com.microsofttranslator.api.Soap s = new CS.com.microsofttranslator.api.Soap();
try
{
string textTranslated = s.Translate("app id", textBoxTranslateFrom.Text, "", "en");
textBoxTranslatedText.Text = textTranslated;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

VB.NET

Private Sub buttonTranslate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonTranslate.Click

Dim s As com.microsofttranslator.api.Soap = New com.microsofttranslator.api.Soap()
Try
Dim strTextTranslated As String = s.Translate("app id", textBoxTranslateFrom.Text, "", "en")
textBoxTranslatedText.Text = strTextTranslated
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

Hàm Translate nhận vào 1 chuổi application ID mà bạn tạo ở trước bài viết, tham số thứ 2 là chuổi cần được dịch, tham số 3 ở đây là rổng vì để application id tự động phát hiện ngôn ngữ, "en" tham số 4 là ngôn ngữ dịch.


BingTranslater.rar (140.72 kb)

Tags:


Categories: bing

blog comments powered by Disqus

About me

I'm  currently employed as Software developer at devinition.com and also a Microsoft Certified Technology Specialist (MCTS), Microsoft Certified Professional Developer (MCPD) in Net Framework 2.0 and 3.5: Web Applications and MCTS .NET Framework 3.5, ADO.NET Applications

Powered by BlogEngine.NET 2.5.0.5 - Eco Theme by n3o Web Designers