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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionNice article! But one querymemberrobzz2120 May '13 - 21:22 
AnswerRe: Nice article! But one queryprofessionalRavi Bhavnani19hrs 53mins ago 
GeneralMy vote of 5memberyhlun18 May '13 - 5:11 
GeneralRe: My vote of 5professionalRavi Bhavnani18 May '13 - 6:47 
Questionend of paragraphs or carriage returnmemberbgsjust28 Apr '13 - 11:17 
AnswerRe: end of paragraphs or carriage returnprofessionalRavi Bhavnani19hrs 52mins ago 
QuestionProxymemberanga15 Jan '13 - 22:11 
AnswerRe: ProxymemberRavi Bhavnani16 Jan '13 - 2:03 
GeneralRe: Proxymemberanga16 Jan '13 - 2:05 
GeneralRe: Proxymemberanga6 Feb '13 - 21:34 
AnswerRe: ProxymemberRavi Bhavnani7 Feb '13 - 7:36 
GeneralRe: Proxymemberanga7 Feb '13 - 19:09 
GeneralRe: Proxymemberanga12 May '13 - 21:39 
GeneralRe: ProxyprofessionalRavi Bhavnani13 May '13 - 2:22 
Questionenglish into urdumemberMember 930213510 Jan '13 - 8:44 
AnswerRe: english into urdumemberRavi Bhavnani10 Jan '13 - 8:53 
GeneralRe: english into urdumemberMember 930213513 Jan '13 - 7:18 
GeneralRe: english into urdumemberRavi Bhavnani13 Jan '13 - 8:40 
QuestionStringParser not foundmemberBhavika.R27 Dec '12 - 0:25 
AnswerRe: StringParser not foundmemberRavi Bhavnani27 Dec '12 - 5:38 
QuestionGreat job!!memberdeddy8316 Dec '12 - 20:24 
AnswerRe: Great job!!memberRavi Bhavnani17 Dec '12 - 1:41 
AnswerRe: Great job!!memberRavi Bhavnani7 Feb '13 - 7:38 
QuestionJapanese~memberkuronekoneko9 Dec '12 - 16:28 
AnswerRe: Japanese~memberRavi Bhavnani10 Dec '12 - 7:56 
GeneralRe: Japanese~memberkuronekoneko11 Dec '12 - 7:31 
GeneralRe: Japanese~memberRavi Bhavnani11 Dec '12 - 7:43 
QuestionNicememberario rakip29 Nov '12 - 2:27 
AnswerRe: NicememberRavi Bhavnani29 Nov '12 - 2:30 
QuestionRequest in VB.Netmemberirvieboy19 Nov '12 - 21:17 
AnswerRe: Request in VB.NetmemberRavi Bhavnani20 Nov '12 - 2:15 
QuestionGreat work - Here is a little bug fix ...memberJohn Whiteman21 Aug '12 - 2:06 
AnswerRe: Great work - Here is a little bug fix ...memberRavi Bhavnani21 Aug '12 - 4:18 
QuestionAuto-detectmemberKendoTM31 Jul '12 - 0:21 
AnswerRe: Auto-detectmemberRavi Bhavnani31 Jul '12 - 2:17 
BugThe bug that limits the translation of the first sentence again?memberMember 462172825 Jul '12 - 2:18 
GeneralRe: The bug that limits the translation of the first sentence again?memberMember 462172825 Jul '12 - 2:30 
GeneralRe: The bug that limits the translation of the first sentence again?memberRavi Bhavnani25 Jul '12 - 3:26 
AnswerRe: The bug that limits the translation of the first sentence again?memberRavi Bhavnani13 Jan '13 - 8:42 
QuestionChinese to English - 2012年06月13日memberdevvvy13 Jun '12 - 23:45 
AnswerRe: Chinese to English - 2012年06月13日memberRavi Bhavnani14 Jun '12 - 1:38 
QuestionReally great work!memberVali Radu29 May '12 - 4:56 
AnswerRe: Really great work!memberRavi Bhavnani29 May '12 - 4:58 
QuestionGoogle TranslatormemberMiguel.Thewolf27 May '12 - 1:26 
AnswerRe: Google TranslatormemberRavi Bhavnani27 May '12 - 7:27 
QuestionPortuguese to english translation problemsmemberbgsjust18 Apr '12 - 2:24 
AnswerRe: Portuguese to english translation problemsmemberRavi Bhavnani18 Apr '12 - 6:05 
GeneralRe: Portuguese to english translation problemsmemberbgsjust18 Apr '12 - 9:08 
GeneralRe: Portuguese to english translation problemsmemberRavi Bhavnani18 Apr '12 - 9:23 
GeneralRe: Portuguese to english translation problemsmemberbgsjust19 Apr '12 - 2:18 

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

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