<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<div>
<asp:updatepanel id="UpdatePanel12" runat="server">
<ContentTemplate>
<div style="position: relative; width: 324px; margin-left: 20px; margin-top:20px;
margin-bottom:20px; ">
<asp:Label ID="lblInstructions" runat="server" Width="359px" Text="Enter some text for the
language you want detected:"></asp:Label>
</div>
<!-- Textbox for the input text to be detected -->
<div style="position: relative; width: 324px; margin-left:20px; margin-bottom:10px;">
<asp:TextBox ID="txtInput" runat="server" Width="359px" Height="200px"
TextMode="MultiLine"></asp:TextBox>
</div>
<!-- Button with click event to detect the language of the text in the txtInput textbox -->
<div style="position: relative;width: 167px; margin-left:20px; margin-bottom:10px;">
<asp:Button ID="btnTranslate" runat="server" Text="Detect Language"
onclick="btnTranslate_Click" />
</div>
<!-- Label for the result of the detect method -->
<div style="position: relative;width: 324px; margin-left:20px; margin-bottom:10px;">
<asp:Label ID="lblOutput" runat="server" Width="602px"></asp:Label>
</div>
<fieldset>
<legend>Results</legend>
<table>
<tr>
<td>
Easily use the Microsoft Translator HTTP API to translate a specific string or
detect the language of specific text. This can be achived through a simple web
request to the Microsft Translator HTTP web service.
</td>
</tr>
<tr >
<td>
The example above demonstrates how the translator can detect a language parsed
to it through the web request URL. Try entering your own text into the above box
and clicking the 'Detect Language' button - the language you entered
will be returned in it's culture code format. Once you've tried
the demo click the Code-Behind tab above to see how this has been achieved in c#.
</td>
</tr>
</table>
</fieldset>
</ContentTemplate>
</asp:updatepanel>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
namespace InteractiveSDK.HTTP
{
public partial class HTTPDemo1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
// Click event for the button
protected void btnTranslate_Click(object sender, EventArgs e)
{
try
{
// Create a web request to the HTTP API web service
// Add the appId and textbox parameters to the request URL
WebRequest req = System.Net.WebRequest.Create(
@"http://api.microsofttranslator.com/V1/Http.svc/Detect?appId=yourAppId&text="
+ txtInput.Text);
// Get the response of the web request
WebResponse resp = req.GetResponse();
// Stream and read the web response
Stream strm = resp.GetResponseStream();
StreamReader reader = new System.IO.StreamReader(strm);
// Create a string for the contents of the web response
string locale = reader.ReadToEnd();
// Display the contents of the string in the label
lblOutput.Text = "The detected language is: '" + locale + "'.";
}
// Catch any undetectable text and display error message in the label
catch
{
lblOutput.Text = "The entered text is not valid.";
}
}
}
}