March 12, 2011

English to Native language Transalator

This program is based on Console Application using C#.Net Framework 4.0. This program using Google Transaltion API and you get your transaltion API from Code.Google.com

Here's Code

static void Main(string[] args)
{
Console.WriteLine("Please write some thing here to be translate");

string text = Console.ReadLine();
string fromLanguage = "en"; //English
string toLanguage = "es"; // Spanish
string apiKey = ""; // Enter your APIKey here
string apiUrl = "https://www.googleapis.com/language/translate/v2?key={0}&source={1}&target={2}&q={3}";
string url = string.Format(apiUrl, apiKey, fromLanguage, toLanguage, text);

var translateLanguage = new Translate();
string returnData = translateLanguage.TranslateLanguage(url);
Console.WriteLine("Your translate sentence in spanish is: \n" + returnData);

Console.ReadLine();
}

Class Code

public class Translate
{
public string TranslateLanguage(string url)
{
string returnString = string.Empty;

try
{
WebRequest request = HttpWebRequest.Create(url);
request.Method = "GET";

using (WebResponse response = request.GetResponse())
{
using (var streamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
returnString = streamReader.ReadToEnd();
}
}
}
catch (Exception exception)
{
Console.Out.WriteLine(exception.Message);
}
return returnString;
}
}