<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>
<!-- Input for the text we want to detect the language of -->
<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 to call the GetLanguages/GetLanguageNames methods through the SOAP API -->
<div style="position: relative;width: 167px; margin-left: 20px; margin-bottom:20px;">
<asp:Button ID="btnGetLang" runat="server" Text="Detect Language"
onclick="btnGetLang_Click" />
</div>
<!-- Label to display the result -->
<div style="position: relative;width: 324px; margin-left: 20px; margin-bottom:20px;">
<asp:Label ID="lblOutput" runat="server" Width="602px"></asp:Label>
</div>
<fieldset>
<legend>Results</legend>
<table>
<tr>
<td>
This demo will build upon the previous one in which we used the Microsoft
Translator SOAP API's Detect method to return the culture code of an input string.
With the GetLanguages and GetLanguageNames methods we can make SOAP calls to
return the full list of supported culture codes and language names.
</td>
<tr />
<tr>
<td>
In this demo we use the two methods to populate a static dictionary, the culture
codes are entered with their associated full language name. Now when the button
is clicked and the Detect method covered previously is executed, we parse the
detected culture code to the dictionary and are returned the name of the language
to be used in our result.
</td>
</tr>
<tr />
<tr>
<td>
Enter some text in the box above and click the 'Detect Language' button, the
detected culture code as well as the language pulled from the dictionary will
be displayed. Click the Code-Behind tab above to see exactly how this is
done 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.SOAP
{
public partial class SOAPDemo3 : System.Web.UI.Page
{
// Create a static dictionary for the GetLanguages/GetLanguageNames results
private static Dictionary<string, string> languages = new Dictionary<string, string>();
// Create a static method for the page so the dictionary is only populated once
// (To save load time and server bandwidth)
static SOAPDemo3()
{
// Create an instance of the TranslatorService
TranslatorService.LanguageServiceClient client =
new TranslatorService.LanguageServiceClient();
// Call the GetLanguages method and parse the appId
// Store the results in an array (they will be culture codes)
string[] locales = client.GetLanguages("yourAppId");
// Call the GetLanguageNames method and parse the appId and page locale
// Store the results in an array (they will be the full language names)
string[] names = client.GetLanguageNames("yourAppId", "en");
// Iterate through the arrays and add the results to the dictionary
for (int i = 0; i < locales.Length; i++)
{
languages[locales[i]] = names[i];
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnTranslate_Click(object sender, EventArgs e)
{
try
{
// Create an instance of the TranslatorService
TranslatorService.LanguageServiceClient client =
new TranslatorService.LanguageServiceClient();
// Call the detect method and parse in the input for the textbox
// Store the result in a string
string locale = client.Detect("yourAppId", txtInput.Text);
// Display the detect result in the label and use the result to match the
// culture code with the full language name in the ditionary then
// display the full name
lblOutput.Text = "The detected language is: '" + locale +
"' which is the culture code for " + languages[locale] + ".";
}
catch
{
// Catch any errors
lblOutput.Text = "The entered text is not valid.";
}
}
}
}