Click here to Skip to main content
Click here to Skip to main content

Google Translator

By , 13 Jan 2013
 

Note:
GoogleTranslator was originally written when screen scraping was the only way to access Google's online translation tools. As has been rightly pointed out in this article's forum, a more modern approach would be to use Google's AJAX APIs. See this link for more information. This screen scraper version of GoogleTranslator continues to be maintained at the request of CP readers.

What is it?

GoogleTranslator in action GoogleTranslator is an object that allows you to translate text using the power of Google's online language tools. GoogleTranslator is an example of a WebResourceProvider and makes use of the StringParser utility class, both of which are published elsewhere at CodeProject.

The demo app also performs a reverse translation, which can often appear amusing when compared to the original text. The app can be used as a poor man's resource translator for simple phrases, but you'd be wise to confirm the translation with a native speaker before using the results.

How do I use it?

You use GoogleTranslator by initializing it and calling its Translate() method.

// Initialize the translator
using RavSoft.GoogleTranslator;
Translator t = new GoogleTranslator();
t.SourceLanguage = "English";
t.TargetLanguage = "French";
t.SourceText = "Hello, how are you?";

// Translate the text
t.Translate();
Console.WriteLine (t.Translation);

How it works

GoogleTranslator works by posting a request to Google's online translation form and parsing the results. All the "heavy lifting" is done by the WebResourceProvider class, described elsewhere at CodeProject. WebResourceProvider fetches the content from Google and offers GoogleTranslator an opportunity to parse it.

  /// <summary>
  /// Parses the fetched content.
  /// </summary>
  protected override void parseContent()
  {
      // Initialize the scraper
      this.Translation = string.Empty;
      string strContent = this.Content;
      RavSoft.StringParser parser = new RavSoft.StringParser (strContent);

      // Scrape the translation
      string strTranslation = string.Empty;
      if (parser.skipToEndOf ("<span id=result_box")) {
          while (parser.skipToEndOf (" önmouseout=\"this.style.backgroundColor='#fff'\">")) {
              string translatedSegment = string.Empty;
              if (parser.extractTo("</span>", ref translatedSegment)) {
                 strTranslation += " " + StringParser.removeHtml (translatedSegment);
              }
          }
      }
  }

As you can see, the parsing logic is very simple! GoogleTranslator uses the the StringParser class to extract the translated text.

Revision History

  • 13 Jan 2013
    Added support for current full language set.
    Refixed bug that limited translation to first sentence.
    Fixed a bug that caused reverse translation to fail when accented characters were present.
     
  • 10 Mar 2010
    Added support for current full language set.
    Fixed bug that limited translation to first sentence.
     
  • 15 Feb 2010
    Even more parsing tweakage.
     
  • 28 Mar 2009
    More parsing tweakage.
     
  • 20 Mar 2007
    Tweaked parsing logic to conform to changes at Google's website.
     
  • 15 Jan 2006
    Initial version.

License

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

About the Author

Ravi Bhavnani
Technical Lead
Canada Canada
Member
Ravi Bhavnani is an ardent fan of Microsoft technologies who loves building Windows apps, especially PIMs, system utilities, and things that go bump on the Internet. During his career, Ravi has developed expert systems, desktop imaging apps, marketing automation software, EDA tools, a platform to help people find, analyze and understand information, trading software for institutional investors and advanced data visualization solutions. He currently works for a company that provides enterprise workforce management solutions to large clients.
 
His interests include the .NET framework, reasoning systems, financial analysis and algorithmic trading, NLP, CHI and UI design. Ravi holds a BS in Physics and Math and an MS in Computer Science and was a Microsoft MVP (C++ and C# in 2006 and 2007). He is also the co-inventor of 2 patents on software security and generating data visualization dashboards. His claim to fame is that he crafted CodeProject's "joke" forum post icon.
 
Ravi's biggest fear is that one day he might actually get a life, although the chances of that happening seem extremely remote.

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionNice article! But one query Pinmemberrobzz2120 May '13 - 21:22 
GeneralMy vote of 5 Pinmemberyhlun18 May '13 - 5:11 
Questionend of paragraphs or carriage return Pinmemberbgsjust28 Apr '13 - 11:17 
QuestionProxy Pinmemberanga15 Jan '13 - 22:11 
Questionenglish into urdu PinmemberMember 930213510 Jan '13 - 8:44 
QuestionStringParser not found PinmemberBhavika.R27 Dec '12 - 0:25 
QuestionGreat job!! Pinmemberdeddy8316 Dec '12 - 20:24 
QuestionJapanese~ Pinmemberkuronekoneko9 Dec '12 - 16:28 
QuestionNice Pinmemberario rakip29 Nov '12 - 2:27 
QuestionRequest in VB.Net Pinmemberirvieboy19 Nov '12 - 21:17 
QuestionGreat work - Here is a little bug fix ... PinmemberJohn Whiteman21 Aug '12 - 2:06 
QuestionAuto-detect PinmemberKendoTM31 Jul '12 - 0:21 
BugThe bug that limits the translation of the first sentence again? PinmemberMember 462172825 Jul '12 - 2:18 
QuestionChinese to English - 2012年06月13日 Pinmemberdevvvy13 Jun '12 - 23:45 
QuestionReally great work! PinmemberVali Radu29 May '12 - 4:56 
QuestionGoogle Translator PinmemberMiguel.Thewolf27 May '12 - 1:26 
QuestionPortuguese to english translation problems Pinmemberbgsjust18 Apr '12 - 2:24 10 
GeneralMy vote of 5 Pinmemberreza-mohamad13 Apr '12 - 8:06 
QuestionOdd behaviour of demo app in German Pinmemberffonzz11 Apr '12 - 21:29 
GeneralMy vote of 5 PinmemberInfarkt11 Apr '12 - 15:16 
QuestionCharacter problem Pinmemberchutchus8 Apr '12 - 22:33 
QuestionI am not able to get this to work for Thai to English. Pinmemberrye0423 Feb '12 - 10:17 
Question[My vote of 2] Not Working, PinmemberMendor8119 Jan '12 - 22:47 15 
Questionplease check this error PinmemberMember 852170615 Jan '12 - 1:55 
Questionquestion Pinmemberrorirami11 Nov '11 - 10:15 
GeneralMy vote of 5 PinmemberOmar Gamil8 Feb '11 - 23:09 
QuestionAlways show result ??? Pinmemberevilchong4 Dec '10 - 5:21 
GeneralSwap between languages PinmemberIon Florian30 Nov '10 - 3:52 
GeneralGoogle translator toolkit PinmemberABlokha7711 May '10 - 13:03 
Generallength limited! Pinmemberswanhfs6 Apr '10 - 4:47 
GeneralError Msg PinmemberRami Shareef19 Mar '10 - 19:48 
GeneralMy vote of 1 Pinmemberluisnike1910 Mar '10 - 7:05 
GeneralMy vote of 1 PinmemberRemi BOURGAREL15 Feb '10 - 20:52 
GeneralDOES NOT WORK! Pinmemberharoun0115 Feb '10 - 8:23 
General[My vote of 2] ajax.googleapis.com PinmemberPriyank Bolia6 Jan '10 - 2:45 12 
GeneralOnly one sentence Pinmemberanga26 Nov '09 - 21:06 
Generalfyi: Google changed the returned html Pinmemberron47717 Nov '09 - 11:16 
Questionwhich version of vb should I open the code in? PinmemberDibyendu Biswas5 Sep '09 - 9:50 
GeneralJapanese Support PinmemberAlien8115 Jun '09 - 18:01 
GeneralUnicode support (added code) Pinmemberron47731 Mar '09 - 12:15 
QuestionJapanese support PinmemberDavidA20056 Mar '09 - 1:15 
QuestionIs there a VB Version Pinmemberelchalateco8 Jul '08 - 2:27 
GeneralNoticed something strange Pinmemberjwaldner4 Apr '08 - 6:07 
GeneralDoesn't Work Anymore PinmemberScott Bruhnsen27 Mar '08 - 6:59 
GeneralUnicode complaince required - then we can support 6 more languages PinmemberYogesh P. Dhakad20 May '07 - 20:36 
GeneralProblems using unicode Pinmemberdiesel16007 May '07 - 3:45 
Jokeone joke PinmemberSalman078622 Mar '07 - 23:09 
QuestionDoesnt work PinmemberUnRusoDeCaracas20 Mar '07 - 19:40 
GeneralProblem Pinmemberhitesh_sharma20 Mar '07 - 7:59 
GeneralTranslation Cutting Off Last Letter Pinmemberpgorman16 Oct '06 - 5:46 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 13 Jan 2013
Article Copyright 2006 by Ravi Bhavnani
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid