Sunday, August 4, 2013

Incorporate Google Translate into your C# program

Step 1: Download and add the Google .Net Client to your C# solution.

Download the google-api-dotnet-client-1.4.0-beta.samples from

https://code.google.com/p/google-api-dotnet-client/wiki/Downloads

Unzip the package and add the "GoogleApis.SampleHelper" and "Translate.TranslateText" projects to your C# solution,

Step 2: Configure your C# project for Google Translate.

Add the following libraries from the "google-api-dotnet-client-1.4.0-beta.samples/Lib" or "google-api-dotnet-client-1.4.0-beta.samples/Lib/ThirdParty" folder to your C# project:

DotNetOpenAuth
Google.Apis
Google.Apis.Authentication.OAuth2
Newtonsoft.Json
System.Net.Http
System.Net.Http.Exteions
System.Net.Http.Primitives
System.Net.Http.WebRequest
Zlib.Portable
System.Threading.Tasks

Add "System.Net" and "GoogleApis.SampleHelper" references to your C# project as well.

Next copy the ""Google.Apis.Translate.v2.cs" from the "Translate.TranslateText" project to your C# project.

Next right-click your C# project and selects "Manage NuGet Packages", in the "Manage NuGet Packages" dialog, search and install the following packages:

Microsoft BCL Build Components
Microsoft BCL Portability Package
Microsoft HTTP Client Libraries

Step 3: Activate your google translate services.
You need to activate your google translate service in order to use google translate in your C# program. go to the Google API Console at:

code.google.com/apis/console‎

Select the "Services" menu from the right panel, and turn on the Google translate service from there. now copy your google api access key by selecting the "API Access" from the right panel, and copying the key in the line starting with "API Key:"

Step 4: Implement the coding
Below i provide a simple singleton class for you to use the translation service.

using Google.Apis.Samples.Helper;
using Google.Apis.Services;
using Google.Apis.Translate.v2;
using Google.Apis.Translate.v2.Data;
using TranslationsResource = Google.Apis.Translate.v2.Data.TranslationsResource;

public sealed class TranslationManager
{
 private static TranslationManager mInstance = null;
 private static object mSyncObj = new object();

 public static TranslationManager Instance
 {
  get
  {
   if (mInstance == null)
   {
    lock (mSyncObj)
    {
     mInstance = new TranslationManager();
    }
   }
   return mInstance;
  }
 }

 private TranslationManager()
 {

 }

 private string GetApiKey()
 {
  return "[Your-API-Key]";
 }

 public KeyValuePair<string, string> Translate(string srcText, string target_language = "en")
 {
  string[] text = new string[1] { srcText };
  Dictionary<string, KeyValuePair<string, string>> translation=Translate(text, target_language);
  return translation[srcText];
 }

 public Dictionary<string, KeyValuePair<string, string>> Translate(string[] srcText, string target_language = "en")
 {

  // Create the service.
  var service = new TranslateService(new BaseClientService.Initializer()
  {
   ApiKey = GetApiKey(),
   ApplicationName = "Translate API Your App"
  });

  TranslationsListResponse response = service.Translations.List(srcText, target_language).Execute();
  Dictionary<string, KeyValuePair<string, string>> translations = new Dictionary<string, KeyValuePair<string, string>>();

  int counter = 0;
  foreach (TranslationsResource translation in response.Translations)
  {
   translations[srcText[counter]] = new KeyValuePair<string, string>(translation.TranslatedText, translation.DetectedSourceLanguage);
   counter++;
  }

  return translations;
 }
}

Below is a simple example of how to use the TranslationManager class:

string sourceText = "早上好";
string target_language = "en";

KeyValuePair<string, string> translation=TranslationManager.Instance.Translate(sourceText, target_language);
Console.WriteLine("Translated: {0}", translation.Key);
Console.WriteLine("Source Language: {0}", translation.Value);

No comments:

Post a Comment