Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  




           


Using new Microsoft Azure (Bing) Translation API via C#
Microsoft offers a translation service API as part of its Azure service offering.

You must sign up for the service. Currently there is an offering of a free level with 2M characters translated for free.

You will get an account ID to use the service.

To use the service from .NET, you can generate the TranslatorContainer class using the button on the Microsoft site, or copy it from below. Then, use the class to translate text. An example method is below.

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18033
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

// Notice: Use of the service proxies that accompany this notice is subject to
// the terms and conditions of the license agreement located at
// http://go.microsoft.com/fwlink/?LinkID=202740
// If you do not agree to these terms you may not use this content.
namespace Microsoft
{
using System;
using System.Collections.Generic;
using System.Data.Services.Client;
using System.Net;
using System.IO;


public partial class Translation
{

private String _Text;

public String Text
{
get
{
return this._Text;
}
set
{
this._Text = value;
}
}
}

public partial class Language
{

private String _Code;

public String Code
{
get
{
return this._Code;
}
set
{
this._Code = value;
}
}
}

public partial class DetectedLanguage
{

private String _Code;

public String Code
{
get
{
return this._Code;
}
set
{
this._Code = value;
}
}
}

public partial class TranslatorContainer : System.Data.Services.Client.DataServiceContext
{

public TranslatorContainer(Uri serviceRoot) :
base(serviceRoot)
{
}

/// <summary>
/// </summary>
/// <param name="Text">the text to translate Sample Values : hello</param>
/// <param name="To">the language code to translate the text into Sample Values : nl</param>
/// <param name="From">the language code of the translation text Sample Values : en</param>
public DataServiceQuery<Translation> Translate(String Text, String To, String From)
{
if ((Text == null))
{
throw new System.ArgumentNullException("Text", "Text value cannot be null");
}
if ((To == null))
{
throw new System.ArgumentNullException("To", "To value cannot be null");
}
DataServiceQuery<Translation> query;
query = base.CreateQuery<Translation>("Translate");
if ((Text != null))
{
query = query.AddQueryOption("Text", string.Concat("\'", System.Uri.EscapeDataString(Text), "\'"));
}
if ((To != null))
{
query = query.AddQueryOption("To", string.Concat("\'", System.Uri.EscapeDataString(To), "\'"));
}
if ((From != null))
{
query = query.AddQueryOption("From", string.Concat("\'", System.Uri.EscapeDataString(From), "\'"));
}
return query;
}

/// <summary>
/// </summary>
public DataServiceQuery<Language> GetLanguagesForTranslation()
{
DataServiceQuery<Language> query;
query = base.CreateQuery<Language>("GetLanguagesForTranslation");
return query;
}

/// <summary>
/// </summary>
/// <param name="Text">the text whose language is to be identified Sample Values : hello</param>
public DataServiceQuery<DetectedLanguage> Detect(String Text)
{
if ((Text == null))
{
throw new System.ArgumentNullException("Text", "Text value cannot be null");
}
DataServiceQuery<DetectedLanguage> query;
query = base.CreateQuery<DetectedLanguage>("Detect");
if ((Text != null))
{
query = query.AddQueryOption("Text", string.Concat("\'", System.Uri.EscapeDataString(Text), "\'"));
}
return query;
}
}
}




/// <summary>
/// Localize a string using online bing API
/// </summary>
/// <param name="englishText"></param>
/// <returns></returns>
public static string TranslateViaWeb(string englishText)
{
// this is the Account Key generated for this app
string accountKey = "PUT YOUR KEY HERE";
string fromlanguage = "en";//from language you can change this as your requirement
string texttotranslate = englishText;//what to be translated?
string tolanguage = "fr";//in which language?

// this is the service root uri for the Microsoft Translator service
var serviceRootUri = new Uri("https://api.datamarket.azure.com/Bing/MicrosoftTranslator/");


// the TranslatorContainer gives us access to the Microsoft Translator services
TranslatorContainer tc = new TranslatorContainer(serviceRootUri);

// Give the TranslatorContainer access to your subscription
tc.Credentials = new NetworkCredential(accountKey, accountKey);

// Generate the query
var translationQuery = tc.Translate(englishText, tolanguage, fromlanguage);


// Call the query and get the results as a List
var translationResults = translationQuery.Execute().ToList();

// Verify there was a result
if (translationResults.Count() <= 0)
{
return null;
}

// In case there were multiple results, pick the first one
var translationResult = translationResults.First();
return translationResult.Text;
}

Created By: amos 5/8/2013 3:52:29 PM