Click here to Skip to main content
Licence CPOL
First Posted 15 Jan 2006
Views 190,264
Downloads 6,670
Bookmarked 123 times

Google Translator

By | 10 Mar 2010 | Article
An object that harnesses the power of Google's online natural language translation tools.

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 ("")) {
              if (parser.extractTo("", ref strTranslation)) {
                  strTranslation = StringParser.removeHtml (strTranslation);
              }
          }
      }
  }

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

Revision History

  • 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



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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionPortuguese to english translation problems Pinmemberbgsjust2:24 18 Apr '12  
AnswerRe: Portuguese to english translation problems PinmemberRavi Bhavnani6:05 18 Apr '12  
GeneralRe: Portuguese to english translation problems Pinmemberbgsjust9:08 18 Apr '12  
GeneralRe: Portuguese to english translation problems PinmemberRavi Bhavnani9:23 18 Apr '12  
GeneralRe: Portuguese to english translation problems Pinmemberbgsjust2:18 19 Apr '12  
GeneralRe: Portuguese to english translation problems Pinmemberbgsjust11:52 22 Apr '12  
AnswerRe: Portuguese to english translation problems PinmemberRavi Bhavnani11:54 22 Apr '12  
GeneralRe: Portuguese to english translation problems Pinmemberbgsjust12:01 22 Apr '12  
GeneralMy vote of 5 Pinmemberreza-mohamad8:06 13 Apr '12  
GeneralRe: My vote of 5 PinmemberRavi Bhavnani6:06 18 Apr '12  
QuestionOdd behaviour of demo app in German Pinmemberffonzz21:29 11 Apr '12  
AnswerRe: Odd behaviour of demo app in German PinmemberRavi Bhavnani6:06 18 Apr '12  
GeneralRe: Odd behaviour of demo app in German PinmemberAC16:22 25 Apr '12  
GeneralRe: Odd behaviour of demo app in German PinmemberRavi Bhavnani7:01 25 Apr '12  
GeneralRe: Odd behaviour of demo app in German Pinmember01MrBean1:35 30 Apr '12  
AnswerRe: Odd behaviour of demo app in German PinmemberRavi Bhavnani5:33 30 Apr '12  
GeneralMy vote of 5 PinmemberInfarkt15:16 11 Apr '12  
GeneralRe: My vote of 5 PinmemberRavi Bhavnani6:06 18 Apr '12  
QuestionCharacter problem Pinmemberchutchus22:33 8 Apr '12  
AnswerRe: Character problem PinmemberRavi Bhavnani6:05 18 Apr '12  
QuestionI am not able to get this to work for Thai to English. Pinmemberrye0410:17 23 Feb '12  
AnswerRe: I am not able to get this to work for Thai to English. PinmemberRavi Bhavnani6:05 18 Apr '12  
Question[My vote of 2] Not Working, PinmemberMendor8122:47 19 Jan '12  
AnswerRe: [My vote of 2] Not Working, PinmemberRavi Bhavnani6:12 20 Jan '12  
QuestionRe: [My vote of 2] Not Working, PinmemberRavi Bhavnani15:30 22 Jan '12  

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
Web01 | 2.5.120517.1 | Last Updated 10 Mar 2010
Article Copyright 2006 by Ravi Bhavnani
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid