Click here to Skip to main content
Licence CPOL
First Posted 25 Jan 2010
Views 10,815
Downloads 283
Bookmarked 19 times

Localize Application using Google Translator

By | 25 Jan 2010 | Article
Commonly we used to store translated phrases, text in resource files .resx or in database, it can be tedious job to store huge resources, In my example, I am going to use Google Translator - it is a little tricky but can be helpful.
GoogleTranslator

Introduction

Since everyone wants their application to be presented according to the user's culture, location, etc, in this example I used the Google Translator, calling GoogleTranslator by HttpRequest and reading the Translated string from the Response stream.

Background

This code will click many ideas in your mind as to how you can use resources already available in the market using HttpRequest and Response.

Using the Code

In the below code, I translate from English Language to the required language. You can change it to the desired culture. Just replace 'en' with the desired culture name.

private string Translate(string text, string l)
{
string translated = null;
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create
	("http://translate.google.com/translate_s?hl=en&clss=&q=" + 
	text.Replace(" ", "+") + "&tq=&sl=en&tl=" + l);
HttpWebResponse res = (HttpWebResponse)hwr.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string html = sr.ReadToEnd();
int rawlength1 = html.IndexOf("<span id=otq><b>");
string rawStr1 = html.Substring(rawlength1);
int rawlength2 = rawStr1.IndexOf("</b>");
string rawstr2 = rawStr1.Substring(0, rawlength2);
translated = rawstr2.Replace("<span id=otq><b>", "");
tbStringToTranslate.Text = text;
return translated;
}        

Points of Interest

I have written this code to localize my application texts, Labels...... I too used to store translated stings in my DataBase. This makes me use this code as I don't need to feed translated data in database whenever a required text to translate is passed to it. First look in DB, if not found it gets it from Google Translator and feeds in DB. I don't even require to maintain resource files for different cultures because I get the Current culture from System.Globalization.CultureInfo.CurrentCulture and pass it to Translate();

History

  • 25th January, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Qutbuddin Kamaal

Software Developer (Senior)
Wave Infotech Solution PVT L.T.D
India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralUse the Google API instead PinmemberGrant Frisken16:39 1 Feb '10  
GeneralRe: Use the Google API instead PinmemberTed21020:22 7 Oct '10  
GeneralRe: Use the Google API instead PinmemberGrant Frisken0:36 7 Oct '10  
GeneralRe: Use the Google API instead Pinmemberbernard joseph20:55 14 Apr '11  
GeneralMy vote of 1 PinmemberSteve Hansen22:35 26 Jan '10  
GeneralAhum... Real-Time Multilingual WPF Demo PinmemberDavid Sleeckx22:33 26 Jan '10  
GeneralToo light PinmemberBizounours23:00 25 Jan '10  
GeneralDo not use it in production PinmemberRemi BOURGAREL21:34 25 Jan '10  
GeneralGreat! But... one little problem PinmemberAnt21008:04 25 Jan '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 25 Jan 2010
Article Copyright 2010 by Qutbuddin Kamaal
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid