Google Translate without API helper for C# and Windows Phone

Hi!

As usual I’m stricken with a class test syllabus and its boring enough to motivate me to get coding on C#. I was actually looking for making a Bangla Dictionary for Windows Phones and Androids later. I had like 2,50,000 words in my hand and wanted to make something that would give me a sql server sdf file and a sqlite database translating all those words in a batch processing mode. 😉

The thing is, I made a pretty interesting stuff today. A very tiny and noob C# library that helps you translate text from google translate without the api’s help itself. Kindly only translate words as I didn’t consider sentences for now.

The library is attached below and named GoogleTranslateWithNoApiBasicWP. I’m planning to make an advanced one with Batch processing from text files, database file returns and xml dictionary and stuff like that. But one night has to be sufficient for this. 🙂

So, this is the daily deeds category of my blog and that means “less talk, more code”.
The library has two releases, one for .net 4 and one for windows phones. And, one other thing, I used this amazing library named Json.net here. So, you have to reference it too to make this library work. Ive already attached the .dll though.

Json.net home : http://json.codeplex.com/

in .net 4 version, all you gotta do is:

TranslateHelper Helper = new TranslateHelper();
TranslatedData foundData=Helper.TranslateThis("love", "en", "bn");

And to get a good vision on the TranslatedData class, I used this to write it using a StreamWriter. You can easily get only the translated data by accessing TranslatedText. But if you need all the meanings you gotta access Meanings list and you’ll find parts of speech and terms under that. I used something like this below to write these in a text file:

StreamWriter writer = new StreamWriter("returnData.txt");

writer.WriteLine("Source Language: " + foundData.SourceLang);
writer.WriteLine("Target Language: " + foundData.TargetLang);
writer.WriteLine("Source Text: " + foundData.SourceText);
writer.WriteLine("Translated Text: " + foundData.TranslatedText);

foreach (var item in foundData.Meanings)
{
writer.WriteLine(item.PartsOfSpeech + ": ");

foreach (var term in item.Terms)
{
writer.WriteLine(term.ToString());
}

}
writer.WriteLine("----------------------------------------");

writer.Close();

Now come to the Windows Phone part: Its kind of the same except you got to use the OnTranslationCompleted event as it loads Async.

public MainPage()
{
InitializeComponent();

helper = new TranslateHelper();
helper.TranslateThis("love", "en", "bn");
helper.OnTranslationCompleted += helper_OnTranslationCompleted;

}

void helper_OnTranslationCompleted(object myObject, TranslateArgs myArgs)
{
Dispatcher.BeginInvoke(()=>this.Data.Text=helper.foundData.TranslatedText);
}

Hope this helps! 🙂 I will probably upload it to codeplex (If it really seems worthy enough later) but for now you can check the little baby lib through here http://sdrv.ms/Yyxkd1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s